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
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
#include <queue>
#include <bitset>
#include <utility>
#include <stack>

using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define MP make_pair
#define FOR(v,p,k) for(int v=(p);v<=(k);++v)
#define FORD(v,p,k) for(int v=(p);v>=(k);--v)
#define REP(i,n) for(int i=0;i<(n);++i)
#define VAR(v,i) __typeof(i) v=(i)
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define PB push_back
#define ST first
#define ND second
#define SIZE(x) (int)x.size()
#define ALL(c) c.begin(),c.end()

#define ODD(x) ((x)%2)
#define EVEN(x) (!(ODD(x)))
VI d, h2, h1;
int main() {
    d.reserve(100009);
    h2.reserve(100009);
    h1.reserve(100009);

    int t;
    scanf("%d\n", &t);
    while(t--) {
        d.clear();
        h2.clear();
        h1.clear();
        int n;
        scanf("%d\n", &n);
        REP(i, n) {
            char c;
            scanf("%c", &c);
            d.PB(c-'0');
        }
        int ones = 0;
        REP(i,n) {
            ones += (d[i] == 1);
        }
        if (ones == 0) {
            printf("0\n");
            continue;
        }

        int curr0 = 0;
        REP(i,n) {
            if (d[i] == 0) {
                curr0++;
            } else {
                if (curr0 > 0) {
                    if ((h2.size() == 0) && (d[0] == 0) && (h1.size() == 0)) {
                        h1.PB(curr0);
                    } else {
                        h2.PB(curr0);
                    }
                }
                curr0 = 0;
            }
        }
        if (curr0 > 0) {
            h1.PB(curr0);
        }
        sort(ALL(h1));
        //FOREACH(it, h1) printf("h1: %d\n", *it);
        sort(ALL(h2));
        //FOREACH(it, h2) printf("h2: %d\n", *it);

        int epochs = 0;
        while ((h1.size() + h2.size()) > 0) {
            if ((h1.size() > 0) && (h1.back() - epochs <= 0)) {
                ones += h1.back();
                h1.pop_back();
                continue;
            }
            if ((h1.size() == 2) && (h1[0] - epochs <= 1)) {
                swap(h1[0], h1[1]);
                ones += h1.back();
                h1.pop_back();
                continue;
            }
            if ((h2.size() > 0) && (h2.back() - 2 * epochs <= 0)) {
                ones += h2.back();
                h2.pop_back();
                continue;
            }
            bool h1_once = false;
            bool h2_once = false;
            if (h2.empty()) {
                h1_once = true;
            } else if (h1.empty()) {
                h2_once = true;
            } else {
                int b2 = h2.back() - 2 * epochs;
                int b2_gain = (b2 == 1) ? 1 : b2-1;

                int b1_gain = h1.back() - epochs;
                int b1_0_gain = 0;
                if (h1.size() == 2) {
                    b1_0_gain = h1[0] - epochs - 1;
                }
                if (b2_gain > b1_gain + b1_0_gain)  {
                    h2_once = true;
                } else {
                    h1_once = true;
                }
            }
            //if ((h2.empty()) || ((h1.size() > 0) && ((h2.back() - 2 * epochs) <= 3) && (h1.back() - epochs > 0))) {
            if (h1_once) {
                ones +=  epochs;
                epochs++;
                h1.pop_back();
                continue;
            }
            //if (h2.size() > 0) {
            if (h2_once) {
                int b = h2.back();
                int curr = b - 2 * epochs;
                if (curr == 1) {
                    ones += 2 * epochs;
                    epochs++;
                } else {
                    ones += 2 * epochs + 1;
                    epochs += 2;
                }
                h2.pop_back();
                continue;
            }
            //assert(0);
        }
        printf("%d\n", ones);

    }
    return 0;
}