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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector <int> g;
int q,n;
int calc(int x){
    int temp = 0;
    for(auto i : g){
        int p = i - x - 1 + (i - x == 1 ? 1 : 0);
        if(p <= 0)
            break;
        temp += p;
        x += 4;
    }
    return temp;
}
void solve(){
    string t; cin >> t;
    int first=0,last=0,x,czy=0,w=n;
    g.clear();
    for(int i = 0; i < n; i++){
        x = t[i] - '0';
        if(x && last){
            if(czy)
                g.push_back(last);
            else
                first = last;
            last = 0;
        }
        else if(!x)
            last++;
        if(x)
            czy = 1;
    }
    sort(g.begin(), g.end(),greater<int>());
    if(max(first,last) == 1 && !g.size()){
        cout << n - 1 << endl;
        return;
    }
    if(first)
        w = min(w,n - (first + calc(2)));
    if(last)
        w = min(w,n - (last  + calc(2)));
    if(first && last)
        w = min(w,n - (last + first - 1 + calc(4)));
    w = min(w,n-calc(0));
    cout << w << endl;
}
int main(){
    ios_base :: sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> q;
    while(q--){
        cin >> n;
        solve();
    }
}