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

template<class T, class Y>
ostream& operator<<(ostream& out, const pair<T,Y>& p)
{out << p.first << ": " << p.second; return out;}

template<class T>
ostream& __print(ostream& out, string st, string en, string sep, T con)
{out << st;typename T::size_type i=0; for (auto it=con.begin(); it!=con.end(); ++it, i++)
{out << *it << (i<con.size()-1 ? sep : "");}
out << en; return out;}

#define register_print1(a, b, c, d) template <class T> \
	ostream& operator<<(ostream& out, const a<T>& con)   \
	{return __print(out, b, c, d, con);}
#define register_print2(a, b, c, d) template <class T, class Y> \
	ostream& operator<<(ostream& out, const a<T, Y>& con)         \
	{return __print(out, b, c, d, con);}

register_print1(vector, "[", "]", ", ")
register_print1(set, "{", "}", ", ")
register_print1(unordered_set, "{", "}", ", ")
register_print2(map, "{", "}", ", ")
register_print2(unordered_map, "{", "}", ", ")

struct group {
	int days;
	int size;
	bool skrajne;
	int start;
};

void solve() {
	int n;
	cin >> n;

	vector<char> nums(n);
	long long full = 0;

	for (int i=0; i<n; i++) {
		cin >> nums[i];
		if (nums[i] == '1') {
			full++;
		}
	}

	cout << boolalpha;
	vector<group> groups;

	long long back = 0;
	long long fwd = 1;
	while (fwd < n) {
		while (nums[fwd] == '0') {
			fwd++;
		}
		if (nums[back] == '1') {
			back++;
		}
		int size = fwd - back;
		bool skrajne = (back == 0) || (fwd == n);
		int days = (skrajne ? size : size/2 + size%2);
		int start = back;
		group foo{days, size, skrajne, start};
		//cout << "start: " << start << ", size: " << size << ", days: " << days << ", skrajne: " << skrajne << "\n";
		// cout << start << " " << size << "\n";

		groups.push_back(foo);
		back = fwd;
		fwd++;
	}

	sort(groups.begin(), groups.end(), [&](group& lhs, group& rhs){
		return (lhs.days == rhs.days
						?	/*lhs.skrajne*/ (lhs.skrajne == rhs.skrajne
							? lhs.size > rhs.size
							: lhs.skrajne)
						:	lhs.days > rhs.days);
	});

	// for (auto x: groups) {
	// 	cout << "start: " << x.start
	// 	     << ", size: " << x.size
	// 			 << ", days: " << x.days
	// 			 << ", skrajne: " << x.skrajne << "\n";
	// }

	// int k = n;

	// long long iter = 0;
	// for (auto& x: groups) {
	// 	if (x.skrajne) {
	// 		int foo = max(0, x.size - iter);
	// 		if (foo) iter += 1;
	// 		k -= foo;
	// 	} else {
	// 		if (x.size % 2 == 1) {
	// 			int foo = max(0, x.size - iter*2);
	// 			if (foo) iter += 2;
	// 			k -= foo;
	// 		} else {
	// 			int foo = max(0, x.size - 1 - iter*2);
	// 			if (foo) iter += 2;
	// 			k -= foo;
	// 		}
	// 	}
	// }

	long long iter = 0;
	for (group x : groups) {
		int jew = (x.skrajne ? 1 : 2);
		if(iter >= x.days) {
			full += x.size;
			continue;
		}
		else {
			full += jew * iter;
		}

		if(jew == 2) {
			if(iter + 1 == x.days) {
				iter++;
				if(x.size % 2 == 0) {
					full++;
				}
			}
			else {
				iter += 2, full++;
			}
		} else {
			iter++;
		}
	}

	cout << full << "\n";

/*
1001000001000

1001000010
0100100001
0010010000

1111110011
*/


}

int main() {int it;cin>>it;while(it--)solve();}