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
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=1e5;
char tab[N+10];
vector<int> av;
vector<int> t;
int f()
{
	int it=0;
	int ans=0;
	for(auto v:t)
	{
		if(v<=2*it)
			break;
		else if(v-2*it<=2)
		{
			ans++;
			it++;
		}
		else
		{
			ans+=(v-2*it-1);
			it+=2;
		}
	}
	return ans;
}
int solve()
{
	int n;
	int d=0;
	bool ok=true;
	av.clear();
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		char c;
		cin>>c;
		if(c=='1')
		{
			if(ok)
			{
				av.push_back(d);
				ok=false;
				d=0;
			}
		}
		else
		{
			ok=true;
			d++;
		}
	}
	av.push_back(d);
	if(av.size()==1)
		return 0;
	t.clear();
	for(int i=1;i<(int)av.size()-1;i++)
		t.push_back(av[i]);
	sort(t.begin(),t.end());
	reverse(t.begin(),t.end());
	vector<int> ans={0};
	ans.push_back(f());
	for(auto &v:t)
		v-=2;
	ans.push_back(f()+max(av[0],av.back()));
	for(auto &v:t)
		v-=2;
	ans.push_back(f()+av[0]+av.back()-1);
	sort(ans.begin(),ans.end());
	return n-ans.back();
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int q;
	cin>>q;
	while(q--)
		cout<<solve()<<"\n";
	return 0;
}