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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <bits/stdc++.h>
#define FOR(i,n) for(int i = 0; i<n; ++i)
#define FOREACH(v, a) for(auto & v : (a))
#define X first
#define Y second
#define PR std::pair
#define MPR std::make_pair
typedef long long ll;
typedef std::vector<int> VI;

using namespace std;

#pragma region Ready functions

int fastPower(int a, int b){
    int res=1, pop_a = a;
    while(b){
        if(b&1) res *= pop_a;
        pop_a *= pop_a;
        b>>=1;
    }
    return res;
}

#pragma endregion


bitset<1000001> vaxxed;

priority_queue<pair<int, pair<int,int>>> citySegments;

vector<int> cities;

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

    int t;
    cin>>t;

    while(t--){

        int n;
        cities.clear();

        cin>>n;

        int covidCont = 0;

        for(int i = 0; i<n; i++){
            char x;
            cin>>x;
            cities.push_back(x-'0');
            if(x=='1') covidCont++;
        }

        if(covidCont==0){
            cout<<0<<endl;
            continue;
        }

        //create left and right segments;

        vaxxed[0] = 1;
        int lewy = 0;

        for(int i = 0; i<n; i++){
            if(cities[i]==1) break;
            lewy = i;
        }

        if(cities[0] != 1){
            citySegments.push({(lewy + 1)*2, {0, lewy}});
            lewy++;
        }


        vaxxed[n-1] = 1;
        int prawy = n-1;

        for(int i = n-1; i>=0; i--){
            if(cities[i]==1) break;
            prawy = i;
        }

        if(cities[n-1] != 1){
            citySegments.push({(n-prawy)*2, {prawy, n-1}});
            prawy--;
        }
        

        for(int i = lewy; i<=prawy; i++){

            if(cities[i]==1) continue;

            int j;

            for(j = i; j<=prawy; j++){
                if(cities[j]==1){
                    j--;
                    break;
                }
            }

            //cout<<i<<" - "<<j<<endl;
            citySegments.push({j-i+1, {i, j}});

            j++;
            i = j;
        }

        int vaxxed = 0;
        int time_passed = 0;

        while(citySegments.size()){


            int i = citySegments.top().second.first;
            int j = citySegments.top().second.second;
            int sz = citySegments.top().first;
            citySegments.pop();

            //cout<<i<<" : "<<j<<endl;
            //cout<<vaxxed<<endl;
            //cout<<time_passed<<endl;


            if(j-i == 0){
                if(1-time_passed > 0){
                    vaxxed++;
                    time_passed +=1;
                }
                continue;
            }

            //we need to vax only one side
            if(sz/(j-i+1) == 2){
                if(sz/2-time_passed > 0){
                    vaxxed += sz/2-time_passed;
                    time_passed += 1;
                }
            }
            else{
                if(sz-time_passed*2 > 0){
                    if(sz - time_passed*2 == 1){
                        vaxxed += 1;
                        time_passed += 1;
                    }
                    else{
                        vaxxed += max(sz - time_passed*2 - 1, 0);
                        if(sz - time_passed*2 - 1 == 1)
                            time_passed += 1;
                        else time_passed += 2;
                    } 
                }
            }
            //cout<<i<<" "<<j<<endl;
            //cout<<vaxxed<<endl;
            //cout<<time_passed<<endl;
        }

        cout<<n-vaxxed<<endl;


    }
}