#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define MOD 1000000007
#define MOD9 1000000009
#define pi 3.1415926535
#define ms(s, n) memset(s, n, sizeof(s))
#define prec(n) fixed<<setprecision(n)s
#define eps 0.000001
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define bolt ios::sync_with_stdio(0)
#define light cin.tie(0);cout.tie(0)
#define forr(i,p,n) for(ll i=p;i<(ll)n;i++)
typedef long long ll;
using namespace std;
// using namespace __gnu_pbds;

// typedef tree<pair<ll,ll>, null_type, less<pair<ll,ll> >, rb_tree_tag,
//              tree_order_statistics_node_update>
//     pb_ds;

ll mult(ll a,ll b, ll p=MOD){return ((a%p)*(b%p))%p;}
ll multbig(ll a,ll b,ll mod){
    if(a==0 or b==0) return 0;
    if(a==1 or b==1) return (a*b)%mod;
    ll cur=multbig(a,b/2,mod);
    cur=(2*cur)%mod;
    if(b%2) cur=(cur+a)%mod;
    return cur;
}
ll add(ll a, ll b, ll p=MOD){return (a%p + b%p)%p;}
ll fpow(ll n, ll k, ll p = MOD) {ll r = 1; for (; k; k >>= 1LL) {if (k & 1LL) r = mult(r,n,p); n = mult(n,n,p);} return r;}
ll inv(ll a, ll p = MOD) {return fpow(a, p - 2, p);}
ll inv_euclid(ll a, ll m = MOD){ll m0 = m;ll y = 0, x = 1;if (m == 1)return 0;while (a > 1){ll q = a / m;ll t = m;m = a % m, a = t;t = y;y = x - q * y;x = t;}if (x < 0)x += m0;return x;}
void google_cp_output(ll ans, ll &cnt){cout<<"Case #"<<cnt<<": ";cout<<ans<<"\n";cnt+=1;}

//#define MAXN 10003
//ll spff[MAXN];

// void spf(){
//     spff[0]=1;
//     spff[1]=1;
//     for(ll i=2;i<MAXN;i++){
//         if(i%2==0){
//             spff[i]=2;
//         }else spff[i]=i;
//     }
//     for(ll i=3;i*i<=MAXN;i++){
//         if(spff[i]==i){
//             for(ll j=i*i;j<=MAXN;j+=i){
//                 // if(spff[j]==j){
//                     spff[j]=i;
//                 // }
//             }
//         }
//     }
// }

void updateBIT(ll BITree[], ll n, ll index, ll val)
{
    // index in BITree[] is 1 more than the index in arr[]
    index = index + 1;

    // Traverse all ancestors and add 'val'
    while (index <= n)
    {
        // Add 'val' to current node of BI Tree
        BITree[index] += val;

        // Update index to that of parent in update View
        index += index & (-index);
    }
}

// Constructs and returns a Binary Indexed Tree for given
// array of size n.
ll *constructBITree(ll arr[], ll n)
{
    // Create and initialize BITree[] as 0
    ll *BITree = new ll[n+1];
    for (int i=1; i<=n; i++)
        BITree[i] = 0;

    // Store the actual values in BITree[] using update()
    for (int i=0; i<n; i++)
        updateBIT(BITree, n, i, arr[i]);

    // Uncomment below lines to see contents of BITree[]
    //for (int i=1; i<=n; i++)
    //      cout << BITree[i] << " ";

    return BITree;
}

// SERVES THE PURPOSE OF getElement()
// Returns sum of arr[0..index]. This function assumes
// that the array is preprocessed and partial sums of
// array elements are stored in BITree[]
ll getSum(ll BITree[], ll index)
{
    ll sum = 0; // Iniialize result

    // index in BITree[] is 1 more than the index in arr[]
    index = index + 1;

    // Traverse ancestors of BITree[index]
    while (index>0)
    {
        // Add current element of BITree to sum
        sum += BITree[index];

        // Move index to parent node in getSum View
        index -= index & (-index);
    }
    return sum;
}

// Updates such that getElement() gets an increased
// value when queried from l to r.
void update(ll BITree[], ll l, ll r, ll n, ll val)
{
    // Increase value at 'l' by 'val'
    updateBIT(BITree, n, l, val);

    // Decrease value at 'r+1' by 'val'
    updateBIT(BITree, n, r+1, -val);
}

// // Driver program to test above function
// int main()
// {
//     // Add 2 to all the element from [2,4]
//     int l = 2, r = 4, val = 2;
//     update(BITree, l, r, n, val);
//
//     // Find the element at Index 4
//     int index = 4;
//     cout << "Element at index " << index << " is " <<
//          getSum(BITree,index) << "\n";
//
//     // Add 2 to all the element from [0,3]
//     l = 0, r = 3, val = 4;
//     update(BITree, l, r, n, val);
//
//     // Find the element at Index 3
//     index = 3;
//     cout << "Element at index " << index << " is " <<
//          getSum(BITree,index) << "\n" ;
//
//     return 0;
// }

int main(){
    // bolt;
    // light;
    ll t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        string s;
        cin>>s;
        deque<pair<ll,ll> >ss;
        vector<ll>v;
        char p = s[0];
        ll len=0;
        for(ll i=0;i<s.length();i++){
            if(p==s[i]){
                len++;
                continue;
            }else{
                v.pb(len);
                p=s[i];
                len=1;
            }
        }
        v.pb(len);
        // for(auto e:v){
        //     cout<<e<<" ";
        // }
        map<ll,ll>m;
        forr(i,0,v.size()){
            m[i]=v[i];
            if(v[i]>1){
                ss.pb(mp(v[i],i));
            }
        }
        ll ind=0;
        ll ans=0;
        while(ind<int(v.size())){
            ll val = m[ind];
            if(val>1){
                ans+=1;
                pair<ll,ll>p = ss.front();
                if(p.se==ind){
                    ss.pop_front();
                }
                ind++;
            }else{
                if(ss.size()>0){
                    pair<ll,ll> new_obj = ss.front();
                    ss.pop_front();
                    ll new_val = new_obj.fi;
                    m[new_obj.se]=new_val-1;
                    if(new_val-1>1){
                        ss.push_front(mp(new_val-1,new_obj.se));
                    }
                    ind++;
                    ans++;
                }else{
                    ind+=2;
                    ans++;
                }
            }
        }
        cout<<ans<<"\n";
    }
}


#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define MOD 1000000007
#define MOD9 1000000009
#define pi 3.1415926535
#define ms(s, n) memset(s, n, sizeof(s))
#define prec(n) fixed<<setprecision(n)s
#define eps 0.000001
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define bolt ios::sync_with_stdio(0)
#define light cin.tie(0);cout.tie(0)
#define forr(i,p,n) for(ll i=p;i<(ll)n;i++)
typedef long long ll;
using namespace std;
// using namespace __gnu_pbds;

// typedef tree<pair<ll,ll>, null_type, less<pair<ll,ll> >, rb_tree_tag,
//              tree_order_statistics_node_update>
//     pb_ds;

ll mult(ll a,ll b, ll p=MOD){return ((a%p)*(b%p))%p;}
ll multbig(ll a,ll b,ll mod){
    if(a==0 or b==0) return 0;
    if(a==1 or b==1) return (a*b)%mod;
    ll cur=multbig(a,b/2,mod);
    cur=(2*cur)%mod;
    if(b%2) cur=(cur+a)%mod;
    return cur;
}
ll add(ll a, ll b, ll p=MOD){return (a%p + b%p)%p;}
ll fpow(ll n, ll k, ll p = MOD) {ll r = 1; for (; k; k >>= 1LL) {if (k & 1LL) r = mult(r,n,p); n = mult(n,n,p);} return r;}
ll inv(ll a, ll p = MOD) {return fpow(a, p - 2, p);}
ll inv_euclid(ll a, ll m = MOD){ll m0 = m;ll y = 0, x = 1;if (m == 1)return 0;while (a > 1){ll q = a / m;ll t = m;m = a % m, a = t;t = y;y = x - q * y;x = t;}if (x < 0)x += m0;return x;}
void google_cp_output(ll ans, ll &cnt){cout<<"Case #"<<cnt<<": ";cout<<ans<<"\n";cnt+=1;}

//#define MAXN 10003
//ll spff[MAXN];

// void spf(){
//     spff[0]=1;
//     spff[1]=1;
//     for(ll i=2;i<MAXN;i++){
//         if(i%2==0){
//             spff[i]=2;
//         }else spff[i]=i;
//     }
//     for(ll i=3;i*i<=MAXN;i++){
//         if(spff[i]==i){
//             for(ll j=i*i;j<=MAXN;j+=i){
//                 // if(spff[j]==j){
//                     spff[j]=i;
//                 // }
//             }
//         }
//     }
// }

void updateBIT(ll BITree[], ll n, ll index, ll val)
{
    // index in BITree[] is 1 more than the index in arr[]
    index = index + 1;

    // Traverse all ancestors and add 'val'
    while (index <= n)
    {
        // Add 'val' to current node of BI Tree
        BITree[index] += val;

        // Update index to that of parent in update View
        index += index & (-index);
    }
}

// Constructs and returns a Binary Indexed Tree for given
// array of size n.
ll *constructBITree(ll arr[], ll n)
{
    // Create and initialize BITree[] as 0
    ll *BITree = new ll[n+1];
    for (int i=1; i<=n; i++)
        BITree[i] = 0;

    // Store the actual values in BITree[] using update()
    for (int i=0; i<n; i++)
        updateBIT(BITree, n, i, arr[i]);

    // Uncomment below lines to see contents of BITree[]
    //for (int i=1; i<=n; i++)
    //      cout << BITree[i] << " ";

    return BITree;
}

// SERVES THE PURPOSE OF getElement()
// Returns sum of arr[0..index]. This function assumes
// that the array is preprocessed and partial sums of
// array elements are stored in BITree[]
ll getSum(ll BITree[], ll index)
{
    ll sum = 0; // Iniialize result

    // index in BITree[] is 1 more than the index in arr[]
    index = index + 1;

    // Traverse ancestors of BITree[index]
    while (index>0)
    {
        // Add current element of BITree to sum
        sum += BITree[index];

        // Move index to parent node in getSum View
        index -= index & (-index);
    }
    return sum;
}

// Updates such that getElement() gets an increased
// value when queried from l to r.
void update(ll BITree[], ll l, ll r, ll n, ll val)
{
    // Increase value at 'l' by 'val'
    updateBIT(BITree, n, l, val);

    // Decrease value at 'r+1' by 'val'
    updateBIT(BITree, n, r+1, -val);
}

// // Driver program to test above function
// int main()
// {
//     // Add 2 to all the element from [2,4]
//     int l = 2, r = 4, val = 2;
//     update(BITree, l, r, n, val);
//
//     // Find the element at Index 4
//     int index = 4;
//     cout << "Element at index " << index << " is " <<
//          getSum(BITree,index) << "\n";
//
//     // Add 2 to all the element from [0,3]
//     l = 0, r = 3, val = 4;
//     update(BITree, l, r, n, val);
//
//     // Find the element at Index 3
//     index = 3;
//     cout << "Element at index " << index << " is " <<
//          getSum(BITree,index) << "\n" ;
//
//     return 0;
// }

