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

#define VERY_LOW_VALUE -1000000

using namespace std;

void printCaseSolution() 
{
	int n;
	cin >> n;
	vector<int> insideGaps(n);
	int maxBorderGap = 0, minBorderGap = 0;
	char c;
	int zeroCount = 0;
	bool frontGapActive = true;
	
	for (int i=0;i<n;++i)
	{
		cin >> c;
		if (c == '0')
		{
			++zeroCount;
		}
		else
		{
			if (frontGapActive)
			{
				maxBorderGap = zeroCount;
				frontGapActive = false;
			}
			else
			{
				if (zeroCount > 0) insideGaps.push_back(zeroCount);
			}
			zeroCount = 0;
		}
	}
	
	if (frontGapActive)
	{
		cout << 0 << endl;
		return;
	}
	
	if (zeroCount > maxBorderGap)
	{
		minBorderGap = maxBorderGap;
		maxBorderGap = zeroCount;
	}
	else
	{
		minBorderGap = zeroCount;
	}
	
	if (insideGaps.size() == 0)
	{
		int x = maxBorderGap + max(minBorderGap - 1, 0);
		cout << n - x << endl;
		return;
	}
	sort(insideGaps.begin(), insideGaps.end(), greater<int>());
	
	int decrement = 0, days = 0;
	int x = 0;
	int i=0;
	for (;i<insideGaps.size();)
	{
		int s = insideGaps[i] - 2*days;
		if (s != 1) --s;
		if (s > maxBorderGap - days)
		{
			if (s <= 0) break;
			x += s;
			days += (s == 1 ? 1 : 2);
			++i;
		}
		else if (maxBorderGap - days > 0)
		{
			x += maxBorderGap - days;
			days += 1;
			maxBorderGap = minBorderGap;
			minBorderGap = VERY_LOW_VALUE;
		}
		else break;
	}
	cout << n - x << endl;
}

int main() 
{
	int t;
	cin >> t;
	
	for (int i=0;i<t;++i)
	{
		printCaseSolution();
	}
	
	return 0;	
}