#include <iostream>
#include <vector>
#include <stack>
using namespace std;

int main()
{
    int n;
    cin >> n;
    for(int T = 0; T < n; T++) {
  string o;
  cin>>o;
  stack<char>p;
        int ne_balansirana = 1;
  for(int y=0; y<o.size(); y+=1){
      if(o[y] == '(' or o[y] == '{' or o[y] == '[') {
          p.push(o[y]);
      }
      else {
          if(o[y] == ')' and  !p.empty() and p.top() == '(') {
              p.pop();
          }
          else if(o[y] == '}' and !p.empty() and p.top() == '{') {
              p.pop();
          }
          else if(o[y] == ']' and !p.empty() and p.top() == '[') {
              p.pop();
          }
          else {
              cout << "NO" << endl;
              ne_balansirana = 0;
              break;
          }
      }
            
    }

        if(ne_balansirana == 0) {
            continue;
        }

    if(p.empty()) {
        cout << "YES" << endl;
    }
    else {
        cout << "NO" << endl;
    }
   
    }
    
    
    return 0;
}
// ]]][[[[
