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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <bits/stdc++.h>

#pragma GCC optimize(-O3)
#pragma GCC optimize(Ofast)
#pragma GCC optimize("unroll-loops")

#define fi first
#define se second
#define p_b push_back
#define pll pair<ll,ll>
#define pii pair<int,int>
#define m_p make_pair
#define all(x) x.begin(),x.end()
#define sset ordered_set
#define sqr(x) (x)*(x)
#define pw(x) (1ll << (x))
#define sz(x) (int)x.size()
#define fout(x) {cout << x << "\n"; return; }

using namespace std;
typedef long long ll;
typedef long double ld;
const int MAXN = 1e5;
const int M = pw(16);
const long long mod = 1e9 + 7;
const int N = 2e5;
const int inf = 1e9;

template <typename T> void vout(T s){cout << s << endl;exit(0);}

void solve(){
    int n;
    cin >> n;
    string s;
    cin >> s;
    vector <int> le(n + 1), ri(n + 1), u(n + 10), lt(n + 10), rt(n + 10);
    vector <bool> f(n + 1);

    for(int i = 1; i <= n; i++) if(s[i - 1] == '1') f[i] = 1;

    int p = 0; 

    for(int i = 1; i <= n; i++) if(f[i]) {
        le[i] = i - p - 1;
        p = i;
    }

    p = n + 1;

    for(int i = n; i > 0; i--) if(f[i]) {
        ri[i] = p - i - 1;
        p = i;
    }

    multiset <int> s2, s1;

    vector < pair <int, pii> > st;

    vector <int> c;

    for(int i = 1; i <= n; i++) if(f[i]) c.p_b(i);

    for(int i = 1; i < c.size(); i++) if(le[c[i]]){
        s2.insert(le[c[i]]);        
    }

    if(c.empty()) {
        cout << 0 << "\n";
        return;
    }

    if(c.size() && le[c[0]] > 0) s1.insert(le[c[0]]);
    if(c.size() && ri[c.back()] > 0) s1.insert(ri[c.back()]);

    int ans = 0;
    
    for(int i = 0; ; i++){
        if(s2.empty() && s1.empty()) break;
        while(true){
            if(s2.empty() && s1.empty()) break;
            if(s1.empty() || s2.empty()){
                if(s1.empty()){
                    int x = *--s2.end();
                    s2.erase(--s2.end());
                    x -= i * 2;
                    if(x > 0){                        
                        ans++;                        
                        s1.insert(i + x - 1);
                        break;                                
                    }else{
                        s2.clear();
                        break;
                    }
                }else{
                    int x = *--s1.end();
                    s1.erase(--s1.end());
                    x -= i;            
                    if(x > 0){
                        ans += x;
                        break;
                    }else{
                        s1.clear();
                        break;
                    }
                }                
            }            
                        
            if((*--s2.end() + 1) / 2 > *--s1.end()){
                int x = *--s2.end();
                s2.erase(--s2.end());
                x -= i * 2;
                if(x > 0){                        
                    ans++;                        
                    s1.insert(i + x - 1);
                    break;                                
                }else{
                    s2.clear();            
                }
            }else{
                int x = *--s1.end();
                s1.erase(--s1.end());
                x -= i;            
                if(x > 0){
                    ans += x;
                    break;
                }else{
                    s1.clear();                    
                }
            }
        }        
    }

    cout << n - ans << "\n";
}

int main(){

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;

    while(t--){
        solve();
    }

    return 0;
}