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
#include <iostream>
#include <queue>
#define rep(a,b) for(int a=0; a<(b); ++a)
#define nl "\n"

using namespace std;

struct city{
	int num; int border;
};

auto cmp = [](const city& a, const city& b){
	/*if(a.border^b.border){
		if(a.border){
			return a.num<2;
		}
		else return b.num>1;
	}
	else return a.num<b.num;*/
	if(a.border==b.border){
		return a.num<b.num;
	}
	else{
		return a.border>b.border;
	}
};

int t,n;
priority_queue<city,vector<city>,decltype(cmp)> cities(cmp);
int main()
{
	cin>>t;
	rep(j,t){
		cin>>n;
		int counter=0;
		int border=1;
		rep(i,n){
			char c;
			cin>>c;
			if(c-'0'){
				if(counter){
					if(counter>2||border){
						cities.push({counter,border});
					}else{
						cities.push({1,1});
					}
					counter=0;
				}
				border=0;
			}
			else{
				++counter;
			}
		}
		if(counter) {cities.push({counter,1});}
		int day=0;
		long long res=0;
		while(!cities.empty()){
			city now=cities.top();
			clog<<now.num<<" "<<now.border<<nl;
			cities.pop();
				if(!now.border){
					if(now.num-(2*day)>1){
						clog<<now.num-(2*day)-1<<" ";
						res+=(now.num-(2*day)-1);
						day+=2;
					}
					else if(now.num-(2*day)>0){
						clog<<now.num-(2*day)<<" ";
						res+=(now.num-(2*day));
						day++;
					}
				}
				else if(now.num-day>0){
						if(/*now.num-day>1||cities.empty()*/true){
						clog<<now.num-day<<" ";
						res+=(now.num-day);
						day++;
					}
				}
			clog<<nl;
		}
		cout<<n-res<<nl;
	}
	//clog<<cmp({3,0},{2,1})<<nl;
	return 0;
}