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
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;


void iter(int& it, const vector<pair<int, pair<int, int> > >& sgs, vector<int>& sit, int& round) {
    for(; it >= 0; --it) {
        int l = sgs[it].second.first, r = sgs[it].second.second;
        l += round, r -= round;
        if(r - l - 1 <= 0) {
            break;
        }
        if(r - l - 1 >= 1) {
            sit[l + 1] = 2;
            ++round;
        }
        ++l, --r;
        if(r - l >= 1) {
            sit[r - 1] = 2;
            ++round;
        }
    }
}


int getRes(const vector<int>& ones, vector<int>& sit, int m, int n) {
    int res = 0;
    for(int i = 0; i < m; ++i) {
        ++res;
        int l = (i > 0) ? ones[i - 1] : -1, r = (i < m - 1) ? ones[i + 1] : n;
        for(int j = ones[i] - 1; j > l; --j) {
            if(sit[j] == 0) {
                sit[j] = 1;
                ++res;
            } else {
                break;
            }
        }
        for(int j = ones[i] + 1; j < r; ++j) {
            if(sit[j] == 0) {
                sit[j] = 1;
                ++res;
            } else {
                break;
            }
        }
    }
    return res;
}

int compute(int type, const vector<int>& ones, const vector<pair<int, pair<int,int>>>& sgs, vector<int> sit, int m, int n) {
    int it = sgs.size() - 1, round = 0;

    if(type == 1 || type == 3) {
        if(ones[0] > 0) {
            sit[ones[0] - 1] = 2;
            ++round;
        }
    }

    if(type == 2 || type == 3) {
        if(ones[m - 1] + round < n - 1) {
            sit[ones[m - 1] + round + 1] = 2;
            ++round;
        }
    }

    iter(it, sgs, sit, round);

    if(type == 0 || type == 2) {
        if(ones[0] - round > 0) {
            sit[ones[0] - round - 1] = 2;
            ++round;
        }
    }

    if(type == 0 || type == 1) {
        if(ones[m - 1] + round < n - 1) {
            sit[ones[m - 1] + round + 1] = 2;
            ++round;
        }
    }

    return getRes(ones, sit, m, n);
}

void solve() {
    int n;
    cin >> n;
    vector<int> ones, sit(n);
    for(int i = 0; i < n; ++i) {
        char c;
        cin >> c;
        sit[i] = c - '0';
        if(c == '1') {
            ones.push_back(i);
        }
    }
    
    int m = ones.size();
    if(m == 0) {
        cout << "0\n";
        return;
    }
    

    vector<pair<int, pair<int, int> >> sgs;

    for(int i = 0; i < m - 1; ++i) {
        sgs.push_back({ones[i + 1] - ones[i], {ones[i], ones[i + 1]}});
    }

    sort(sgs.begin(), sgs.end());


    int res = 1e9;
    for(int i = 0; i < 4; ++i) {
        res = min(res, compute(i, ones, sgs, sit, m, n));
    }
    cout << res << "\n";
}

int main() {
    ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    while(t--) {
        solve();
    }
}