#include <bits/stdc++.h>
using namespace std;
int T;
stack<char> st;
signed main() {
scanf("%d", &T);
for (int i = 1; i <= T; i++) {
string s;
scanf("%s", &s);
bool p = 1;
for (int j = 0; j < s.size(); j++) {
int c = char(s[j]);
if (c == '(' || c == '[') {
st.push(c);
}
if ((c == ')' || c == ']') && !st.empty()) {
if ((c == ')' && st.top() != '(') || (c == ']' && st.top() != '[')) {
p = 0;
} else {
if (!st.empty())
st.pop();
else {
p = 0;
}
}
}
}
if (!st.empty())
p = 0;
if (p) {
printf("Yes");
} else {
printf("No");
}
puts("");
}
}