// Created by ash_98

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

#define mx 200005
#define ll long long
#define mod 1000000007

int ar[mx];
char ch[mx];
int n,m,ii,k;

void solve()
{
	scanf("%s",ch);
	n=strlen(ch);
	int cnt1=0,cnt2=0,re=0;
	for(int i=n-1;i>=0;i--)
	{
		if(ch[i]=='(')
		{
			if(cnt1>0)
			{
				cnt1--;
				re++;
			}
		}
		if(ch[i]=='[')
		{
			if(cnt2>0)
			{
				cnt2--;
				re++;
			}
		}
		if(ch[i]==')')cnt1++;
		if(ch[i]==']')cnt2++;
	}
	printf("%d\n",re );
}

int main()
{
	int t=1;
	scanf("%d",&t);
	while(t--)solve();
	return 0;
}