#include<iostream>
#include<stack>
using namespace std;
bool match(char a,char b)
{
if(a=='('&&b==')') return true;
else if(a=='<'&&b=='>') return true;
else if(a=='['&&b==']') return true;
else if(a=='{'&&b=='}') return true;
else return false;
}
bool Bracket(string str)
{
stack<char> s;
for(int i=0;i<(int)str.length();i++)
{
if(str[i]=='('||str[i]=='<'||str[i]=='['||str[i]=='{')
s.push(str[i]);
else if(!s.empty()&&match(s.top(),str[i]))
s.pop();
else
return false;
}
if(s.empty()) return true;
else return false;
}
int main()
{
int t;cin>>t;
for(int n=0;n<t;n++)
{
string str;cin>>str;
if(Bracket(str))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
}