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

using namespace std;

bool desc(int a, int b) {
    return a > b;
}

pair<int, int> count_twos(vector<int> &vec, int tick) {
    int sum = 0;
    int penalty = tick * 2;
    int ticks = 0;

    for (const auto &item: vec) {
        int actual = item - penalty;
        if (actual > 0) {
            if (actual == 1) {
                sum += 1;
                penalty += 2;
                ticks += 1;
            }
            if (actual == 2) {
                sum += 1;
                penalty += 2;
                ticks += 1;
            }
            if (actual > 2) {
                sum += actual - 1;
                penalty += 4;
                ticks += 2;
            }
        } else {
            break;
        }
    }
    return make_pair(sum, ticks);
}

void solve_test_case() {
    int n;
    string bits;
    cin >> n;
    cin >> bits;

    vector<int> twos;

    int first = 0;
    int last = 0;
    int zeros = 0;
    bool was_one = false;

    for (const auto &bit: bits) {
        if (bit == '0') {
            zeros += 1;
        }
        if (bit == '1') {
            if (zeros > 0) {
                if (!was_one) {
                    first = zeros;
                } else {
                    twos.push_back(zeros);
                }
                zeros = 0;
            }
            was_one = true;
        }
    }
    if (zeros > 0) {
        if (was_one) {
            last = zeros;
        }
    }

    if (!was_one) {
        cout << "0" << endl;
        return;
    }

    if (first == 0 && last == 0 && twos.empty()) {
        cout << n << endl;
        return;
    }

    sort(twos.begin(), twos.end(), desc);

    vector<int> sols;

    if (first != 0 && last != 0) {
        // first last twos
        if (last == 1) {
            // first twos
            int val = n - (first + count_twos(twos, 1).first);
            sols.push_back(val);
        } else { // > 1
            // first last twos
            int val = n - (first + last - 1 + count_twos(twos, 2).first);
            sols.push_back(val);
        }
        // last first twos
        if (first == 1) {
            // last twos
            int val = n - (last + count_twos(twos, 1).first);
            sols.push_back(val);
        } else { // > 1
            // last first twos
            int val = n - (last + first - 1 + count_twos(twos, 2).first);
            sols.push_back(val);
        }

        // first twos last
        pair<int, int> counted = count_twos(twos, 1);
        int val = n - (first + counted.first + max(last - counted.second - 1, 0));
        sols.push_back(val);

        // last twos first
        counted = count_twos(twos, 1);
        val = n - (last + counted.first + max(0, first - counted.second - 1));
        sols.push_back(val);

        // twos last first
        counted = count_twos(twos, 0);
        val = n - (counted.first + max(0, last - counted.second) + max(0, first - counted.second - 1));
        sols.push_back(val);

        // twos first last
        counted = count_twos(twos, 0);
        val = n - (counted.first + max(0, first - counted.second) + max(0, last - counted.second - 1));
        sols.push_back(val);

    } else if (first == 0 && last != 0) {
        // last twos
        sols.push_back(n - (last + count_twos(twos, 1).first));

        // twos last
        const pair<int, int> &counted = count_twos(twos, 0);
        sols.push_back(n - (counted.first + max(0, last - counted.second)));
    } else if (first != 0) {
        // first twos
        sols.push_back(n - (first + count_twos(twos, 1).first));

        // twos first
        const pair<int, int> &counted = count_twos(twos, 0);
        sols.push_back(n - (counted.first + max(0, first - counted.second)));
    } else {
        sols.push_back(n - count_twos(twos, 0).first);
    }

    cout << *min_element(sols.begin(), sols.end()) << endl;
}

int main() {
    ios_base::sync_with_stdio(false);

    int t;
    cin >> t;
    while (t--) {
        solve_test_case();
    }

    return 0;
}