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

void solve() {
    string s;
    cin >> s;
    int open1 = 0, open2 = 0;
    int r = 0;
    for(int i = 0; i < s.size(); ++i) {
        if(s[i] == '(') {
            open1 += 1;
        } else if(s[i] == ')') {
            if(open1 > 0) {
                open1 -= 1;
                r += 1;
            }
        } else if(s[i] == '[') {
            open2 += 1;
        } else if(s[i] == ']') {
            if(open2 > 0) {
                open2 -= 1;
                r += 1;
            }
        }
    }
    cout << r << endl;
}

int main() {
    int t;
    cin >> t;
    // t = 1;
    for(int c = 1; c <= t; ++c) {
        solve();
    }
}