1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
#include <bits/stdc++.h>
using namespace std;
#define IOS                       \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define repV(i, a, n) for (int i = n; i >= a; i--)
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
constexpr int M = 1e5 + 5;
int n, t, akt, w, idx;
ll ans;
pii maks;
string s;
priority_queue<pii> pq;

void solve()
{
    cin >> t;
    while (t--)
    {
        pq = priority_queue<pii>();
        akt = 0, w = 0, ans = 0, idx = 0;
        cin >> n >> s;
        s = s + " ";
        rep(i, 0, n - 1)
        {
            if (s[i] == '0')
            {
                if (s[i - 1] == '1' && i > 0)
                    w = 1;
                akt++;
            }
            else
            {
                if (s[i - 1] == '0' && i > 0)
                {
                    ++w;
                    if (w == 1)
                        akt *= 2;
                    pq.push({akt, w});
                    akt = 0;
                    w = 0;
                }
            }
        }
        if (akt)
        {
            if (!w)
            {
                cout << 0;
                continue;
            }
            pq.push({akt * 2, 1});
        }
        if (pq.empty())
        {
            cout << n;
            continue;
        }

        idx = 0, ans = 0;
        while (true)
        {
            maks = pq.top();
            if (maks.st - idx > 0)
            {
                if (maks.st - idx == 1)
                {
                    ++ans;
                    break;
                }

                if (maks.nd == 1)
                {
                    ans += ((maks.st - idx) / 2);
                    idx += 2;
                }
                else
                {
                    ans += (maks.st - idx - 1);
                    idx += 4;
                }
            }
            else
                break;
            pq.pop();
        }
        cout << (n - ans) << "\n";
    }
}

int main()
{
    IOS
    solve();
}