#include <iostream>
#include <stack>
#include <string>
using namespace std;
/*
Rules of failing
1.extra \'(\' or \'[\'
2.extra \')\' or \']\'
3.extra notation
4.miss match like \'(\' to \']\' or \'[\' to \')\'
*/
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++){
string s,temp;
cin>>s;
stack<string> stk;
bool correct = true;
for(int i=0;i<s.length();i++){
if(s.empty()){
break;
}
else if(s[i]==\'(\' || s[i]==\'[\'){
temp=s[i];
stk.push(temp);
}
else if(s[i]==\')\' && !stk.empty() && stk.top()=="("){ // notice!! if stk.empty(), then there\'s no stk.top()
stk.pop();
}
else if(s[i]==\']\' && !stk.empty() && stk.top()=="["){
stk.pop();
}
else{
correct = false;
break;
}
}
if(!stk.empty()){ //check if there are extra \'(\' or \'[\'
correct = false;
}
cout<<((correct)? "Yes":"No")<<endl;
}
return 0;
}