#define pb push_back
#define ll long long int
#define vi vector<int>
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int mod = 1e9 + 7;

void solve() {

	string s;
	cin >> s;
	stack<char> st1;
	stack<char> st2;
	int cnt = 0;
	for (int i = 0; i < s.size(); i++) {
		if (s[i] == '(') {
			st1.push(s[i]);
		} else if (s[i] == '[') {
			st2.push(s[i]);
		} else if (s[i] == ')') {
			if (!st1.empty() && st1.top() == '(') {
				st1.pop();
				cnt++;
			}
		} else if (s[i] == ']') {
			if (!st2.empty() && st2.top() == '[') {
				st2.pop();
				cnt++;
			}
		}
	}
	cout << cnt;

}

int main() {

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int t = 1;
	cin >> t;
	while (t--) {
		solve();
		cout << '\n';
	}
	return 0;
}