#include<bits/stdc++.h>

using namespace std;
using ll = long long;
using pint = pair<int,int>;
void solve();

int main()
{
	int T;
	cin >> T;
	while(T--)solve();
}

void solve()
{
	string s; cin >> s;
	int a = 0, b = 0;
	int n = s.size();
	int cnt = 0;
	for(int i = 0; i < n; i++) {
		if(s[i] == '(') a++;
		if(s[i] == '[') b++;
		if(s[i] == ')' && a) {
			cnt++;
			a--;
		} 
		if(s[i] == ']' && b) {
			cnt++;
			b--;
		}
	}
	cout << cnt << endl;
}