#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll ,ll> pll;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(X) (X).begin(), (X).end()
#define REP(i,x,y) for(ll i = x;i <= y;++i)
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
vector<string> vec_splitter(string s) {
    for(char& c: s) c = c == ','?  ' ': c;
    stringstream ss; ss << s;
    vector<string> res;
    for(string z; ss >> z; res.push_back(z))
        ;
    return res;
}
void debug_out(vector<string> args, int idx) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, Head H, Tail... T) {
    if(idx > 0) cerr << ", ";
    stringstream ss; ss << H;
    cerr << args[idx] << " = " << ss.str();
    debug_out(args, idx + 1, T...);
}
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __VA_ARGS__)
void localTest() {
    #ifndef ONLINE_JUDGE
        freopen("inp", "r", stdin);
        freopen("out", "w", stdout);
    #endif      
}
int main() {

    localTest();
    fastio;
    ll t;
    cin >> t;
    while(t--) {
        string s;
        cin >> s;
        ll n = s.size(), ans = 0;
        stack<char> s1, s2;
        for(ll i = 0; i < n; ++i) {
            if(s[i] == '(') {
                s1.push('(');
            } else if(s[i] == '[') {
                s2.push(']');
            } else if(s[i] == ')') {
                if(!s1.empty()) {
                    s1.pop();
                    ++ans;
                }
            } else {
                if(!s2.empty()) {
                    s2.pop();
                    ++ans;
                }
            }
        }
        cout << ans << "\n";
    }

    return 0;
}