#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>s;
stack<char>stk;
int f=1;
int x=s.size();
for(int j=0;j<x;j++){
if(s[j]=='('){
stk.push('(');
}else if(s[j]=='[')
{
stk.push('[');
}else if(s[j]==')'){
if(stk.empty()){
f=0;
}
else{
if(stk.top()=='('){
stk.pop();
}else{
f=0;
}
}
}else if(s[j]==']'){
if(stk.empty()){
f=0;
}else{
if(stk.top()=='['){
stk.pop();
}else{
f=0;
}
}
}
}
if(f==1&&stk.empty()){
cout<<"Yes\n";
}else cout<<"No\n";
}
return 0;
}