#include <bits/stdc++.h>
using namespace std;
#define LL long long
 
int a[1001000];
 
int main() {
    ios_base::sync_with_stdio(false);
	int t;
	t = 1;
	cin >> t;
	
	while(t--){
	    string s;
	    cin >> s;
	    int ans = 0;
	    int b = 0;
	    int c = 0;
	    for(int i = 0; i < s.size(); i++) {
	        if (s[i] == '(') {
	            b++;
	            continue;
	        }
	        if (s[i] == '[') {
	            c++;
	            continue;
	        }
	        if (s[i] == ')') {
	            if (b > 0) {
	                b--;
	                ans++;
	            }
	            continue;
	        }
	        if (s[i] == ']') {
	            if (c > 0) {
	                c--;
	                ans++;
	            }
	            continue;
	        }
	        
	    }
	    cout << ans << "\n";
	}
	return 0;
}