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
167
168
169
170
171
172
173
174
175
#include <sstream>
#include <vector>
#include <iostream>
#include <list>
#include <iterator>


using namespace std;

/*

2
42
000010010001000001001000100000000000000000
69
010100100010000001001000001000100010000000000000000000000000000000011


 1
 3
 101

 1
 20
11010001110110111100

 */
list<int> initOneEnd(string cities, int n) {
    list<int> oneEnd;

    int start = 0;
    for (int i = 0; i < n; ++i) {
        if (cities[i] == '0') {
            ++start;
        } else {
            break;
        }
    }
    if (start > 0) {
        oneEnd.push_front(start);
    }

    int end = 0;
    for (int i = n - 1; i >= 0; --i) {
        if (cities[i] == '0') {
            ++end;
        } else {
            break;
        }
    }
    if (end > 0) {
        oneEnd.push_front(end);
    }
    oneEnd.sort(greater<int>());
    return oneEnd;
}

list<int> initTwoEnd(string cities, int n) {
    list<int> twoEnd;

    bool isBlockStarted = false;
    bool isBeggining = true;
    int blockLength = 0;

    for (int i = 0; i < n; i++) {
        if (isBeggining) {
            if (cities[i] == '1') {
                isBeggining = false;
            }

        } else {
            if (cities[i] == '0' && !isBlockStarted) {
                isBlockStarted = true;
                ++blockLength;
            } else if (cities[i] == '0' && isBlockStarted) {
                ++blockLength;
            } else if (cities[i] == '1' && isBlockStarted) {
                isBlockStarted = false;
                twoEnd.push_front(blockLength);
                blockLength = 0;
            } else if (cities[i] == '1' && !isBlockStarted) { ; // do nothing
            }
        }
    }
    twoEnd.sort(greater<int>());
    return twoEnd;
}

bool isVirusFree(string cities, int n) {
    for (int i = 0; i < n; i++) {
        if (cities[i] == '1') {
            return false;
        }
    }
    return true;
}

int pandemia(string cities, int n) {
    if (isVirusFree(cities, n)) {
        return 0;
    }
    list<int> oneEnd = initOneEnd(cities, n);
    list<int> twoEnd = initTwoEnd(cities, n);

    int days = 0;
    int result = 0;

    while (!oneEnd.empty() || !twoEnd.empty()) {

        if (oneEnd.empty()) {
            int portion = twoEnd.front();
            twoEnd.pop_front();
            if (portion - 1 > days * 2) {
                result += (portion - 1 - (2 * days));
                days += 2;
            } else if (portion - 1 == days * 2) {
                result += 1;
                days += 1;
            }
        } else if (twoEnd.empty()) {
            int portion = oneEnd.front();
            oneEnd.pop_front();
            if (portion > days) {
                result += (portion - days);
                days++;
            }
        } else {
            int oneEndStart = oneEnd.front();
            int twoEndStart = twoEnd.front();

            if (oneEndStart * 2 >= twoEndStart) {
                //start with one end
                int portion = oneEnd.front();
                oneEnd.pop_front();
                if (portion > days) {
                    result += (portion - days);
                    days++;
                }

            } else {
                int portion = twoEnd.front();
                twoEnd.pop_front();
                if (portion - 1 > days * 2) {
                    result += (portion - 1 - (2 * days));
                    days += 2;
                } else if (portion - 1 == days * 2) {
                    result += 1;
                    days += 1;
                }
            }
        }
    }
    return n - result;
}


int main() {
    int t, i;
    cin >> t;
    string str;

    int n_array[t];
    string str_array[t];

    for (i = 0; i < t; i++) {
        cin >> n_array[i];
        cin >> str_array[i];
    }

    for (i = 0; i < t; i++) {
        cout << pandemia(str_array[i], n_array[i]) << endl;
    }

    return 0;
}