/* In the name of Allah */
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define debug(x) cout << "[" << #x << ":" << x << "]"
#define pii pair<int , int>
#define pdd pair<double , double>
#define F first
#define S second
const int maxn = 1e6 + 10 , inf = 1e9;const ld pi = 3.14159265;
int t , answer;
string s;
vector<char> a , b;
int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> t;
    while (t --){
        cin >> s;
        a.clear(); b.clear(); answer = 0;
        for (int i = 0; i < s.size(); i ++){
            if (s[i] == '('){
                a.push_back('('); continue;
            }
            if (s[i] == ')'){
                if (a.size()) {a.pop_back(); answer ++;} continue;
            }
            if (s[i] == '['){
                b.push_back('['); continue;
            }
            if (b.size()) {b.pop_back(); answer ++;}
        }
        cout << answer << endl;
    }
    return 0;
}
