#include <bits/stdc++.h>

#define int long long
using namespace std;

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;

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

        int sq = 0, curl = 0;
        int ans = 0;
        for (auto c: s) {
            if (c=='('){
                curl++;
            }
            else if (c=='['){
                sq++;
            }
            else if (c==')'){
                if (curl){
                    curl--;
                    ans++;
                }
            }
            else if (c==']' && sq){
                sq--;
                ans++;
            }

        }
        cout<<ans<<'\n';
    }
}