#include <bits/stdc++.h>

using namespace std;
#define back(i, n)   for(int i =n-1;i>=0;i--)
#define ll              long long
#define endl              "\n"
#define pb              push_back
#define lp(i, n)        for(int i = 0 ; (int)i<n ; i++)
#define loop(i, x, n)       for(int i=x ; (int)i<=n ; i++)

int countDigit(int n) {
    int count = 0;
    while (n != 0) {
        n = n / 10;
        ++count;
    }
    return count;
}

void tiranga() {
    int t;
    cin >> t;
    lp(i, t) {
        string s;
        cin>>s;
        int fir=0,sec=0;
        int res=0;
        lp(i,s.length()){
            if(s[i]=='(') fir++;
            else if(s[i]=='[') sec++;
            else if(s[i]==')'){
                if(fir) fir-- , res++;
            }
            else{
                if (sec) sec-- , res++;
            }
        }
        cout<<res<<endl;
    }
}

int main() {
    tiranga();
    return 0;
}
