#include <bits/stdc++.h>

using namespace std;

#define int long long
#define nn "\n"
#define pi pair<int, int>
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define eb emplace_back

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

    int t;
    cin >> t;

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

        if (s[0] == '1') {
            int ans = 0;

            for (int i = 1; i < n; i++) {
                if (s[i] == '0') ans++;
            }

            cout << ans << nn;
            continue;
        }

        int total1 = 0;
        for (char c : s) {
            if (c == '1') total1++;
        }

        int ans = total1;

        int before1 = 0;
        int after0 = 0;

        for (int i = 0; i < n; i++) {
            if (s[i] == '0') after0++;
        }

        for (int i = 0; i < n; i++) {
            if (s[i] == '1') {
                ans = min(ans, before1 + after0);
                before1++;
            } else {
                after0--;
            }
        }

        cout << ans << nn;
    }

    return 0;
}