1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Patryk Jędrzejczak

#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for(int i = 0; i < n; ++i)
#define REPR(i, n) for(int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define FORR(i, a, b) for(int i = b - 1; i >= a; --i)
#define EB emplace_back
#define MP make_pair
#define ST first
#define ND second
#define S size
#define RS resize
#define endl '\n'
#define L long long

template<class T> using V = vector<T>;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t;
  cin >> t;

  REP(f, t) {
    int n;
    string s;
    cin >> n >> s;

    bool virus_exists = false;
    REP(i, n) {
      if (s[i] == '1') {
        virus_exists = true;
      }
    }
    if (!virus_exists) {
      cout << 0 << '\n';
      continue;
    }

    int l = 0;
    while (s[l] == '0') {
      l++;
    }
    int r = 0;
    while (s[n - r - 1] == '0') {
      r++;
    }

    V<int> zeros;
    int actual = 0;
    FOR(i, l, n - r) {
      if (s[i] == '0') {
        actual++;
      }
      else if (actual > 0) {
        zeros.EB(actual);
        actual = 0;
      }
    }
    if (l < r) {
      swap(l, r);
    }

    int zs = zeros.size();
    sort(zeros.begin(), zeros.end());
    reverse(zeros.begin(), zeros.end());

    V<V<int> > dp(zs + 1, V<int>(3));
    V<V<int> > days(zs + 1, V<int>(3));

    if (l > 0) {
      dp[0][1] = l;
      days[0][1] = 1;
    }
    if (r > 1) {
      dp[0][2] = l + r - 1;
      days[0][2] = 2;
    }

    FOR(i, 1, zs + 1) {
      dp[i][0] = dp[i - 1][0];
      days[i][0] = days[i - 1][0];
      if (zeros[i - 1] - 2 * days[i][0] > 2) {
        dp[i][0] += zeros[i - 1] - 2 * days[i - 1][0] - 1;
        days[i][0] += 2;
      }
      else if (zeros[i - 1] - 2 * days[i][0] > 0) {
        dp[i][0]++;
        days[i][0]++;
      }

      dp[i][1] = dp[i - 1][1];
      days[i][1] = days[i - 1][1];
      if (zeros[i - 1] - 2 * days[i][1] > 2) {
        dp[i][1] += zeros[i - 1] - 2 * days[i - 1][1] - 1;
        days[i][1] += 2;
      }
      else if (zeros[i - 1] - 2 * days[i][1] > 0) {
        dp[i][1]++;
        days[i][1]++;
      }
      if (dp[i][0] + l - days[i][0] > dp[i][1]) {
        dp[i][1] = dp[i][0] + l - days[i][0];
        days[i][1] = days[i][0] + 1;
      }

      dp[i][2] = dp[i - 1][2];
      days[i][2] = days[i - 1][2];
      if (zeros[i - 1] - 2 * days[i][2] > 2) {
        dp[i][2] += zeros[i - 1] - 2 * days[i - 1][2] - 1;
        days[i][2] += 2;
      }
      else if (zeros[i - 1] - 2 * days[i][2] > 0) {
        dp[i][2]++;
        days[i][2]++;
      }
      if (dp[i][1] + r - days[i][1] > dp[i][2]) {
        dp[i][2] = dp[i][1] + r - days[i][1];
        days[i][2] = days[i][1] + 1;
      }
    }

    int res = max(dp[zs][0], dp[zs][1]);
    res = max(res, dp[zs][2]);
    cout << n - res << '\n';
  }
}