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

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t; 
    cin >> t;
    while (t--) {
        int n;
        string s;
        cin >> n >> s;

        if (n == 1) {
            // Only one char: 
            // press = 1, plus move if it's '1'
            cout << 1 + (s[0]=='1') << "\n";
            continue;
        }

        int T01 = 0, T10 = 0;
        for (int i = 0; i+1 < n; i++) {
            if      (s[i]=='0' && s[i+1]=='1') T01++;
            else if (s[i]=='1' && s[i+1]=='0') T10++;
        }
        int trans = T01 + T10;
        int start = (s[0]=='1');

        // How many moves can we cut out?
        int best_delta = 0;

        // (A) Cut 2 if possible:
        if (T01 >= 2 || T10 >= 2) {
            best_delta = 2;
        } else if (s[0]=='1' && T01 >= 1) {
            // flip the very first '1'→'0' _and_ collapse one 0→1
            best_delta = 2;
        }

        // (B) Otherwise, maybe cut 1:
        if (best_delta == 0) {
            // 1) merge one internal boundary against the end
            bool boundary1 = false;
            for (int i = 0; i+1 < n; i++) {
                if (s[i] != s[i+1] && s[i] == s[n-1]) {
                    boundary1 = true;
                    break;
                }
            }
            // 2) flip only the very first move by reversing a prefix that ends
            //    in a '0' (so start 1→0) without introducing a new boundary
            bool flip_start = (s[0]=='1' && s[n-1]=='0');

            if (boundary1 || flip_start)
                best_delta = 1;
        }

        int ans = n + start + trans - best_delta;
        cout << ans << "\n";
    }
    return 0;
}
