#include <bits/stdc++.h>
using namespace std;

#define get_ch getchar_unlocked
#define test int t;cin>>t;while(t--)
#define fo(i, k, n) for(int i=k; i<n; i++)
#define ll long long
#define ld long double
#define all(x) x.begin(), x.end()
#define deb(x) cout << #x << " " << x <<'\n'
#define is_sorted(v) std::is_sorted(v.begin(), v.end())
#define pb push_back
#define sz(x) (int)((x).size())

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<pii> vpii;
typedef set<int> Si;
typedef set<ll> Sll;
typedef set<pii> Spii;
typedef set<pll> Spll;
typedef map<int, int> Mii;
typedef map<ll, ll> Mll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
const int mod = 1000000007;
const int pi = 3.1415926535897932384626;
const int e = 2.71828182845904523536;

bool visited[100001];
vi adj[100001];
int Distance[100001];
queue<int> q;

void dfs(int s){
    if (visited[s]){
        return;
    }
    visited[s]=true;
    for(auto x:adj[s]){
        dfs(x);
    }
}

void bfs(int s){
    visited[s] = true;
    Distance[s] = 0;
    q.push(s);
    while (!q.empty()) {
        int s = q.front(); q.pop();

        for (auto u : adj[s]) {
            if (visited[u]){
                continue;
            }
            visited[u] = true;
            Distance[u] = Distance[s]+1;
            q.push(u);
        }
    }
}

void solve(){
    string s;
    cin >> s;
    int n = s.length();
    bool arr[n];
    for(int j=0; j<n; j++) arr[j] = false;
    int ind = -1;
    int count = 0;
    vi b1o,b1c,b2o,b2c;
    for(int j=0; j<n; j++){
        if(s[j] == '(') b1o.pb(j);
        if(s[j] == ')') b1c.pb(j);
        if(s[j] == '[') b2o.pb(j);
        if(s[j] == ']') b2c.pb(j);
    }
    int i=0,j=0;
    while (i<sz(b1o) && j<sz(b1c)){
        if(b1o[i]<b1c[j]){
            count++;
            i++;
            j++;
        }
        else{
            j++;
        }
    }
    i=0;
    j=0;
    while (i<sz(b2o) && j<sz(b2c)){
        if(b2o[i]<b2c[j]){
            count++;
            i++;
            j++;
        }
        else{
            j++;
        }
    }
    cout << count << endl;
}


int main(){
    ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    
    test{
        solve();
    }
    return 0;
}