#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MOD = 1000000007;
int A[200100], B[500100], C[500100];
char S[200100];
void ycl() {
}
struct cmp { //priority_queue
bool operator()(const int& a, const int& b) {
return a < b;
}
};
struct edge {
int a;
bool operator<(const edge& x)const {
return x.a < a;
}
edge(int x) { a = x; }
};
bool cmpp(int a, int b) {
return a > b;
}
int ksm (int a, int b){
int ans = 1;
while (b){
if (b & 1)ans = ans * a % MOD;
a = a * a % MOD;
b >>= 1;}
return ans % MOD;};
//priority_queue<pair<int, int>, vector<pair<int, int>>, cmp>q;
void solve() {
string s;
cin >> s;
stack<char>st;
int si = s.size();
for (int i = 0; i < si; i++) {
if (s[i] == ')')s[i] = '(';
else if(s[i] == ']')s[i] = '[';
}
map<int, int>mp;
int now = 0,ma=0;
for (int i = 0; i < si; i++) {
if (st.empty() || st.top() != s[i]) {
st.push(s[i]);
if (s[i] == '[') {
now++;
ma = max(ma, now);
}
}
else {
if (!st.empty() && st.top() == '[')now--;
st.pop();
}
if (s[i] == '(') {
if (mp.find(now) == mp.end())mp[now] = 1;
else mp[now]++;
}
}
for (auto it : mp) {
if (it.second > 2) {
cout << "NO" << endl;
return;
}
}
mp.clear();
now = 0, ma = 0;
for (int i = 0; i < si; i++) {
if (st.empty() || st.top() != s[i]) {
st.push(s[i]);
if (s[i] == '(') {
now++;
ma = max(ma, now);
}
}
else {
if (!st.empty()&&st.top() == '(')now--;
st.pop();
}
if (s[i] == '[') {
if (mp.find(now) == mp.end())mp[now] = 1;
els