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
#include<bits/stdc++.h>
using namespace std;

const int MAXN = 1e5 + 1;


int go(vector<int> gaps, int t) {
	int res = 0;
	for(int l: gaps) {
		if(2*t+1 == l) res += 2*t;
		else res += min(l, 2*t+1);	
		t+=2;
	}
	return res;
}

void solve() {
	int n; cin>>n;
	string s; cin>>s;
	int sum = 0;
	for(int i=0; i<n; ++i) {
		if(s[i] == '1') {
			sum++;
		}
	}
	if(sum == 0) {
		cout<<0<<'\n';
		return;
	}
	vector<int> gaps;
	int cur = 0, pref = 0, suf = 0;
	for(int i=0; i<n; ++i) {
		if(s[i] == '0') {
			if(cur == i) pref++;
			cur++;
		}
		else {
			if(cur > 0 && i - cur > 0) gaps.push_back(cur);
			cur = 0;
		}
	}
	suf = cur;
	sort(gaps.rbegin(), gaps.rend());
	int ans = sum + min({go(gaps, 0) + pref + suf, 
						go(gaps, 1) + min(pref, suf),
						go(gaps, 2) + 1});;
	cout<<ans<<'\n';
	return;
}

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