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
#include<bits/stdc++.h>
using namespace std;
//#define int long long
int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        string s;
        cin>>s;
        vector<int>inds;
        int ctr=0;
        vector<int>holes;
        int lengththishole=0;
        for(int i=0;i<n;i++){
            if(s[i]=='1'){
                ctr++;
                inds.push_back(i);
                holes.push_back(lengththishole);
                lengththishole=0;
            }
            else{
                lengththishole++;
            }
        }
        holes.push_back(lengththishole);
        if(ctr==0){
            cout<<0<<'\n';
            continue;
        }
        if(ctr==1&&((inds[0]==0)||(inds[0]==n-1))){
            cout<<1<<'\n';
            continue;
        }
        if(ctr==1){
            cout<<min(n,2)<<'\n';
            continue;
        }
        /*int at1=0;
        int firsthole=0;
        while(s[at1]!='1'){
            at1++;
        }
        firsthole=at1;
        int at=n-1;
        while(s[at]!='1'){
            at--;
        }
        int lasthole=n-1-at;
        */
        vector<int>noextremes;
        for(int i=1;i<holes.size()-1;i++){
            noextremes.push_back(holes[i]);
        }
        sort(noextremes.begin(),noextremes.end(),greater<int>());
        //nie bierzemy bocznych
        int saved1=0;
        int at=0;
        int time=0;
        while(at<noextremes.size()){
            //cout<<saved1<<" ";
            int toadd=noextremes[at]-time*2-1;
            if(toadd==0)saved1++;
            if(toadd<=0)break;
            saved1+=toadd;
            time+=2;
            at++;
        }
        //bierzemy lewo lub prawo (wieksze);
        int saved2=0;
        saved2+=max(holes[0],holes[holes.size()-1]);
        at=0;
        time=1;
        while(at<noextremes.size()){
            int toadd=noextremes[at]-time*2-1;
            if(toadd==0)saved2++;
            if(toadd<=0)break;
            saved2+=toadd;
            time+=2;
            at++;
        }
        //bierzemy oba
        int saved3=0;
        saved3+=holes[0]+holes[holes.size()-1]-1;
        at=0;
        time=2;
        while(at<noextremes.size()){
            int toadd=noextremes[at]-time*2-1;
            if(toadd==0)saved3++;
            if(toadd<=0)break;
            saved3+=toadd;
            time+=2;
            at++;
        }
        cout<<n-max(saved1,max(saved2,saved3))<<'\n';
    }
}