//题目描述
//小明今年上大学,在大学里发现有很多同学都女朋友,
//两人整天都在一起腻歪,小明看到后感
//觉很孤单,现在,给你一行括号序列,你来判断一下其中的括号是否配对。
//输入
//第一行输入一个数N(0<N≤100),表示有N组测试数据。
//后面的N行输入多组输入数据,每组
//输入数据都是一个字符串S(S的长度小于10000,且S不是空串),
//测试数据组数少于5组。数据保
//证S中只含有”[”, ”]”, ”(”, ”)” 四种字符 ,输入以“EOF”结束。
//输出
//每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,
//则输出Yes,如果不配对则
//输出No。
//输入样例
//3
//[(])
//(])
//([[]()])
//输出样例
//No
//No
//Yes
#include<iostream>
#include<stack>
#include<string>
#include <stdio.h>
using namespace std;
bool judge(string str)
{
stack<char>S;
int len=str.length();
int i;char temp;
for(i=0;i<len;i++)
{
if(str[i]=='('||str[i]=='[')
S.push(str[i]);
else
{
if(S.empty())
return false;
temp=S.top();
S.pop();
switch(str[i])
{
case ')':
if(temp!='(')
return false;
break;
case ']':
if(temp!='[')
return false;
break;
}
}
}
if(!S.empty()) return false;
return true;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
string str;
while(n--)
{
cin>>str;
if(judge(str))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
}
return 0;
}