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

template<class C> C reversed(C c) {reverse(c.begin(),c.end()); return c;}
#define rep(i, n) for(int i=0;i<(int)(n);i++)
#define all(X) (X).begin(), (X).end()
#define mp make_pair
#define st first
#define nd second
typedef long long ll;
typedef pair<int,int> pii;

int _solve(const vector<int>& data, int days) {
    int res = 0;

    for(int i = 0;i < data.size();++i) {
        int resc = data[i] - 2 * days;
        if(resc <= 0)
            break;

        res += max(1, resc - 1);
        days += 2;
    }

    return res;
}

void solve() {
    int n;
    string str;
    cin >> n >> str;

    int left = 0, right = 0;
    while(left < n && str[left] == '0') left++;

    if(left != n)
        while(right < n && str[n - right - 1] == '0') right++;

    vector<int> data;
    int toResc = 0;
    for(int i = left;i < n - right;++i) {
        if(str[i] == '0') {
            ++toResc;
        }
        else if(toResc > 0) {
            data.push_back(toResc);
            toResc = 0;
        }
    }

    sort(all(data), greater<int>());

    int res0 = _solve(data, 0);
    int res1 = _solve(data, 1);
    int res2 = _solve(data, 2);

    cout << n - max(max(res0, res2 + left + right - 1), res1 + max(left, right)) << "\n";
}

int32_t main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;

    while(t --> 0)
        solve();

    return 0;
}