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
#include <iostream>
#include <queue>
using namespace std;


struct Range
{
	int start;
	int length;
	int vaccinated;
	int dead;
};

		int N;
		int steps = 0;
		struct cmpRange
		{
			bool operator() (const Range& l, const Range& r) const
			{
					if (l.start == 0 || l.start + l.length == N)
					{
						if (r.vaccinated == 1)
							return l.length - steps < r.length - steps - r.dead; 
						else if (r.start != 0 && r.start + r.length != N)
							return l.length - steps < r.length - 2 * steps; 
					
					}
					
					if (r.start == 0 || r.start + r.length == N)
					{
						if (l.vaccinated == 1)
							return l.length - steps - l.dead < r.length - steps;
						else if (l.start != 0 && l.start + l.length != N)
							return l.length - 2 * steps < r.length - steps;
					
					}
				
					return l.length < r.length;
			}
		};
	
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int t;
	cin >> t;
	for (int q = 0; q < t; ++q)
	{
		steps = 0;
		cin >> N;
	
		priority_queue<Range, vector<Range>, cmpRange> free_cities;
		string cities;
		cin >> cities;
		int prev_start = 0;
		bool inside = true;
		for (int i = 0; i < N; ++i)
		{
			if (cities[i] == '1' && !inside)
			{
				inside = true;
				free_cities.push({prev_start, i - prev_start, 0, 0});
//				cout << "range from " << prev_start << " to " << i << "\n";
			}
			else if (cities[i] == '0' && inside)
			{
				inside = false;
				prev_start = i;
			}
		}

		if (!inside)
		{	
			free_cities.push({prev_start, N - prev_start, 0, 0});
//			cout << "range from " << prev_start << " to " << N << "\n";
		}

		int vaccinated = 0;
		while (!free_cities.empty())
		{
			Range s = free_cities.top();
			free_cities.pop();

			if ((s.start == 0 || s.start + s.length == N) && s.length - steps <= 0)
			{
				continue;
			}
			else if (s.vaccinated == 1 && s.length - steps - s.dead <= 0)
			{
				continue;
			}
			
			s.vaccinated++;
			if (s.start == 0 || s.start + s.length == N)
			{
				vaccinated += s.length - steps;
			}
			else if (s.vaccinated == 1)
			{
				s.dead = steps;
				free_cities.push(s);
			}
			else if (s.vaccinated == 2)
			{
				vaccinated += s.length - steps - s.dead;
			}
			
			++steps;
		}
		
		cout << N - vaccinated << "\n";
	}
	return 0;
}