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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

struct spread {
	int size;
	int speed;
	int round;
};

struct spread_comp {
	bool operator()(const spread& left, const spread& right) {
		int l_eff_size = left.speed == 1 ? left.size * 2 : left.size;
		int r_eff_size = right.speed == 1 ? right.size * 2 : right.size;
		return (left.round == right.round) ? (l_eff_size > r_eff_size) : (left.round > right.round);
	}
};

void solve() {
	int n;
	std::string in;
	std::cin >> n >> in;
	int last_infected = -1;
	std::vector<spread> spreads;
	for (int i = 0; i < n; ++i) {
		if (in[i] == '1') {
			if (last_infected != i - 1) {
				spread s;
				s.size = i - last_infected - 1;
				s.speed = ((last_infected == -1) ? 1 : 2);
				s.round = ((s.speed == 2) ? ((s.size + 1) / 2) : s.size);
				spreads.push_back(s);
			}
			last_infected = i;
		}
	}
	if (last_infected != n - 1) {
		spread s;
		s.size = n - last_infected - 1;
		s.speed = 1;
		s.round = s.size;
		spreads.push_back(s);
	}

	spread_comp comp;
	std::sort(spreads.begin(), spreads.end(), comp);
	if (spreads.size() == 1 && spreads[0].size == n) {
		std::cout << 0 << std::endl;
		return;
	}
	int round = 0;
	int pos = 0;
	int saved = 0;
	while (pos < spreads.size() && round < spreads[pos].round) {
		spread& curr = spreads[pos++];
		if (curr.speed == 1) {
			saved += curr.size - round;
			++round;
		}
		else {
			if (curr.size - round * 2 == 1) {
				saved += 1;
				++round;
			}
			else {
				saved += curr.size - round * 2 - 1;
				round += 2;
			}
		}
	}
	std::cout << n - saved << std::endl;
}

int main() {
	int t;
	std::cin >> t;

	while (t--) {
		solve();
	}

	return 0;
}