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
#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SZ(a) ((int)(a).size())
#define ALL(a) (a).begin(), (a).end()

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;

constexpr int kInf = 1000 * 1000 * 1000 + 7;
constexpr long long kInfLL = 1e18;

// #include <atcoder/fenwicktree>
// using namespace atcoder;
// using mint = modint998244353;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int t;
  cin >> t;

  REP(i, t) {
    int n;
    cin >> n;

    string s;
    cin >> s;

    vi a, b;

    REP(i, n) {
      if (s[i] == '1') continue;
      int j = i;
      while (j < n && s[i] == s[j]) ++j;
      int len = j - i;
      if (i == 0 || j == n) {
        a.push_back(len);
      } else {
        b.push_back(len);
      }
      i = j - 1;
    }

    sort(a.rbegin(), a.rend());
    sort(b.rbegin(), b.rend());

    int mx = 0;
    for (int mask = 0; mask < (1 << SZ(a)); ++mask) {
      REP(i, SZ(b) + 1) {
        vi cur;
        bool failed = false;
        int result = 0;
        int c = 0;
        REP(j, 2) if (mask & (1 << j)) {
          result += max(a[j] - 2 * i - j, 0);
          ++c;
        }
        REP(j, i) {
          if (b[j] <= 2 * j) {
            failed = true;
            break;
          }
          result += max(b[j] - 2 * j - i, 1);
        }
        if (i < SZ(b) && b[i] - 2 * (2 * i + c) > 0) {
          ++result;
        }
        mx = max(mx, result);
        if (failed) break;
      }
    }
    cout << n - mx << '\n';
  }
}