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
#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
typedef pair<int, int> pii;
typedef pair<lld, lld> pll;
#define ff first
#define ss second
#define mp make_pair

constexpr int N = 1 << 20;
pii t[N];
char s[N];

int chk(int p, int m) {
	int res = p - (m << 1);
	
	if (res <= 0)
		return 0;
	
	return max(res - 1, 1);
}

void solve() {
	int n, k = 0, p = 0, q = 0, m = 0, res = 0;
	scanf("%d", &n);
	scanf("%s", s);
	
	while (p < n && s[p] == '0') 
		++p;
		
	if (p == n) {
		puts("0");
		return;
	}
	
	while (q < n && s[n - 1 - q] == '0') 
		++q;
	
	for (int i = p; i < n - q; ++i) {
		if (s[i] == '0') {
			++t[k].ff;
		}
		else {
			if (t[k].ff > 0) {
				t[k].ff = chk(t[k].ff, 0);
				t[k].ss = 2;
				++k;
			}
		}
	}

	if (t[k].ff > 0) {
		t[k].ff = chk(t[k].ff, 0);
		t[k].ss = 2;
		++k;
	}
	
	
	
	if (p > 0) {
		t[k] = mp(p << 1, 1);
		++k;
	}
	
	if (q > 0) {
		t[k] = mp(q << 1, 1);
		++k;
	}
		
		
		
	sort(t, t + k, greater<pii>());
	
	for (int i = 0; i < k; ++i) {
		if (t[i].ss == 1) {
			t[i].ff >>= 1;
			t[i].ff -= m;
			
			if (t[i].ff > 0) {
				res += t[i].ff;
				++m;
			}
			else {
				break;
			}
		}
		else {
			t[i].ff = chk(t[i].ff + 1, m);
			
			if (t[i].ff > 0){
				res += t[i].ff;
				m += min(2, t[i].ff);
			}
			else {
				break;
			}
		}
	}
	
	
	
	for (int i = 0; i < k; ++i) 
		t[i] = mp(0, 0);
	
	printf("%d\n", n - res);
}

int main() {
	int T = 1;
	scanf("%d", &T);
	
	while(T--) {
		solve();
	}
	
	return 0;
}
/*

3
8
00110100
10
1001000010
4
0000


1
20
00010000010100101000


1
20
00010000000000000000

*/