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
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;

vector<int> v;
string s;

void solve()
{
	int n, pocz = 0, kon = 0, a = 0, b = 0, wyn = 0;
	cin>>n>>s;
	v.clear();
	while(s[a] == '0' && a < s.size())
	{
		pocz++;
		a++;
	}
	for(int i=a; i<s.size(); i++)
	{
		if(s[i] == '1')
		{
			if(b > 0)
			{
				v.pb(b);
				b = 0;
			}
			wyn++;
		}
		else b++;
	}
	kon = b;
	sort(v.begin(), v.end(), greater<int>());
	a = 0;
	for(int i=0; i<v.size(); i++)
	{
		b = v[i];
		if(pocz > 0 && 2 * pocz - 1 >= b)
		{
			if(pocz > a)
			{
				wyn += a;
				a++;
			}
			else wyn += pocz;
			pocz = -1;
		}
		if(kon > 0 && 2 * kon - 1 >= b)
		{
			if(kon > a)
			{
				wyn += a;
				a++;
			}
			else wyn += kon;
			kon = -1;
		}
		if(2 * a + 1 < b)
		{
			wyn += (2 * a + 1);
			a += 2;
		}
		else if(2 * a + 1 == b)
		{
			wyn += 2 * a;
			a++;
		}
		else wyn += b;
	}
	if(pocz > 0)
	{
		if(pocz > a)
		{
			wyn += a;
			a++;
		}
		else wyn += pocz;
	}
	if(kon > 0)
	{
		if(kon > a)
		{
			wyn += a;
			a++;
		}
		else wyn += kon;
	}
	cout<<wyn<<"\n";
	return;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin>>t;
	while(t--) solve();
	return 0;
}