int main(){
    // bolt;
    // light;
    ll t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        string s;
        cin>>s;
        multiset<pair<ll,ll> >ss;
        vector<ll>v;
        char p = s[0];
        ll len=0;
        for(ll i=0;i<s.length();i++){
            if(p==s[i]){
                len++;
                continue;
            }else{
                v.pb(len);
                p=s[i];
                len=1;
            }
        }
        v.pb(len);
        // for(auto e:v){
        //     cout<<e<<" ";
        // }
        map<ll,ll>m;
        forr(i,0,v.size()){
            m[i]=v[i];
            ss.insert(mp(v[i],n-i));
        }
        ll ind=0;
        ll ans=0;
        while(ind<int(v.size())){
            ll val = m[ind];
            if(val>1){
                ans+=1;
                ss.erase(ss.find(mp(val,n-ind)));
                ind++;
            }else{
                pair<ll,ll> new_obj = *ss.rbegin();
                ll new_val = new_obj.fi;
                if(new_val>1){
                    ss.erase(ss.find(new_obj));
                    ss.insert(mp(new_val-1,new_obj.se));
                    m[n-new_obj.se]=new_val-1;
                    ind++;
                    ans++;
                }else{
                    ind+=2;
                    ans++;
                }
            }
        }
        cout<<ans<<"\n";
    }
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
#define maxn (200020)
char s[maxn];
signed main()
{
	int t; cin >> t; while(t--) {
		int n; scanf("%d", &n);
		scanf("%s", s + 1);
		int l = 1, r = n, ans = 0;
		while(l <= r) {
			if(r - l + 1 <= 2) {ans++; break;}
			if(s[l] != s[l+1]) {l++; r--; ans++; continue; }
			char c = s[l];
			while(l <= r && s[l] == c) {
				l++;
			}
			ans++;
		}
		printf("%d\n", ans);
	}

	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define maxn (200020)
string s;
vector<int> v;
int main()
{
	ios::sync_with_stdio(false);
	int t; cin >> t; while(t--) {
		int n; cin >> n;
		s.clear(); cin >> s;
		s.push_back('$');
		v.clear();
		for(int i = 0, cnt = 0, typ, sz = s.size(); i < sz; i++) {
			if(i > 0 && s[i] != s[i-1]) {
				v.push_back(cnt);
				cnt = 0;
			}
			cnt++;
		}
		int ans = 0, left = 0;
		for(int i = 0, sz = v.size(); i < sz; i++) {
			if(v[i] < 2) {
				left = max(left, i + 1);
				while(left < sz && v[left] < 2) {
					left++;
				}
				if(left < sz) {
					v[left]--;
				} else {
					i++;
				}
			}
			ans++;
		}
		cout << ans << endl;
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
#define maxn (200020)
char s[maxn];
signed main()
{
	int t; cin >> t; while(t--) {
		int n; scanf("%d", &n);
		scanf("%s", s + 1);
		int l = 1, r = n, ans = 0;
		while(l <= r) {
			if(r - l + 1 <= 2) {ans++; break;}
			if(s[l] != s[l+1]) r--;
			char c = s[l];
			while(l <= r && s[l] == c) {
				l++;
			}
			ans++;
		}
		printf("%d\n", ans);
	}

	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
#define maxn (200020)
char s[maxn];
int del[maxn];
signed main()
{
	int t; cin >> t; while(t--) {
		int n; scanf("%d", &n);
		scanf("%s", s + 1);
		for(int i = 1; i <= n; i++) del[i] = 0;
		int l = 1, r = n, idx = -1, ans = 0;
		int time = 1;
		while(l <= r) {
			if(del[l]) {l++; continue;}
			// printf("T = %d\n", time);
			if(r - l + 1 <= 2) {ans++; break;}
			if(s[l] != s[l+1]) {
				idx = max(idx, l + 1);
				while(idx + 1 <= n) {
					if(s[idx-1] == s[idx] && s[idx] == s[idx+1]) break;
					idx++;
				}
				if(idx + 1 <= n) {
					// printf("1 del = %d\n", idx);
					del[idx] = 1;
				} else {
					while(r > l && del[r]) r--;
					if(r > l) {
						del[r--] = 1;
						// printf("2 del = %d\n", r);
					}
				}
				l++;
			} else {
				char c = s[l];
				while(l <= r && (del[l] || s[l] == c)) {
					l++;
				}
			}
			// printf("# cur l = %d, r = %d\n", l, r);
			ans++;
			time++;
		}
		printf("%d\n", ans);
	}

	return 0;
}
// 1
// 7
// 1001000


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
#define maxn (200020)
char s[maxn];
int del[maxn];
signed main()
{
	int t; cin >> t; while(t--) {
		int n; scanf("%d", &n);
		scanf("%s", s + 1);
		for(int i = 1; i <= n; i++) del[i] = 0;
		int l = 1, r = n, idx = -1, ans = 0;
		int time = 1;
		while(l <= r) {
			// printf("T = %d\n", time);
			if(r - l + 1 <= 2) {ans++; break;}
			if(s[l] != s[l+1]) {
				idx = max(idx, l + 1);
				while(idx + 1 <= n) {
					if(s[idx-1] == s[idx] && s[idx] == s[idx+1]) break;
					idx++;
				}
				if(idx + 1 <= n) {
					// printf("1 del = %d\n", idx);
					del[idx] = 1;
				} else {
					while(r > l && del[r]) r--;
					if(r > l) {
						del[r--] = 1;
						// printf("2 del = %d\n", r);
					}
				}
				l++;
			} else {
				char c = s[l];
				while(l <= r && (del[l] || s[l] == c)) {
					l++;
				}
			}
			// printf("# cur l = %d, r = %d\n", l, r);
			ans++;
			time++;
		}
		printf("%d\n", ans);
	}

	return 0;
}
// 1
// 7
// 1001000


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define maxn (200020)
struct node {
	char c;
	int cnt;
};

char s[maxn];
vector<node> v;
signed main()
{
	int t; cin >> t; while(t--) {
		int n; scanf("%d", &n);
		scanf("%s", s + 1); s[n+1] = '$';
		v.clear();
		for(int i = 1; i <= n; i++) {
			int j = i;
			while(s[j+1] == s[j]) j++;
			v.push_back((node){s[i], j - i + 1});
			i = j;
		}
		int ans = 0;
		for(int i = 0, j = 0, sz = v.size(); i < sz; i++) {
			ans++;
			if(i == sz - 1) break;
			if(v[i].cnt > 1) ;
			else if(v[i+1].cnt > 2) v[i+1].cnt--;
			else {
				j = max(j, i + 2);
				while(j < sz && v[j].cnt <= 2) j++;
				if(j < sz) v[j].cnt--;
				else {
					v[i+1].cnt--;
					if(!v[i+1].cnt) i++;
				}
			}
		}
		printf("%d\n", ans);
	}

	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
#define maxn (200020)
char s[maxn];
signed main()
{
	int t; cin >> t; while(t--) {
		int n; scanf("%d", &n);
		scanf("%s", s + 1);
		int l = 1, r = n, ans = 0;
		while(l <= r) {
			if(r - l + 1 <= 2) {ans++; break;}
			// if(s[l] != s[l+1]) r--;
			if(s[l] != s[l+1]) l += 2;
			else {
				char c = s[l];
				while(l <= r && s[l] == c) {
					l++;
				}
			}
			ans++;
		}
		printf("%d\n", ans);
	}

	return 0;
}


#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <string>
#include <cassert>
#include <climits>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define abs(x) ((x) > 0 ? (x) : -(x))
#define FOREACH(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e)
typedef long long LL;

int n;
string s;

void solve() {
    int cnt = 1;
    char c = s[0];
    set<pair<int, int> > pos, val;
    int tot = 0;
    for (int i = 1; i < n; i++) {
        if (s[i] == c) {
            cnt++;
        } else {
            pos.insert(make_pair(tot, cnt));
            val.insert(make_pair(cnt, -tot));
            tot++;
            c = s[i];
            cnt = 1;
        }
    }
    pos.insert(make_pair(tot, cnt));
    val.insert(make_pair(cnt, -tot));
    /*
    for (auto it: pos) {
        cout << it.first << " " << it.second << endl;
    }
    */

    int res = 0;
    while (!val.empty()) {
        // op 1
        res++;
        pair<int, int> t = *(val.rbegin());
        //cout << t.second << " " << t.first << " " << res << endl;
        val.erase(t);
        pos.erase(make_pair(-t.second, t.first));

        if (t.first > 1) {
            pos.insert(make_pair(-t.second, t.first - 1));
            val.insert(make_pair(t.first - 1, -t.second));
        }

        // op 2
        if (!pos.empty()) {
            pair<int, int> t2 = *(pos.begin());
            //cout << t.first << " " << t.second << endl;
            pos.erase(t2);
            val.erase(make_pair(t2.second, -t2.first));
        }
    }
    cout << res << endl;
}

int main() {
    int ts;
    cin >> ts;
    while (ts--) {
        cin >> n;
        cin >> s;
        solve();
    }
    return 0;
}


#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <string>
#include <cassert>
#include <climits>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define abs(x) ((x) > 0 ? (x) : -(x))
#define FOREACH(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e)
typedef long long LL;

int n;
string s;

void solve() {
    int cnt = 1;
    char c = s[0];
    set<pair<int, int> > pos, val;
    int tot = 0;
    for (int i = 1; i < n; i++) {
        if (s[i] == c) {
            cnt++;
        } else {
            pos.insert(make_pair(tot, cnt));
            val.insert(make_pair(cnt, tot));
            tot++;
            c = s[i];
            cnt = 1;
        }
    }
    pos.insert(make_pair(tot, cnt));
    val.insert(make_pair(cnt, tot));
    /*
    for (auto it: pos) {
        cout << it.first << " " << it.second << endl;
    }
    */

    int res = 0;
    while (!pos.empty()) {
        // op 1
        res++;
        pair<int, int> t = *(val.rbegin());
        //cout << t.second << " " << t.first << " " << res << endl;
        val.erase(t);
        pos.erase(make_pair(t.second, t.first));
        if (t.first > 1) {
            pos.insert(make_pair(t.second, t.first - 1));
            val.insert(make_pair(t.first - 1, t.second));
        }

        // op 2
        pair<int, int> t2 = *(pos.begin());
        //cout << t.first << " " << t.second << endl;
        pos.erase(t2);
        val.erase(make_pair(t2.second, t2.first));
    }
    cout << res << endl;
}

int main() {
    int ts;
    cin >> ts;
    while (ts--) {
        cin >> n;
        cin >> s;
        solve();
    }
    return 0;
}


#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <string>
#include <cassert>
#include <climits>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define abs(x) ((x) > 0 ? (x) : -(x))
#define FOREACH(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e)
typedef long long LL;

int n;
string s;

void solve() {
    int cnt = 1;
    char c = s[0];
    set<pair<int, int> > pos, val;
    int tot = 0;
    for (int i = 1; i < n; i++) {
        if (s[i] == c) {
            cnt++;
        } else {
            pos.insert(make_pair(tot, cnt));
            val.insert(make_pair(cnt, -tot));
            tot++;
            c = s[i];
            cnt = 1;
        }
    }
    pos.insert(make_pair(tot, cnt));
    val.insert(make_pair(cnt, -tot));
    /*
    for (auto it: pos) {
        cout << it.first << " " << it.second << endl;
    }
    */

    int res = 0;
    while (!val.empty()) {
        // op 1
        res++;
        pair<int, int> t = *(val.rbegin());
        //cout << t.second << " " << t.first << " " << res << endl;
        val.erase(t);
        pos.erase(make_pair(-t.second, t.first));

        if (t.first > 1) {
            pos.insert(make_pair(-t.second, t.first - 1));
            val.insert(make_pair(t.first - 1, t.second));
        }

        // op 2
        if (!pos.empty()) {
            pair<int, int> t2 = *(pos.begin());
            //cout << t.first << " " << t.second << endl;
            pos.erase(t2);
            val.erase(make_pair(t2.second, -t2.first));
        }
    }
    cout << res << endl;
}

int main() {
    int ts;
    cin >> ts;
    while (ts--) {
        cin >> n;
        cin >> s;
        solve();
    }
    return 0;
}


#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <string>
#include <cassert>
#include <climits>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define abs(x) ((x) > 0 ? (x) : -(x))
#define FOREACH(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e)
typedef long long LL;

int n;
string s;

void solve() {
    vector<pair<int, int> > gto;
    vector<int> vec;
    int cnt = 1;
    char c = s[0];
    int idx = 0;
    for (int i = 1; i < n; i++) {
        if (s[i] == c) {
            cnt++;
        } else {
            vec.push_back(cnt);
            if (cnt > 1) {
                gto.push_back(make_pair(idx, cnt));
            }
            idx++;
            c = s[i];
            cnt = 1;
        }
    }
    vec.push_back(cnt);
    if (cnt > 1) {
        gto.push_back(make_pair(idx, cnt));
    }
    idx++;
    /*
    for (int i = 0; i < vec.size(); i++) {
        cout << vec[i] << " ";
    }
    cout << endl;
    for (int i = 0; i < gto.size(); i++) {
        cout << gto[i].first << " " << gto[i].second << endl;
    }
    */
    int res = 0;
    for (int i = 0, j = 0; i < vec.size(); i++) {
        while (j < gto.size() && gto[j].first < i) {
            j++;
        }
        while (j < gto.size() && gto[j].second == 1) {
            j++;
        }
        if (j < gto.size() && gto[j].second > 1) {
            gto[j].second--;
        } else {
            i++;
        }
        res++;
    }
    cout << res << endl;
}

int main() {
    int ts;
    cin >> ts;
    while (ts--) {
        cin >> n;
        cin >> s;
        solve();
    }
    return 0;
}


#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <string>
#include <cassert>
#include <climits>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define abs(x) ((x) > 0 ? (x) : -(x))
#define FOREACH(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e)
typedef long long LL;

int n;
string s;

void solve() {
    int cnt = 1;
    char c = s[0];
    set<pair<int, int> > pos, val;
    int tot = 0;
    for (int i = 1; i < n; i++) {
        if (s[i] == c) {
            cnt++;
        } else {
            pos.insert(make_pair(tot, cnt));
            val.insert(make_pair(cnt, tot));
            tot++;
            c = s[i];
            cnt = 1;
        }
    }
    pos.insert(make_pair(tot, cnt));
    val.insert(make_pair(cnt, tot));
    /*
    for (auto it: pos) {
        cout << it.first << " " << it.second << endl;
    }
    */

    int res = 0;
    while (!val.empty()) {
        // op 1
        res++;
        pair<int, int> t = *(val.rbegin());
        //cout << t.second << " " << t.first << " " << res << endl;
        val.erase(t);
        pos.erase(make_pair(t.second, t.first));

        if (t.first > 1) {
            pos.insert(make_pair(t.second, t.first - 1));
            val.insert(make_pair(t.first - 1, t.second));
        }

        // op 2
        if (!pos.empty()) {
            pair<int, int> t2 = *(pos.begin());
            //cout << t.first << " " << t.second << endl;
            pos.erase(t2);
            val.erase(make_pair(t2.second, t2.first));
        }
    }
    cout << res << endl;
}

int main() {
    int ts;
    cin >> ts;
    while (ts--) {
        cin >> n;
        cin >> s;
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

char buf[200043];

int main()
{
	int t;
	scanf("%d", &t);
	for(int tc = 1; tc <= t; tc++) {
		int n;
		scanf("%d", &n);
		scanf("%s", buf);
		string s = buf;
		queue<int> q;
		int cur = 0;
		for(int i = 0; i < n; i++)
		{
		 	if(i > 0 && s[i] != s[i - 1])
		 		cur++;
		 	if(i > 0 && s[i] == s[i - 1])
		 		q.push(cur);
		}
		int deleted = 0;
		int score = 0;
		for(int i = 0; i < n; i++)
		{
			if(q.empty())
				break;
			q.pop();
			deleted++;
			score++;
			while(!q.empty() && q.front() == i)
			{
				q.pop();
				deleted++;
			}
			deleted++;
			//cerr << deleted << endl;
		}
		score += (n - deleted + 1) / 2;
		printf("%d\n", score);
	}
}


#include<bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); // Remove in problems with online queries!
    int t;
    cin >> t;
    while(t--)
    {
       int n;
       cin >>n;
       string s;
       cin >> s;
       int b=s.size();
       int l=0;
       int r=b-1;
       int ans=0;
       while(l<=r)
       {
              char c=s[l];
              int len=0;
              while(s[l]==c && l<=r)
              {
                     l++;
                     len++;
              }
              if(len==1)
              {
                     ans++;
                     r--;
              }
              else
              {
                     ans++;
              }
       }
       cout << ans << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main() {
	ios::sync_with_stdio(0); cin.tie(0); cout.precision(6); cout << fixed;
	int T;
	cin >> T;
	while(T--) {
		int n; string s;
		cin >> n >> s;
		int ans = 0, need = 0;
		for(int i = 0, j; i < n && (n - i) > need; i = j) {
			need ++;
			ans ++;
			for(j = i; j < n && s[i] == s[j];) j++;
			int num = min(j - i - 1, need);
			need -= num;
		}
		cout << ans << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main() {
	ios::sync_with_stdio(0); cin.tie(0); cout.precision(6); cout << fixed;
	int T;
	cin >> T;
	while(T--) {
		int n; string s;
		cin >> n >> s;
		int ans = 0, need = 0;
		for(int i = 0, j; i < n && (n - i) > need; i = j) {
// 			need ++;
			ans ++;
			int len=0;
			for(j = i; j < n && s[i] == s[j];) j++,len++;
			int num = min(j - i - 1, need);
		       if(len==1)
		       {
		              need++;
		       }
// 			need -= num;
		}
		cout << ans << endl;
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); // Remove in problems with online queries!
    int t;
    cin >> t;
    while(t--)
    {
       int n;
       cin >>n;
       string s;
       cin >> s;
       int b=s.size();
       int l=0;
       int r=b-1;
       int ans=0;
       while(l<=r)
       {
              char c=s[l];
              int len=0;
              while(s[l]==c && l<=r)
              {
                     l++;
                     len++;
              }
              if(len==1 && l<=r)
              {
                     // ans++;
                     r--;
              }
              // else
              // {
              //        ans++;
              // }
              ans++;
       }
       cout << ans << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main (){
	int t, n;
	cin >> t;
	string s;
	while(t--) {
		cin >> n >> s;
		int cnt1 = 1, cnt2 = 0;
		for(int i = 1; i < n; i++) {
			if(s[i] != s[i-1]) cnt1++;
			else {
				cnt2 = min(++cnt2, cnt1);
			}
		}
		cout << (cnt2 + cnt1 + 1)/2 << "\n";
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int n;
char s[N];
void sol() {
    scanf("%d%s", &n, s);
    int x = 0, y = 0;
    char ch = s[0];
    int cnt = 0;
    for (int i = 1; i < n; ++ i) {
        if(s[i] == ch) ++ cnt;
        else {
            if(cnt > 1) ++ y;
            else ++ x;
            cnt = 1;
        }
    }
    if(cnt > 1) ++ y;
    else ++ x;
//    printf("x = %d, y = %d\n", x, y);
    int ans = y + (x + 1) / 2;

    printf("%d\n", ans);
//    puts("---------------");
}
int main() {
    int _; scanf("%d", &_); while(_ --) sol();
    return 0;
}
/**
Rounded up means ceiling function. That is, if a + b is odd, then the resulting number is (a + b + 1) / 2.
**/


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int n;
char s[N];
void sol() {
    scanf("%d%s", &n, s);
    char ch = s[0];
    int cnt = 1;
    int ans = 0, x = 0;
    for (int i = 1; i < n; ++ i) {
        if(s[i] == ch) ++ cnt;
        else {
            if(cnt > 1) {
                if(cnt >= x) {
                    ans += x;
                    cnt -= x;
                    x = 0;
                    if(cnt == 1) ++ x;
                    else if(cnt > 1) ++ ans;
                } else {
                    ans += cnt;
                    x -= cnt;
                }
            } else ++ x;
            cnt = 1;
            ch = s[i];
        }
    }
    if(cnt > 1) {
        if(cnt >= x) {
            ans += x;
            cnt -= x;
            x = 0;
            if(cnt == 1) ++ x;
            else if(cnt > 1) ++ ans;
        } else {
            ans += cnt;
            x -= cnt;
        }
    } else ++ x;
    ans += (x + 1) / 2;
    printf("%d\n", ans);
//    puts("---------------");
}
int main() {
    int _; scanf("%d", &_); while(_ --) sol();
    return 0;
}
/**
7
1011110
10
1110001010
15
111000101011101
**/


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int n;
char s[N];
void sol() {
    scanf("%d%s", &n, s);
    char ch = s[0];
    int cnt = 1;
    int ans = 0, x = 0;
    for (int i = 1; i < n; ++ i) {
        if(s[i] == ch) ++ cnt;
        else {
            if(cnt > 1) {
                ++ ans;
                int y = cnt - 2;
                y = min(x, y);
                ans += y;
                x -= y;
            } else ++ x;
            cnt = 1;
            ch = s[i];
        }
    }
    if(cnt > 1) {
        ++ ans;
        int y = cnt - 1;
        y = min(x, y);
        ans += y;
        x -= y;
        x = 0;
    } else ++ x;
    ans += (x + 1) / 2;
    printf("%d\n", ans);
//    puts("---------------");
}
int main() {
    int _; scanf("%d", &_); while(_ --) sol();
    return 0;
}
/**
7
1011110
10
1110001010
15
111000101011101
**/


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int n;
char s[N];
vector<int> a;
void sol() {
    scanf("%d%s", &n, s);
    a.clear();
    char ch = s[0];
    int cnt = 1;
    for (int i = 1; i < n; ++ i) {
        if(s[i] == ch) ++ cnt;
        else {
            a.push_back(cnt);
            cnt = 1;
            ch = s[i];
        }
    }
    a.push_back(cnt);
    int ans = 0;
    int len = a.size();
    int r = 0;
    for (int i = 0; i < len; ++ i) {
        if(a[i] == 1) {
            for (int j = max(r, i); j < len; ++ j) if(a[j] > 1) {
                r = j; -- a[j]; ++ ans; a[i] = 0; break;
            }
        } else ++ ans;
    }
    int x = 0;
    for (int i = 0; i < len; ++ i) if(a[i] == 1) ++ x;

    ans += (x + 1) / 2;
    printf("%d\n", ans);
//    puts("---------------");
}
int main() {
    int _; scanf("%d", &_); while(_ --) sol();
    return 0;
}
/**
10
7
1011110
10
1110001010
15
111000101011101
**/


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int n;
char s[N];
void sol() {
    scanf("%d%s", &n, s);
    char ch = s[0];
    int cnt = 0;
    int ans = 0, x = 0;
    for (int i = 1; i < n; ++ i) {
        if(s[i] == ch) ++ cnt;
        else {
            if(cnt > 1) {
                ++ ans;
                int y = cnt - 2;
                y = min(x, y);
                ans += y;
                x -= y;
            } else ++ x;
            cnt = 1;
            ch = s[i];
        }
    }
    if(cnt > 1) {
        ++ ans;
        int y = cnt - 1;
        y = min(x, y);
        ans += y;
        x -= y;
    } else ++ x;
    ans += (x + 1) / 2;
    printf("%d\n", ans);
//    puts("---------------");
}
int main() {
    int _; scanf("%d", &_); while(_ --) sol();
    return 0;
}
/**
7
1011110
10
1110001010
15
111000101011101
**/


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int n;
char s[N];
void sol() {
    scanf("%d%s", &n, s);
    char ch = s[0];
    int cnt = 1;
    int ans = 0, x = 0;
    for (int i = 1; i < n; ++ i) {
        if(s[i] == ch) ++ cnt;
        else {
            if(cnt > 1) {
                if(cnt >= x) {
                    ans += x;
                    cnt -= x;
                    if(cnt == 1) ++ x;
                    else if(cnt > 1) ++ ans;
                    x = 0;
                } else {
                    ans += cnt;
                    x -= cnt;
                }
            } else ++ x;
            cnt = 1;
            ch = s[i];
        }
    }
    if(cnt > 1) {
        if(cnt >= x) {
            ans += x;
            cnt -= x;
            if(cnt == 1) ++ x;
            else if(cnt > 1) ++ ans;
        } else {
            ans += cnt;
            x -= cnt;
        }
    } else ++ x;
    ans += (x + 1) / 2;
    printf("%d\n", ans);
//    puts("---------------");
}
int main() {
    int _; scanf("%d", &_); while(_ --) sol();
    return 0;
}
/**
7
1011110
10
1110001010
15
111000101011101
**/


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int n;
char s[N];
vector<int> a;
void sol() {
    scanf("%d%s", &n, s);
    a.clear();
    char ch = s[0];
    int cnt = 1;
    for (int i = 1; i < n; ++ i) {
        if(s[i] == ch) ++ cnt;
        else {
            a.push_back(cnt);
            cnt = 1;
            ch = s[i];
        }
    }
    a.push_back(cnt);
    int ans = 0;
    int len = a.size();
    int r = 0;
    for (int i = 0; i < len; ++ i) {
        if(a[i] == 1) {
            for (int j = max(r, i); j < len; ++ j) if(a[j] > 1) {
                r = i; -- a[j]; ++ ans; a[i] = 0; break;
            }
        } else ++ ans;
    }
    int x = 0;
    for (int i = 0; i < len; ++ i) if(a[i] == 1) ++ x;

    ans += (x + 1) / 2;
    printf("%d\n", ans);
//    puts("---------------");
}
int main() {
    int _; scanf("%d", &_); while(_ --) sol();
    return 0;
}
/**
10
7
1011110
10
1110001010
15
111000101011101
**/


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int n;
char s[N];
void sol() {
    scanf("%d%s", &n, s);
    char ch = s[0];
    int cnt = 1;
    int ans = 0, x = 0;
    for (int i = 1; i < n; ++ i) {
        if(s[i] == ch) ++ cnt;
        else {
            if(cnt > 1) {
                if(cnt >= x) {
                    ans += x;
                    cnt -= x;
                    if(cnt == 1) ++ x;
                    else if(cnt > 1) ++ ans;
                    x = 0;
                } else {
                    ans += cnt;
                    x -= cnt;
                }
            } else ++ x;
            cnt = 1;
            ch = s[i];
        }
    }
    if(cnt > 1) {
        if(cnt >= x) {
            ans += x;
            cnt -= x;
            if(cnt == 1) ++ x;
            else if(cnt > 1) ++ ans;
        } else {
            ans += cnt;
            x -= cnt;
        }
        x = 0;
    } else ++ x;
    ans += (x + 1) / 2;
    printf("%d\n", ans);
//    puts("---------------");
}
int main() {
    int _; scanf("%d", &_); while(_ --) sol();
    return 0;
}
/**
7
1011110
10
1110001010
15
111000101011101
**/


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int n;
char s[N];
void sol() {
    scanf("%d%s", &n, s);
    int cnt = 1;
    char ch = s[0];
    int one = 0;
    int ans = 0;
    for (int i = 1; i < n; ++ i) {
        if(s[i] == ch) ++ cnt;
        else {
            if(cnt == 1) ++ one;
            else {
                while(cnt > 1 && one > 0) ++ ans, -- cnt, -- one;
                if(cnt > 1) ++ ans;
                else ++ one;
            }
            cnt = 1;
            ch = s[i];
        }
    }
    if(cnt == 1) ++ one;
    else {
        while(cnt > 1 && one > 0) ++ ans, -- cnt, -- one;
        if(cnt > 1) ++ ans;
        else ++ one;
    }
    ans += (one + 1) / 2;
    printf("%d\n", ans);
}
int main() {
    int _; scanf("%d", &_); while(_ --) sol();
    return 0;
}
/**
10
7
1011110
10
1110001010
15
111000101011101
**/


#include <bits/stdc++.h>
#define mod 1000000007
#define mxn 100007
#define pb push_back

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int>par;

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

    int t;
    cin >>t;
    while(t--){

    	int n;
    	string s;
    	cin >> n >> s;
    	int pos = 0;
    	int dif = 0;
    	int cont = 0;
    	deque<int>dq;
    	set<int>quitaos;
    	vector<par>v;
    	bool patra = 0;

    	while(pos <= n-1){

    		cont ++;
    		dif = max(dif, pos);
    		while(dif <= n - 1 && !patra){
    			if(!dq.empty() && s[dq.back()] == s[dif] && dif < n-1 && s[dif+1] == s[dif])
    			{
    				quitaos.insert(dif);
    				dif++;
    				break;
    			}
    			dq.push_back(dif);

    			if(dif==n-1 ){
    				patra = 1;

    				int ant = -1;
    				while(!dq.empty()){

    					if(ant != -1){
    						if(s[dq.back()] == s[ant])
    							v.pb(par(dq.back(), ant)), ant = -1;
    						else ant = dq.back();
    					} else ant = dq.back();
    					dq.pop_back();
    				}
    				break;
    			}
    			dif++;
    		}

    		if(v.size() > 0 && patra && v[v.size()-1].second > pos)
    				quitaos.insert(v[v.size()-1].second), v.pop_back();
    		else if (patra) pos += 1;

    		char ant = 'a';
    		bool r = false;
    		while(pos < n){

    			r = (quitaos.find(pos) != quitaos.end());
    			if(s[pos] != s[pos-1] && ant != 'a' && r==false)
    				break;
    			if(r == false)
    				ant = s[pos];
    			pos++;
    		}

    		while(!dq.empty() && dq.front() < pos)
    			dq.pop_front();

    	}
    	cout << cont << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define mod 1000000007
#define mxn 100007
#define pb push_back

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int>par;

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

    int t;
    cin >>t;
    while(t--){

    	int n;
    	string s;
    	cin >> n >> s;
    	int pos = 0;
    	int dif = 0;
    	int cont = 0;
    	deque<char>dq;
    	queue<int>poses;
    	while(pos <= n-1){

    		cont ++;
    		dif = max(dif, pos);
    		while(dif <= n - 1){
    			if(!dq.empty() && dq.back() == s[dif] && dif < n-1 && s[dif+1] == s[dif])
    			{
    				poses.push(dif);
    				dif++;

    				int t = dif;
    				break;
    			}
    			dq.push_back(s[dif]);
    			if(dif==n-1){
    				dif--;
    				n-=1;
    				break;
    			}
    			dif++;
    		}
    		bool first = true;
    		char ant;
    		while(!dq.empty() && (first || dq.front() == ant)){
    			ant= dq.front();
    			dq.pop_front();
    			first = false;
    			pos++;
    		}
    		while(!poses.empty() && poses.front() <= pos){
    			poses.pop();
    			pos++;
    		}
    		if(pos == dif -1)
    			pos++;
    		while(s[pos] == s[pos-1])
    			pos++;
    	}
    	cout << cont << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define mod 1000000007
#define mxn 100007
#define pb push_back

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int>par;

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

    int t;
    cin >>t;
    while(t--){

    	int n;
    	string s;
    	cin >> n >> s;
    	int pos =0, dif = 0,cont = 0;
    	deque<int>dq;
    	set<int>quitaos;
    	int kk = 1;

    	while(pos <= n-1){

    		cont += 1;
    		dif = max(dif, pos);

    		if(dif < n)
				while(dif <= n - 1){
					if(!dq.empty() && s[dq.back()] == s[dif])
					{
						dq.pop_back();
						dq.push_back(dif);
						quitaos.insert(dq.back());
						dif++;
						if(dif==n){
							dif = n + 1;
						}
						break;
					}
					dq.push_back(dif);

					if(dif==n-1){
						quitaos.insert(dif);
						dq.pop_back();
						dif = n + 1;
						break;
					}
					dif++;
				}
    		else kk = 2;

    		for(int i = 1; i <= kk; i++){
				char ant = 'a';
				bool r = false;

				while(pos < n){
					r = (quitaos.find(pos) != quitaos.end());
					if(s[pos] != s[pos-1] && ant != 'a' && r==false)
						break;
					if(r == false)
						ant = s[pos];
					pos++;
				}
				while(!dq.empty() && dq.front() < pos)
					dq.pop_front();
				}

    	}
    	cout << cont << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define mod 1000000007
#define mxn 100007
#define pb push_back

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int>par;

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

    int t;
    cin >>t;
    while(t--){

    	int n;
    	string s;
    	cin >> n >> s;
    	int pos = 0;
    	int dif = 0;
    	int cont = 0;
    	deque<int>dq;
    	set<int>quitaos;
    	bool patra = 0;
    	vector<par>v;
    	while(pos <= n-1){

    		cont ++;
    		dif = max(dif, pos);
    		while(dif <= n - 1 && !patra){
    			if(!dq.empty() && s[dq.back()] == s[dif] && dif < n-1 && s[dif+1] == s[dif])
    			{
    				quitaos.insert(dif);
    				dif++;
    				break;
    			}
    			dq.push_back(dif);

    			if(dif==n-1 ){
    				patra = 1;

    				int ant = -1;
    				while(!dq.empty()){

    					if(ant != -1){
    						if(s[dq.back()] == s[ant])
    							v.pb(par(dq.back(), ant)), ant = -1;
    						else ant = dq.back();
    					} else ant = dq.back();
    					dq.pop_back();
    				}
    				break;
    			}
    			dif++;
    		}

    		if(v.size() > 0 && patra && dif > pos && v[v.size()-1].second > pos)
    				quitaos.insert(v[v.size()-1].second), v.pop_back();
    		else if (patra) pos += 1;

    		char ant = 'a';
    		bool r = false;
    		while(pos < n){

    			r = (quitaos.find(pos) != quitaos.end());
    			if(s[pos] != s[pos-1] && ant != 'a' && r==false)
    				break;
    			if(r == false)
    				ant = s[pos];
    			pos++;
    		}

    		while(!dq.empty() && dq.front() < pos)
    			dq.pop_front();

    	}
    	cout << cont << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define mod 1000000007
#define mxn 100007
#define pb push_back

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int>par;

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

    int t;
    cin >>t;
    while(t--){

    	int n;
    	string s;
    	cin >> n >> s;
    	int pos = 0;
    	int dif = 0;
    	int cont = 0;
    	deque<int>dq;
    	set<int>quitaos;
    	int kk = 1;
    	while(pos <= n-1){

    		cont += 1;
    		dif = max(dif, pos);
    		if(dif < n)
				while(dif <= n - 1){
					if(!dq.empty() && s[dq.back()] == s[dif])
					{
						dq.pop_back();
						quitaos.insert(dif);
						dif++;
						if(dif==n){
							dif = n + 1;
						}
						break;
					}
					dq.push_back(dif);

					if(dif==n-1){
						dif = n + 1;
						kk = 2;
						break;
					}
					dif++;
				}
    		else kk = 2;

    		for(int i = 1; i <= kk; i++){
    		char ant = 'a';
    		bool r = false;

    		while(pos < n){

    			r = (quitaos.find(pos) != quitaos.end());
    			if(s[pos] != s[pos-1] && ant != 'a' && r==false)
    				break;
    			if(r == false)
    				ant = s[pos];
    			pos++;
    		}
    		while(!dq.empty() && dq.front() < pos)
    			dq.pop_front();
    		}

    	}
    	cout << cont << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define mod 1000000007
#define mxn 100007
#define pb push_back

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int>par;

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

    int t;
    cin >>t;
    while(t--){

    	int n;
    	string s;
    	cin >> n >> s;
    	int pos = 0;
    	int dif = 0;
    	int cont = 0;
    	deque<char>dq;
    	while(pos <= n-1){

    		cont ++;
    		dif = max(dif, pos);
    		while(dif <= n - 1){
    			if(!dq.empty() && dq.front() == s[dif] && dif < n-1 && s[dif+1] == s[dif])
    			{
    				dif++;
    				pos++;
    				int t = dif;
    				break;
    			}
    			dq.push_back(s[dif]);
    			if(dif==n-1){
    				dif--;
    				n-=1;
    				break;
    			}
    			dif++;
    		}
    		bool first = true;
    		char ant;
    		while(!dq.empty() && (first || dq.front() == ant)){
    			ant= dq.front();
    			dq.pop_front();
    			first = false;
    			pos++;
    		}
    		if(pos == dif -1)
    			pos++;
    		while(s[pos] == s[pos-1])
    			pos++;
    	}
    	cout << cont << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define mod 1000000007
#define mxn 100007
#define pb push_back

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int>par;

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

    int t;
    cin >>t;
    while(t--){

    	int n;
    	string s;
    	cin >> n >> s;
    	int pos = 0;
    	int dif = 0;
    	int cont = 0;
    	deque<char>dq;
    	queue<int>poses;
    	set<int>quitaos;
    	while(pos <= n-1){

    		cont ++;
    		dif = max(dif, pos);
    		while(dif <= n - 1){
    			if(!dq.empty() && dq.back() == s[dif] && dif < n-1 && s[dif+1] == s[dif])
    			{
    				poses.push(dif);
    				quitaos.insert(dif);
    				dif++;

    				int t = dif;
    				break;
    			}
    			dq.push_back(s[dif]);
    			if(dif==n-1){
    				dif--;
    				n-=1;
    				while(quitaos.find(dif) != quitaos.end())
    					dif--, n-=1;

    				break;
    			}
    			dif++;
    		}
    		bool first = true;
    		char ant;
    		while(!dq.empty() && (first || dq.front() == ant)){
    			ant= dq.front();
    			dq.pop_front();
    			first = false;
    			if((!poses.empty() && poses.front() == pos))
    			    poses.pop();
    			pos++;
    		}
    		while(!poses.empty() && poses.front() < pos){
    			poses.pop();
    		}
    		while(s[pos] == s[pos-1] || (!poses.empty() && poses.front() == pos)){
    			if((!poses.empty() && poses.front() == pos))
    				poses.pop();

    			pos++;
    		}

    	}
    	cout << cont << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define mod 1000000007
#define mxn 100007
#define pb push_back

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int>par;

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

    int t;
    cin >>t;
    while(t--){

    	int n;
    	string s;
    	cin >> n >> s;
    	int pos = 0;
    	int dif = 0;
    	int cont = 0;
    	deque<int>dq;
    	set<int>quitaos;
    	int kk = 1;
    	while(pos <= n-1){

    		cont += 1;
    		dif = max(dif, pos);
    		while(dif <= n - 1){
    			if(!dq.empty() && s[dq.back()] == s[dif])
    			{
    				dq.pop_back();
    				quitaos.insert(dif);
    				dif++;
    				if(dif==n){
						dif = n + 1;
						kk = 2;
					}
    				break;
    			}
    			dq.push_back(dif);

    			if(dif==n-1){
    				dif = n + 1;
    				kk = 2;
    				break;
    			}
    			dif++;
    		}

    		for(int i = 1; i <= kk; i++){
    		char ant = 'a';
    		bool r = false;

    		while(pos < n){

    			r = (quitaos.find(pos) != quitaos.end());
    			if(s[pos] != s[pos-1] && ant != 'a' && r==false)
    				break;
    			if(r == false)
    				ant = s[pos];
    			pos++;
    		}
    		while(!dq.empty() && dq.front() < pos)
    			dq.pop_front();
    		}

    	}
    	cout << cont << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define mod 1000000007
#define mxn 100007
#define pb push_back

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int>par;

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

    int t;
    cin >>t;
    while(t--){

    	int n;
    	string s;
    	cin >> n >> s;
    	int pos = 0;
    	int dif = 0;
    	int cont = 0;
    	deque<char>dq;
    	set<int>quitaos;
    	while(pos <= n-1){

    		cont ++;
    		dif = max(dif, pos);
    		while(dif <= n - 1){
    			if(!dq.empty() && dq.back() == s[dif] && dif < n-1 && s[dif+1] == s[dif])
    			{
    				quitaos.insert(dif);
    				dif++;
    				break;
    			}

    			dq.push_back(s[dif]);

    			if(dif==n-1){
    				while(quitaos.find(dif) != quitaos.end() && dif >= pos)
    					dif--, n-=1;
    				quitaos.insert(dif);
    				break;
    			}
    			dif++;
    		}
    		char ant = 'a';
    		bool r = false;
    		while(pos < n){

    			r = (quitaos.find(pos) != quitaos.end());
    			if(s[pos] != s[pos-1] && ant != 'a' && r==false)
    				break;
    			if(r == false)
    				ant = s[pos];
    			pos++;
    		}

    	}
    	cout << cont << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define mod 1000000007
#define mxn 100007
#define pb push_back

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int>par;

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

    int t;
    cin >>t;
    while(t--){

    	int n;
    	string s;
    	cin >> n >> s;
    	int pos = 0;
    	int dif = 0;
    	int cont = 0;
    	deque<int>dq;
    	set<int>quitaos;
    	int kk = 1;
    	while(pos <= n-1){

    		cont += 1;
    		dif = max(dif, pos);
    		while(dif <= n - 1){
    			if(!dq.empty() && s[dq.back()] == s[dif])
    			{
    				dq.pop_back();
    				quitaos.insert(dif);
    				dif++;
    				break;
    			}
    			dq.push_back(dif);

    			if(dif==n-1){
    				dif = n + 1;
    				kk = 2;
    				break;
    			}
    			dif++;
    		}

    		for(int i = 1; i <= kk; i++){
    		char ant = 'a';
    		bool r = false;

    		while(pos < n){

    			r = (quitaos.find(pos) != quitaos.end());
    			if(s[pos] != s[pos-1] && ant != 'a' && r==false)
    				break;
    			if(r == false)
    				ant = s[pos];
    			pos++;
    		}
    		while(!dq.empty() && dq.front() < pos)
    			dq.pop_front();
    		}

    	}
    	cout << cont << '\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define  ll long long
const ll inf=1e18+7;
const int maxn=2e5+7;
string s1;
int val[maxn];
struct node{
    int index;
    int val;
    bool operator<(const node& rhs)const{
        return val<rhs.val;
    }
};
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)val[i]=0;
        cin>>s1;
        int cnt=0;
        int temp=0;
        for(int i=1;i<n;i++){
            if(s1[i]!=s1[i-1]){
                node node1;
                node1.index=++cnt;
                node1.val=i-temp;
                val[node1.index]=node1.val;
                temp=i;
            }
        }
        node node1;
        node1.index=++cnt;
        node1.val=n-temp;
        val[node1.index]=node1.val;
        //cout<<-1<<endl;
        int res=0;
        node u;
        u.index=cnt+1;
        u.val=inf;
        for(int i=2;i<=cnt;i++){
            if(val[i]>1){
                u.index=i;
                u.val=val[i];
            }
        }
        for(int i=1;i<=cnt;i++){
            if(i==cnt){
                res++;
                break;
            }
            if(val[i]==1) {
                if(u.index<=i||u.val<=1) {
                    while (u.index <= cnt) {
                        ++u.index;
                        if (u.index == cnt + 1)break;
                        u.val = val[u.index];
                        if (u.index > i && u.val >= 2)break;
                    }
                }
                    if(u.index==cnt+1){
                        res += (cnt - i + 2) / 2;
                        break;
                    }
                    else{
                        res++;
                        u.val--;
                        val[u.index] = u.val;
                    }
            }
            else {
                res++;
            }
     //       cout<<i<<" "<<u.index<<" "<<res<<endl;
        }
        cout<<res<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define  ll long long
const ll inf=1e18+7;
const int maxn=2e5+7;
string s1;
int val[maxn];
struct node{
    int index;
    int val;
    bool operator<(const node& rhs)const{
        return val<rhs.val;
    }
};
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)val[i]=0;
        cin>>s1;
        int cnt=0;
        int temp=0;
        for(int i=1;i<n;i++){
            if(s1[i]!=s1[i-1]){
                node node1;
                node1.index=++cnt;
                node1.val=i-temp;
                val[node1.index]=node1.val;
                temp=i;
            }
        }
        node node1;
        node1.index=++cnt;
        node1.val=n-temp;
        val[node1.index]=node1.val;
        //cout<<-1<<endl;
        int res=0;
        node u;
        u.index=cnt+1;
        u.val=inf;
        for(int i=2;i<=cnt;i++){
            if(val[i]>2){
                u.index=i;
                u.val=val[i];
            }
        }
        for(int i=1;i<=cnt;i++){
            if(i==cnt){
                res++;
                break;
            }
            if(val[i]==1) {
                if(u.index<=i||u.val<=2) {
                    while (u.index <= cnt) {
                        ++u.index;
                        if (u.index == cnt + 1)break;
                        u.val = val[u.index];
                        if (u.index > i && u.val >= 3)break;
                    }
                }
                    if(u.index==cnt+1){
                        int cnt1=0;
                        int cnt2=0;
                        for(int j=i;j<=cnt;j++){
                            if(val[j]==1)cnt1++;
                            if(val[j]==2)cnt2++;
                        }
                        res+=(cnt1+cnt2*2+1)/2;
                        break;
                    }
                    else{
                        res++;
                        u.val--;
                        val[u.index] = u.val;
                    }
            }
            else {
                res++;
            }
     //       cout<<i<<" "<<u.index<<" "<<res<<endl;
        }
        cout<<res<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define  ll long long
const ll inf=1e18+7;
const int maxn=2e5+7;
string s1;
int val[maxn];
struct node{
    int index;
    int val;
    bool operator<(const node& rhs)const{
        return val<rhs.val;
    }
};
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)val[i]=0;
        cin>>s1;
        int cnt=0;
        int temp=0;
        for(int i=1;i<n;i++){
            if(s1[i]!=s1[i-1]){
                node node1;
                node1.index=++cnt;
                node1.val=i-temp;
                val[node1.index]=node1.val;
                temp=i;
            }
        }
        node node1;
        node1.index=++cnt;
        node1.val=n-temp;
        val[node1.index]=node1.val;
        //cout<<-1<<endl;
        int res=0;
        node u;
        u.index=cnt+1;
        u.val=inf;
        for(int i=2;i<=cnt;i++){
            if(val[i]>1){
                u.index=i;
                u.val=val[i];
            }
        }
        for(int i=1;i<=cnt;i++){
            cout<<i<<" "<<u.index<<endl;
            if(i==cnt){
                res++;
                break;
            }
            if(val[i]==1) {
                if(u.index<=i||u.val<=1) {
                    while (u.index <= cnt) {
                        ++u.index;
                        u.val = val[u.index];
                        if (u.index <= cnt && u.index > i && u.val >= 2)break;
                    }
                }
                    if(u.index==cnt+1){
                        res += (cnt - i + 2) / 2;
                        break;
                    }
                    else{
                        res++;
                        u.val--;
                        val[u.index] = u.val;
                    }
            }
            else {
                res++;
            }
        }
        cout<<res<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define  ll long long
const ll inf=1e18+7;
const int maxn=2e5+7;
string s1;
int val[maxn];
struct node{
    int index;
    int val;
    bool operator<(const node& rhs)const{
        return val<rhs.val;
    }
};
priority_queue<node> pq1;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        while(!pq1.empty())pq1.pop();
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)val[i]=0;
        cin>>s1;
        int cnt=0;
        int temp=0;
        for(int i=1;i<n;i++){
            if(s1[i]!=s1[i-1]){
                node node1;
                node1.index=++cnt;
                node1.val=i-temp;
                pq1.push(node1);
                val[node1.index]=node1.val;
                temp=i;
            }
        }
        node node1;
        node1.index=++cnt;
        node1.val=n-temp;
        pq1.push(node1);
        val[node1.index]=node1.val;
        //cout<<-1<<endl;
        int res=0;
        for(int i=1;i<=cnt;i++){
            if(i==cnt){
                res++;
                break;
            }
            if(val[i]==1){
                while(pq1.top().index<=i)pq1.pop();
                node u=pq1.top();
                pq1.pop();
                if(u.val>2){
                    res++;
                    u.val--;
                    val[u.index]=u.val;
                    pq1.push(u);
                }
                else{
                        int cnt1=0;
                        int cnt2=0;
                        for(int j=i;j<=cnt;j++){
                            if(val[j]==1)cnt1++;
                            if(val[j]==2)cnt2++;
                        }
                        res+=(cnt1+cnt2*2+1)/2;
                    break;
                }
            }
            else{
                res++;
            }
        }
        cout<<res<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define  ll long long
const ll inf=1e18+7;
const int maxn=2e5+7;
string s1;
int val[maxn];
struct node{
    int index;
    int val;
    bool operator<(const node& rhs)const{
        return val<rhs.val;
    }
};
priority_queue<node> pq1;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        while(!pq1.empty())pq1.pop();
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)val[i]=0;
        cin>>s1;
        int cnt=0;
        int temp=0;
        for(int i=1;i<n;i++){
            if(s1[i]!=s1[i-1]){
                node node1;
                node1.index=++cnt;
                node1.val=i-temp;
                pq1.push(node1);
                val[node1.index]=node1.val;
                temp=i;
            }
        }
        node node1;
        node1.index=++cnt;
        node1.val=n-temp;
        pq1.push(node1);
        val[node1.index]=node1.val;
        //cout<<-1<<endl;
        int res=0;
        for(int i=1;i<=cnt;i++){
            if(i==cnt){
                res++;
                break;
            }
            if(val[i]==1){
                while(pq1.top().index<=i)pq1.pop();
                node u=pq1.top();
                pq1.pop();
                if(u.val>1){
                    res++;
                    u.val--;
                    val[u.index]=u.val;
                    pq1.push(u);
                }
                else{
                    res+=(cnt-i+2)/2;
                    break;
                }
            }
            else{
                res++;
            }
        }
        cout<<res<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define  ll long long
const ll inf=1e18+7;
const int maxn=2e5+7;
string s1;
int val[maxn];
struct node{
    int index;
    int val;
    bool operator<(const node& rhs)const{
        return val<rhs.val;
    }
};
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)val[i]=0;
        cin>>s1;
        int cnt=0;
        int temp=0;
        for(int i=1;i<n;i++){
            if(s1[i]!=s1[i-1]){
                node node1;
                node1.index=++cnt;
                node1.val=i-temp;
                val[node1.index]=node1.val;
                temp=i;
            }
        }
        node node1;
        node1.index=++cnt;
        node1.val=n-temp;
        val[node1.index]=node1.val;
        //cout<<-1<<endl;
        int res=0;
        node u;
        u.index=cnt+1;
        u.val=inf;
        for(int i=2;i<=cnt;i++){
            if(val[i]>1){
                u.index=i;
                u.val=val[i];
            }
        }
        for(int i=1;i<=cnt;i++){
          //  cout<<i<<" "<<u.index<<endl;
            if(i==cnt){
                res++;
                break;
            }
            if(val[i]==1) {
                if(u.index<=i||u.val<=1) {
                    while (u.index <= cnt) {
                        ++u.index;
                        u.val = val[u.index];
                        if (u.index <= cnt && u.index > i && u.val >= 2)break;
                    }
                }
                    if(u.index==cnt+1){
                        res += (cnt - i + 2) / 2;
                        break;
                    }
                    else{
                        res++;
                        u.val--;
                        val[u.index] = u.val;
                    }
            }
            else {
                res++;
            }
        }
        cout<<res<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define  ll long long
const ll inf=1e18+7;
const int maxn=2e5+7;
string s1;
int val[maxn];
struct node{
    int index;
    int val;
    bool operator<(const node& rhs)const{
        return val<rhs.val;
    }
};
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)val[i]=0;
        cin>>s1;
        int cnt=0;
        int temp=0;
        for(int i=1;i<n;i++){
            if(s1[i]!=s1[i-1]){
                node node1;
                node1.index=++cnt;
                node1.val=i-temp;
                val[node1.index]=node1.val;
                temp=i;
            }
        }
        node node1;
        node1.index=++cnt;
        node1.val=n-temp;
        val[node1.index]=node1.val;
        //cout<<-1<<endl;
        int res=0;
        node u;
        u.index=cnt+1;
        u.val=inf;
        for(int i=2;i<=cnt;i++){
            if(val[i]>2){
                u.index=i;
                u.val=val[i];
            }
        }
        for(int i=1;i<=cnt;i++){
            if(i==cnt){
                res++;
                break;
            }
            if(val[i]==1) {
                if(u.index<=i||u.val<=1) {
                    while (u.index <= cnt) {
                        ++u.index;
                        if (u.index == cnt + 1)break;
                        u.val = val[u.index];
                        if (u.index > i && u.val >= 3)break;
                    }
                }
                    if(u.index==cnt+1){
                        int cnt1=0;
                        int cnt2=0;
                        for(int j=i;j<=cnt;j++){
                            if(val[j]==1)cnt1++;
                            if(val[j]==2)cnt2++;
                        }
                        res+=(cnt1+cnt2*2+1)/2;
                        break;
                    }
                    else{
                        res++;
                        u.val--;
                        val[u.index] = u.val;
                    }
            }
            else {
                res++;
            }
     //       cout<<i<<" "<<u.index<<" "<<res<<endl;
        }
        cout<<res<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define  ll long long
const ll inf=1e18+7;
const int maxn=2e5+7;
string s1;
int val[maxn];
struct node{
    int index;
    int val;
    bool operator<(const node& rhs)const{
        return val<rhs.val;
    }
};
int main(){
 //   freopen("in.txt","r",stdin);
  //  freopen("1.txt","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)val[i]=0;
        cin>>s1;
        int cnt=0;
        int temp=0;
        for(int i=1;i<n;i++){
            if(s1[i]!=s1[i-1]){
                node node1;
                node1.index=++cnt;
                node1.val=i-temp;
                val[node1.index]=node1.val;
                temp=i;
            }
        }
        node node1;
        node1.index=++cnt;
        node1.val=n-temp;
        val[node1.index]=node1.val;
        //cout<<-1<<endl;
        int res=0;
        node u;
        u.index=cnt+1;
        u.val=inf;
        for(int i=2;i<=cnt;i++){
            if(val[i]>1){
                u.index=i;
                u.val=val[i];
                break;
            }
        }
        for(int i=1;i<=cnt;i++){
         //   cout<<i<<" "<<u.index<<endl;
            if(i==cnt){
                res++;
                break;
            }
            if(val[i]==1) {
                if(u.index<=i||u.val<=1) {
                    while (u.index <= cnt) {
                        ++u.index;
                        u.val = val[u.index];
                        if (u.index <= cnt && u.index > i && u.val >= 2)break;
                    }
                }
                    if(u.index==cnt+1){
                        res += (cnt - i + 2) / 2;
                        break;
                    }
                    else{
                        res++;
                        u.val--;
                        val[u.index] = u.val;
                    }
            }
            else {
                res++;
            }
        }
      //  cout<<s1<<endl;
        cout<<res<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define  ll long long
const ll inf=1e18+7;
const int maxn=2e5+7;
string s1;
int val[maxn];
struct node{
    int index;
    int val;
    bool operator<(const node& rhs)const{
        return val<rhs.val;
    }
};
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)val[i]=0;
        cin>>s1;
        int cnt=0;
        int temp=0;
        for(int i=1;i<n;i++){
            if(s1[i]!=s1[i-1]){
                node node1;
                node1.index=++cnt;
                node1.val=i-temp;
                val[node1.index]=node1.val;
                temp=i;
            }
        }
        node node1;
        node1.index=++cnt;
        node1.val=n-temp;
        val[node1.index]=node1.val;
        //cout<<-1<<endl;
        int res=0;
        node u;
        u.index=cnt+1;
        u.val=inf;
        for(int i=2;i<=cnt;i++){
            if(val[i]>1){
                u.index=i;
                u.val=val[i];
            }
        }
        for(int i=1;i<=cnt;i++){
            if(i==cnt){
                res++;
                break;
            }
            if(val[i]==1) {
                if(u.index<=i||u.val<=1){
                    while(u.index<=cnt){
                        ++u.index;
                        u.val=val[u.index];
                        if(u.index<=cnt&&u.index>i&&u.val>=2)break;
                    }
                    if(u.index==cnt+1){
                        res += (cnt - i + 2) / 2;
                        break;
                    }
                    else{
                        res++;
                        u.val--;
                        val[u.index] = u.val;
                    }
                }
            }
            else {
                res++;
            }
        }
        cout<<res<<endl;
    }
    return 0;
}


#include "bits/stdc++.h"
using namespace std;

/* ============================== defines =============================*/
#define cypher     signed main
#define ll         long long
#define int        long long
#define test       int TT;cin>>TT;while(TT--)
#define tezi       ios_base::sync_with_stdio(false);cin.tie(NULL);
#define f(i,a,b)   for(int i=(a);i<(b);i++)
#define fr(i,b,a)  for(int i=(b);i>=(a);i--)
#define pb         push_back
#define mp         make_pair
#define pii        pair<int,int>
#define vi         vector<int>
#define qi         queue<int>
#define vpii       vector<pii>
#define desc       greater<int>()
#define umap       unordered_map<int,int>
#define setbits    __builtin_popcount
#define triple     pair<pii,int>
#define ff         first
#define ss         second
#define all(x)     (x).begin(),(x).end()
#define ub         upper_bound
#define lb         lower_bound
#define PI         acos(-1)
#define eps        1e-9
#define inf        1e12

/***********************   utilities   *************************/
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};

struct comp{    //set custom comp  // careful about inserting uplicates
    bool operator() (triple a,triple b){
        if(a.ss  == b.ss)return a.ff.ff < b.ff.ff;
        return a.ss > b.ss;
    }
};

int fb(int n) {
    if(n==0) return 0;
    n |= n >> 1; n |= n >> 2; n |= n >> 4;
    n |= n >> 8; n |= n >> 16; n = n + 1;
    return (n >> 1);
}
/***********************   for debugging   *************************/
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)

/******************************* Clock *******************************/
#define start_clock   auto StArT = chrono::high_resolution_clock::now();
#define stop_clock    auto eNd   = chrono::high_resolution_clock::now();double time__taken =  chrono::duration_cast<chrono::nanoseconds>(eNd - StArT).count();time__taken *= 1e-9;
#define show_time     cerr << "Time taken by program is : " << fixed  << time__taken << setprecision(9);cerr << " sec" << endl;

/*         Think more Code less        */

/*************************** MAIN **************************/
cypher(){

    tezi
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    start_clock
    test{
        int n;cin>>n;
        string s;cin>>s;
        vi seg;
        for(int i = 0;i<n;){
            int j = i;
            while(s[j]==s[i] && j < n){
                j++;
            }
            seg.pb(j-i);
            i = j;
        }
        int ans = 0;
        int sz = seg.size();
        int j = 0;
        int i;
        for(i=0;i < sz;i++){
            while(j<sz && seg[j]==1)j++;
            ans++;
            if(i==j)j++;
            else if(j<sz){
                seg[j]--;
            }else i++;
        }
        cout<<ans<<"\n";
    }

    stop_clock
    show_time

}


#include "bits/stdc++.h"
using namespace std;

/* ============================== defines =============================*/
#define cypher     signed main
#define ll         long long
#define int        long long
#define test       int TT;cin>>TT;while(TT--)
#define tezi       ios_base::sync_with_stdio(false);cin.tie(NULL);
#define f(i,a,b)   for(int i=(a);i<(b);i++)
#define fr(i,b,a)  for(int i=(b);i>=(a);i--)
#define pb         push_back
#define mp         make_pair
#define pii        pair<int,int>
#define vi         vector<int>
#define qi         queue<int>
#define vpii       vector<pii>
#define desc       greater<int>()
#define umap       unordered_map<int,int>
#define setbits    __builtin_popcount
#define triple     pair<pii,int>
#define ff         first
#define ss         second
#define all(x)     (x).begin(),(x).end()
#define ub         upper_bound
#define lb         lower_bound
#define PI         acos(-1)
#define eps        1e-9
#define inf        1e12

/***********************   utilities   *************************/
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};

struct comp{    //set custom comp  // careful about inserting uplicates
    bool operator() (triple a,triple b){
        if(a.ss  == b.ss)return a.ff.ff < b.ff.ff;
        return a.ss > b.ss;
    }
};

int fb(int n) {
    if(n==0) return 0;
    n |= n >> 1; n |= n >> 2; n |= n >> 4;
    n |= n >> 8; n |= n >> 16; n = n + 1;
    return (n >> 1);
}
/***********************   for debugging   *************************/
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)

/******************************* Clock *******************************/
#define start_clock   auto StArT = chrono::high_resolution_clock::now();
#define stop_clock    auto eNd   = chrono::high_resolution_clock::now();double time__taken =  chrono::duration_cast<chrono::nanoseconds>(eNd - StArT).count();time__taken *= 1e-9;
#define show_time     cerr << "Time taken by program is : " << fixed  << time__taken << setprecision(9);cerr << " sec" << endl;

/*         Think more Code less        */

/*************************** MAIN **************************/
cypher(){

    tezi
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    start_clock
    test{
        int n;cin>>n;
        string s;cin>>s;
        vi seg;
        for(int i = 0;i<n;){
            int j = i;
            while(s[j]==s[i] && j < n){
                j++;
            }
            seg.pb(j-i);
            i = j;
        }
        int ans = 0;
        int sz = seg.size();
        int li = sz-1;
        while(seg[li] == 1 && li>=0)li--;
        int i;
        for(i=0;i<=li;i++){
            if(seg[li]==1)break;
            dbg(i);
            if(seg[i] == 1){
                ans++;
                seg[li]--;
                while(seg[li]==1 && li >= 0)li--;
            }else{
                ans++;
            }
        }
        ans += (sz-i+1)/2;
        // dbg(sz);
        cout<<ans<<"\n";
    }

    stop_clock
    show_time

}


#include "bits/stdc++.h"
using namespace std;

/* ============================== defines =============================*/
#define cypher     signed main
#define ll         long long
#define int        long long
#define test       int TT;cin>>TT;while(TT--)
#define tezi       ios_base::sync_with_stdio(false);cin.tie(NULL);
#define f(i,a,b)   for(int i=(a);i<(b);i++)
#define fr(i,b,a)  for(int i=(b);i>=(a);i--)
#define pb         push_back
#define mp         make_pair
#define pii        pair<int,int>
#define vi         vector<int>
#define qi         queue<int>
#define vpii       vector<pii>
#define desc       greater<int>()
#define umap       unordered_map<int,int>
#define setbits    __builtin_popcount
#define triple     pair<pii,int>
#define ff         first
#define ss         second
#define all(x)     (x).begin(),(x).end()
#define ub         upper_bound
#define lb         lower_bound
#define PI         acos(-1)
#define eps        1e-9
#define inf        1e12

/***********************   utilities   *************************/
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};

struct comp{    //set custom comp  // careful about inserting uplicates
    bool operator() (triple a,triple b){
        if(a.ss  == b.ss)return a.ff.ff < b.ff.ff;
        return a.ss > b.ss;
    }
};

int fb(int n) {
    if(n==0) return 0;
    n |= n >> 1; n |= n >> 2; n |= n >> 4;
    n |= n >> 8; n |= n >> 16; n = n + 1;
    return (n >> 1);
}
/***********************   for debugging   *************************/
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)

/******************************* Clock *******************************/
#define start_clock   auto StArT = chrono::high_resolution_clock::now();
#define stop_clock    auto eNd   = chrono::high_resolution_clock::now();double time__taken =  chrono::duration_cast<chrono::nanoseconds>(eNd - StArT).count();time__taken *= 1e-9;
#define show_time     cerr << "Time taken by program is : " << fixed  << time__taken << setprecision(9);cerr << " sec" << endl;

/*         Think more Code less        */

/*************************** MAIN **************************/
cypher(){

    tezi
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    start_clock
    test{
        int n;cin>>n;
        string s;cin>>s;
        vi seg;
        for(int i = 0;i<n;){
            int j = i;
            while(s[j]==s[i] && j < n){
                j++;
            }
            seg.pb(j-i);
            i = j;
        }
        int ans = 0;
        int sz = seg.size();
        int li = sz-1;
        while(seg[li] == 1 && li>=0)li--;
        int i;
        for(i=0;i<=li;i++){
            dbg(i);
            if(seg[i] == 1){
                ans++;
                seg[li]--;
                while(seg[li]==1 && li >= 0)li--;
                if(li < i)break;
            }else{
                ans++;
            }
        }
        ans += (sz-i+1)/2;
        // dbg(sz);
        cout<<ans<<"\n";
    }

    stop_clock
    show_time

}


#include "bits/stdc++.h"
using namespace std;

/* ============================== defines =============================*/
#define cypher     signed main
#define ll         long long
#define int        long long
#define test       int TT;cin>>TT;while(TT--)
#define tezi       ios_base::sync_with_stdio(false);cin.tie(NULL);
#define f(i,a,b)   for(int i=(a);i<(b);i++)
#define fr(i,b,a)  for(int i=(b);i>=(a);i--)
#define pb         push_back
#define mp         make_pair
#define pii        pair<int,int>
#define vi         vector<int>
#define qi         queue<int>
#define vpii       vector<pii>
#define desc       greater<int>()
#define umap       unordered_map<int,int>
#define setbits    __builtin_popcount
#define triple     pair<pii,int>
#define ff         first
#define ss         second
#define all(x)     (x).begin(),(x).end()
#define ub         upper_bound
#define lb         lower_bound
#define PI         acos(-1)
#define eps        1e-9
#define inf        1e12

/***********************   utilities   *************************/
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};

struct comp{    //set custom comp  // careful about inserting uplicates
    bool operator() (triple a,triple b){
        if(a.ss  == b.ss)return a.ff.ff < b.ff.ff;
        return a.ss > b.ss;
    }
};

int fb(int n) {
    if(n==0) return 0;
    n |= n >> 1; n |= n >> 2; n |= n >> 4;
    n |= n >> 8; n |= n >> 16; n = n + 1;
    return (n >> 1);
}
/***********************   for debugging   *************************/
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)

/******************************* Clock *******************************/
#define start_clock   auto StArT = chrono::high_resolution_clock::now();
#define stop_clock    auto eNd   = chrono::high_resolution_clock::now();double time__taken =  chrono::duration_cast<chrono::nanoseconds>(eNd - StArT).count();time__taken *= 1e-9;
#define show_time     cerr << "Time taken by program is : " << fixed  << time__taken << setprecision(9);cerr << " sec" << endl;

/*         Think more Code less        */

/*************************** MAIN **************************/
cypher(){

    tezi
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    start_clock
    test{
        int n;cin>>n;
        string s;cin>>s;
        vi seg;
        for(int i = 0;i<n;){
            int j = i;
            while(s[j]==s[i] && j < n){
                j++;
            }
            seg.pb(j-i);
            i = j;
        }
        int ans = 0;
        int sz = seg.size();
        for(int i=0;i<sz;i++){
            if(seg[i] == 1){
                ans++,sz--;
            }else ans++;
        }
        // dbg(sz);
        cout<<ans<<"\n";
    }

    stop_clock
    show_time

}


#include "bits/stdc++.h"
using namespace std;

/* ============================== defines =============================*/
#define cypher     signed main
#define ll         long long
#define int        long long
#define test       int TT;cin>>TT;while(TT--)
#define tezi       ios_base::sync_with_stdio(false);cin.tie(NULL);
#define f(i,a,b)   for(int i=(a);i<(b);i++)
#define fr(i,b,a)  for(int i=(b);i>=(a);i--)
#define pb         push_back
#define mp         make_pair
#define pii        pair<int,int>
#define vi         vector<int>
#define qi         queue<int>
#define vpii       vector<pii>
#define desc       greater<int>()
#define umap       unordered_map<int,int>
#define setbits    __builtin_popcount
#define triple     pair<pii,int>
#define ff         first
#define ss         second
#define all(x)     (x).begin(),(x).end()
#define ub         upper_bound
#define lb         lower_bound
#define PI         acos(-1)
#define eps        1e-9
#define inf        1e12

/***********************   utilities   *************************/
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};

struct comp{    //set custom comp  // careful about inserting uplicates
    bool operator() (triple a,triple b){
        if(a.ss  == b.ss)return a.ff.ff < b.ff.ff;
        return a.ss > b.ss;
    }
};

int fb(int n) {
    if(n==0) return 0;
    n |= n >> 1; n |= n >> 2; n |= n >> 4;
    n |= n >> 8; n |= n >> 16; n = n + 1;
    return (n >> 1);
}
/***********************   for debugging   *************************/
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)

/******************************* Clock *******************************/
#define start_clock   auto StArT = chrono::high_resolution_clock::now();
#define stop_clock    auto eNd   = chrono::high_resolution_clock::now();double time__taken =  chrono::duration_cast<chrono::nanoseconds>(eNd - StArT).count();time__taken *= 1e-9;
#define show_time     cerr << "Time taken by program is : " << fixed  << time__taken << setprecision(9);cerr << " sec" << endl;

/*         Think more Code less        */

/*************************** MAIN **************************/
cypher(){

    tezi
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    start_clock
    test{
        int n;cin>>n;
        string s;cin>>s;
        vi seg;
        for(int i = 0;i<n;){
            int j = i;
            while(s[j]==s[i] && j < n){
                j++;
            }
            seg.pb(j-i);
            i = j;
        }
        int ans = 0;
        int sz = seg.size();
        for(int i=0;i<sz;i++){
            if(seg[i] == 1){
                ans++;
                if(seg[sz-1] == 1)sz--;
                else seg[sz-1]--;
            }else ans++;
        }
        // dbg(sz);
        cout<<ans<<"\n";
    }

    stop_clock
    show_time

}


#include "bits/stdc++.h"
using namespace std;

/* ============================== defines =============================*/
#define cypher     signed main
#define ll         long long
#define int        long long
#define test       int TT;cin>>TT;while(TT--)
#define tezi       ios_base::sync_with_stdio(false);cin.tie(NULL);
#define f(i,a,b)   for(int i=(a);i<(b);i++)
#define fr(i,b,a)  for(int i=(b);i>=(a);i--)
#define pb         push_back
#define mp         make_pair
#define pii        pair<int,int>
#define vi         vector<int>
#define qi         queue<int>
#define vpii       vector<pii>
#define desc       greater<int>()
#define umap       unordered_map<int,int>
#define setbits    __builtin_popcount
#define triple     pair<pii,int>
#define ff         first
#define ss         second
#define all(x)     (x).begin(),(x).end()
#define ub         upper_bound
#define lb         lower_bound
#define PI         acos(-1)
#define eps        1e-9
#define inf        1e12

/***********************   utilities   *************************/
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};

struct comp{    //set custom comp  // careful about inserting uplicates
    bool operator() (triple a,triple b){
        if(a.ss  == b.ss)return a.ff.ff < b.ff.ff;
        return a.ss > b.ss;
    }
};

int fb(int n) {
    if(n==0) return 0;
    n |= n >> 1; n |= n >> 2; n |= n >> 4;
    n |= n >> 8; n |= n >> 16; n = n + 1;
    return (n >> 1);
}
/***********************   for debugging   *************************/
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)

/******************************* Clock *******************************/
#define start_clock   auto StArT = chrono::high_resolution_clock::now();
#define stop_clock    auto eNd   = chrono::high_resolution_clock::now();double time__taken =  chrono::duration_cast<chrono::nanoseconds>(eNd - StArT).count();time__taken *= 1e-9;
#define show_time     cerr << "Time taken by program is : " << fixed  << time__taken << setprecision(9);cerr << " sec" << endl;

/*         Think more Code less        */

/*************************** MAIN **************************/
cypher(){

    tezi
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    start_clock
    test{
        int n;cin>>n;
        string s;cin>>s;
        vi seg;
        for(int i = 0;i<n;){
            int j = i;
            while(s[j]==s[i] && j < n){
                j++;
            }
            seg.pb(j-i);
            i = j;
        }
        int ans = 0;
        int sz = seg.size();
        int li = sz-1;
        for(int i=0;i<sz;i++){
            if(seg[i]==0)continue;
            if(seg[i] == 1){
                ans++;
                seg[li]--;
                if(seg[li]==0)li--;
            }else ans++;
        }

        // dbg(sz);
        cout<<ans<<"\n";
    }

    stop_clock
    show_time

}


#include <bits/stdc++.h>
#define For(i,a,b) for ( int i=(a);i<=(b);i++ )
#define Dow(i,b,a) for ( int i=(b);i>=(a);i-- )
#define GO(i,x) for ( int i=head[x];i;i=e[i].nex )
#define mem(x,s) memset(x,s,sizeof(x))
#define cpy(x,s) memcpy(x,s,sizeof(x))
#define YES return puts("YES"),0
#define NO return puts("NO"),0
#define GG return puts("-1"),0
#define pb push_back
using namespace std;

struct IO
{
	#define MAXSIZE (1 << 20)
	#define isdigit(x) (x >= '0' && x <= '9')
  	char buf[MAXSIZE],*p1,*p2;
  	char pbuf[MAXSIZE],*pp;
  	IO():p1(buf),p2(buf),pp(pbuf){}
  	inline char gc() {
    	return getchar();
    	if (p1==p2) p2=(p1=buf)+fread(buf,1,MAXSIZE,stdin);
    	return p1==p2?' ':*p1++;
  	}
	inline bool blank(char ch) {return ch==' '||ch =='\n'||ch == '\r'||ch == '\t';}
  	template <class T>
  	inline void read(T &x)
	{
	    register double tmp=1;register bool sign=0; x=0;
	    register char ch=gc();
	    for (;!isdigit(ch);ch=gc()) if(ch=='-') sign=1;
	    for (;isdigit(ch);ch=gc()) x=x*10+(ch-'0');
	    if (ch=='.') for (ch=gc();isdigit(ch);ch=gc()) tmp/=10.0,x+=tmp*(ch-'0');
	    if (sign) x=-x;
  	}
  	inline void read(char *s)
	{
    	register char ch=gc();
		for (;blank(ch);ch=gc());
		for (;!blank(ch);ch=gc()) *s++=ch;
    	*s=0;
  	}
  	inline void read(char &c) {for(c=gc();blank(c);c=gc());}
  	template<class t> inline void write(t x){
		if(x<0) putchar('-'),write(-x);
		else{if(x>9) write(x/10);putchar('0'+x%10);}
	}
} io;

const int mod=1e9+7;
const int mo=998244353;
const int N=2e5+5;

int n,m,a[N],Q,b[N],tr[N*4],ans;

inline void solve()
{
	io.read(n);
	ans=0;
	For(i,1,n) scanf("%1d",&a[i]);
	int las=a[1],gs=1;
	b[gs]=1;
	For(i,2,n)
	{
		if(a[i]==las) b[gs]++;
		else gs++,b[gs]=1,las=a[i];
	}
	int nxt=1;
	while(b[nxt]==1&&nxt<=gs) nxt++;
	int i=1;
	while(i<=gs)
	{
		while(nxt<=i) nxt++;
		while(b[nxt]==1&&nxt<=gs) nxt++;
		if(b[i]>1) ans++;
		else if(nxt<=gs)
		{
			b[nxt]--;
			while(b[nxt]==1&&nxt<=gs) nxt++;
			ans++;
		}
		else ans++,i++;
		i++;
	}
	io.write(ans),puts("");
}

int main()
{
	io.read(Q);
	for (;Q--;) solve();
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int l[200010],r[200010],len[200010];

priority_queue<int,vector<int>,greater<int> >pq;

char st[200010];
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		while(pq.size()) pq.pop();
		int n;
		scanf("%d",&n);
		scanf("%s",st);
		char ch=st[0];
		int t=1;
		int length=0;
		l[1]=1,r[1]=1;
		for(int i=1;i<n;i++){
			if(ch==st[i]){
				r[t]++;
			}else{
				ch=st[i];
				l[++t]=r[t]=i+1;
			}
		}
		for(int i=1;i<=t;i++){
			len[i]=r[i]-l[i]+1;
			length+=len[i];
			if(len[i]>=2) pq.push(i);
		}

		int ans=0;
		int ll=1,rr=t;
		while(t-ll+1){
			if(len[ll]>=2){
				ll++;
				ans++;
				if(pq.size()&&pq.top()<ll) pq.pop();
			}else{
				bool flg=1;
				if(pq.size()){
					flg=0;
					int x=pq.top();pq.pop();
					len[x]--;
					if(len[x]>=2) pq.push(x);
					ans++;
				}
				if(flg){
					ll++;
					if(pq.size()&&pq.top()<ll) pq.pop();
					ans++;
				}
				if(t-ll+1)ll++;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<math.h>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;

const ll maxn=2e5+10;
ll t,n,ans,sum,now,nxt,tot;
ll b[maxn];
char la;
char a[maxn];

int main(void)
{
//	freopen("2.out","w",stdout);

	scanf("%lld",&t);

	while(t--)
	{
		sum=1;
		ans=0;
		tot=0;

		scanf("%lld",&n);
		scanf("%s",a+1);

		la=a[1];

		for(int i=1;i<=n;i++)
		{
			if(a[i]!=a[i+1])
			{
				b[++tot]=sum;
				sum=1;
			}
			else
			{
				sum++;
			}
		}

//		for(int i=1;i<=tot;i++)
//		{
//			printf("%lld\n",b[i]);
//		}
//
		ll i=1;

		while(i<=tot)
		{
//			printf("%lld\n",i);

			if(i==tot)
			{
				ans++;
				break;
			}

			if(b[i]==1&&b[i+1]==1)
			{
				i+=2;
				ans++;
			}
			if(b[i]>=2)
			{
				i+=1;
				ans++;
			}
		}

		printf("%lld\n",ans);
	}

	return 0;
}