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
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <cmath>
#include <algorithm>

using namespace std;



int main(){
	long t,n;
	cin >>t;
	while(t--) {
		string s;
		cin >>n >> s;
		//cout << s[0] << s[1] << "sss\n";
		long d[n]={}, counter=0;
		long first=0,last=0;
	
		
		char temp = s[0];
		bool minus =false;
		if(s[0]=='1') minus=true;
		long liczba=1;
		for(long i=1;i<n;i++){
			if(i==n-1) {
				if(temp!=s[i]) {
					if(minus==false) {
						d[counter]=liczba;
						counter++;
						d[counter]=-1;
					}
					else {
						d[counter]=-liczba;
						counter++;
						d[counter]=1;
					}
				counter++;
				}
				else {
					if(minus==false)
						d[counter]=liczba+1;
					else 
						d[counter]=-liczba-1;
					counter++;

				} 
				}
			else {
				if(temp!=s[i]) {
													
					if(minus==false)
						d[counter]=liczba;
					else 
						d[counter]=-liczba;
					counter++;
					liczba=1;
					minus =!minus;
					}
				else
					liczba++;

				temp=s[i];
				

			}
		}
	//	cout <<endl;
	//	for(int i=0;i<counter;i++) { cout <<i << " " <<d[i]<<endl; }
		first=d[0];
		if(first<=0) first=0;
		last=d[counter-1];
		if(last<=0)last=0;
		if(counter-1 <=0) {
			if (minus==false) cout<<"0" << endl;
			else cout <<n <<endl;
		}
		else {
		//	cout <<"first last "<<first << " " << last<<endl;
			sort(d+1,d+counter-1,greater < long >());

			long time=0,out=0,time2=0;
		//	for(int i=0;i<counter;i++) { cout <<i << " " <<d[i]<<endl; }
	//	cout <<endl;
			bool czy=false;
			bool firsti=true;
			for(int i=1;i<counter-1;i++) { 
		//		cout << " i " << i << " d[i]" << d[i]<< " out " <<out << " time " << time <<" first "<<first  << " last " <<last <<endl;
				if(i>1) czy = true;
				if(d[i]-time+first+last<=0 and czy) break;

				if(d[i]-time<=first-time2 and last < first) {
					out+= first-time2;
					first=0;
					i--;
					//last--;
					czy=true;
					time+=2;
					time2++;
				}
				else if(d[i]-time<=last-time2) {
					out+= last-time2;
					last=0;
					i--;
					//first--;
					czy=true;
					time+=2;
					time2++;
				}
				else {
					
					if(first) {
						if(d[i]-1>0)
							out+=d[i]-1;
							time+=4;
							time2+=2;
					}
					else {
			//			cout <<time << " "<<endl;
						if(d[i]-time>0)
							//time+=2;
							if(d[i]-time<=2)
								out+=1;
							else	
								out+=d[i]-time;
							time+=4;
							time2+=2;
					}
				}
				firsti =false;
	//			cout<<endl;
				//cout << " i " << i << " d[i]" << d[i]<< " out " <<out << " time " << time <<" first "<<first  << " last " <<last <<endl;
				
			//	cout <<d[i]<<endl;
			//	time+=2;
				
			}
	
			cout <<  n-out <<endl;
	
	
		}
	}
    return 0;
}