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

inline void brutal_force()
{
	int n;
	std::cin >> n;

	std::map<int, int, std::greater<>> reg;
	std::map<int, int, std::greater<>> borders;

	int len = 0;
	bool border = false;
	bool r = false;
	for(int i=0;i<n;i++)
	{
		unsigned char c;
		std::cin >> c;

		if(c=='0')
		{
			if(i==0) border = true;
			len++;
			r = true;
		}
		else
		{
			if(r)
			{
				if(border)
				{
					borders[len]++;
					border = false;
				}
				else
				{
					reg[len]++;
				}
				r = false;
				len = 0;
			}
		}
		//std::cout << "i=" << i << " c=" << c << " len=" << len << " border=" << std::boolalpha << border << "\n";
	}
	if(len!=0)
	{
		borders[len]++;
	}
	/*
	std::cout << "reg: ";
	for(auto it=reg.begin(); it!=reg.end(); it++)
	{
		std::cout << "[" << it->first << " : " << it->second << "] ";
	}
	std::cout << "\n";
	std::cout << "borders: ";
	for(auto it=borders.begin(); it!=borders.end(); it++)
	{
		std::cout << "[" << it->first << " : " << it->second << "] ";
	}
	std::cout << "\n";
*/
	int shitLen = 0;
	int saved = 0;
	while(1)
	{
		if(borders.empty() && reg.empty()) break;
		const auto rf = reg.begin()->first;
		/*std::cout << "rf = " << rf << "\n";
		std::cout << "rf-shitLen = " << rf-shitLen << "\n";*/
		if(not borders.empty())
		{
			bool noop = false;
			while(not noop)
			{
				const auto bf = borders.begin()->first;
				/*std::cout << "bf = " << bf << "\n";
				std::cout << "bf-shitLen/2 = " << bf-(shitLen/2) << "\n";*/
				noop = true;
				if(2*(bf-(shitLen/2))>=rf-shitLen && bf-(shitLen/2) >= 1)
				{
					//std::cout << "saving border=" << bf << "\n";
					saved += bf-(shitLen/2);
					shitLen += 2;
					borders[bf]--;
					if(borders[bf]==0) borders.erase(bf);
					noop = false;
				}
			}
		}
		
		if(rf-shitLen >= 3)
		{
			saved += rf-shitLen-1;
			shitLen += 4;
			reg[rf]--;
			if(reg[rf]==0) reg.erase(rf);
		}
		else if(rf-shitLen >= 1)
		{
			saved += 1;
			shitLen += 2;
			reg[rf]--;
			if(reg[rf]==0) reg.erase(rf);
		}
		else if(rf-shitLen <= 0)
		{
			break;
		}
		//std::cout << "loop | saved: " << saved << " shitLen: " << shitLen << "\n";

	}
/*
	std::cout << "saved: " << saved << " shitLen: " << shitLen << "\n";
*/
	std::cout << (n-saved) << "\n";
}

int main()
{
	std::ios_base::sync_with_stdio(0);
	
	int t;
	std::cin >> t;

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

	return 0;
}