#include<bits/stdc++.h>
using namespace std;
// DCG-27
// Given a string of round, curly, and square open and closing brackets,
// return whether the brackets are balanced (well-formed).
// For example, given the string "([])[]({})", you should return true.
// Given the string "([)]" or "((()", you should return false.
// input:
// 3
// ([])[]({})
// ([)]
// ((()
// output"
// true
// false
// false
bool balanced(string s){
stack<char> st;
for(int i=0;i<s.size();i++){
if(s[i] == '(' || s[i] == '[' || s[i] == '{'){
// opening bracket
st.push(s[i]);
}else{
// closing bracket
if( !st.empty() && (
(st.top()=='(' && s[i] ==')')
|| (st.top()=='[' && s[i]==']')
|| (st.top()=='{' && s[i]=='}')
) ){
st.pop(); // match
}else{
return false; // mismatch
}
}
}
if(st.empty()){
return true;
}
return false;
}
int main(){
freopen("ip.txt","r",stdin);
int t;
cin>>t;
while(t--){
string s;
cin>>s;
cout<<balanced(s)<<endl;
}
return 0;
}