#include <bits/stdc++.h>

using namespace std;
#define forl(i,n) for(int i=0;i<n;i++)
#define fore(i,n) for(int i=1;i<=n;i++)
#define revforl(i,n) for(int i=n-1;i>=0;i--)
#define revfore(i,n) for(int i=n;i>0;i--)
#define vi vector<int>
#define vvi vector<vector<int>>
#define li long int
#define lli long long int
#define mp make_pair
#define pb push_back
#define CASE(t) cout<<"Case #"<<(t)<<": ";
#define MAXN 1e5
#define LIMIT 1000000
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

int bs_lowerbound(int a[],int n,int x){
    int l=0;
    int h=n-1;
    while(l<=h){
        int mid=(l+h)/2;
        if(x>a[mid]) l=mid+1;
        else h=mid-1;
    }
    return l;
}
int bs_upperbound(int a[],int n,int x){
    int l=0;
    int h=n-1;
    while(l<=h){
        int mid=(l+h)/2;
        if(x<a[mid]) h=mid-1;
        else l=mid+1;
    }
    return h;
}

int prime_flag[LIMIT];
void calcprime_flag(){
    prime_flag[0]=1;
    prime_flag[1]=1;
    for(long long int i=2;i*i<LIMIT;i++){
        if(!prime_flag[i]){
            for(long long int j=i*i;j<LIMIT;j+=i){
                prime_flag[j]=1;
            }
        }
    }
}

int check_perfect_square(long long n){
    double sqrt1=sqrt(n);
    if(sqrt1==int(sqrt(n))) return 1;
    else return 0;
}

bool is_integer(float k)
{
    return floor(k) == k;
}

bool vis[LIMIT];
int solve(){
    //CODE HERE
    string s; cin>>s;
    memset(vis,0,sizeof(vis));
    stack<int> br;
    vector<pair<int,int>>p;
    int n=s.length();
    forl(i,n){
        if(s[i]=='(' || s[i]=='[') br.push(i);
        else{
            if(br.empty()) continue;
            if((s[i]==')' && s[br.top()]=='(') || (s[i]==']' && s[br.top()]=='[')){
                vis[i]=1;vis[br.top()]=1;
                br.pop();
            }
            else{
                while(!br.empty()) br.pop();
            }
        }
    }
    int ans=0,lef,rig,tmp=0;
    forl(i,n){
        if(!vis[i]) continue;
        int j=i;
        while(vis[j] && j<n){
            if(s[j]=='[') tmp++;
            if(tmp>=ans){
                ans=tmp;
                lef=i;
                rig=j;
            }
            j++;
        }
        p.pb(mp(lef,rig));
        i=j-1;
    }
    cout<<ans<<endl;
    for(int i=0;i<p.size();i++){
        for(int j=p[i].first;j<=p[i].second;j++){
            cout<<s[j];
        }
    }
    cout<<endl;

}

int main()
{
    IOS
    int T;
    cin>>T;
    fore(t,T)
    {
       // CASE(t);
        solve();
    }
}


#include <bits/stdc++.h>

using namespace std;
#define forl(i,n) for(int i=0;i<n;i++)
#define fore(i,n) for(int i=1;i<=n;i++)
#define revforl(i,n) for(int i=n-1;i>=0;i--)
#define revfore(i,n) for(int i=n;i>0;i--)
#define vi vector<int>
#define vvi vector<vector<int>>
#define li long int
#define lli long long int
#define mp make_pair
#define pb push_back
#define CASE(t) cout<<"Case #"<<(t)<<": ";
#define MAXN 1e5
#define LIMIT 1000000
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

int bs_lowerbound(int a[],int n,int x){
    int l=0;
    int h=n-1;
    while(l<=h){
        int mid=(l+h)/2;
        if(x>a[mid]) l=mid+1;
        else h=mid-1;
    }
    return l;
}
int bs_upperbound(int a[],int n,int x){
    int l=0;
    int h=n-1;
    while(l<=h){
        int mid=(l+h)/2;
        if(x<a[mid]) h=mid-1;
        else l=mid+1;
    }
    return h;
}

int prime_flag[LIMIT];
void calcprime_flag(){
    prime_flag[0]=1;
    prime_flag[1]=1;
    for(long long int i=2;i*i<LIMIT;i++){
        if(!prime_flag[i]){
            for(long long int j=i*i;j<LIMIT;j+=i){
                prime_flag[j]=1;
            }
        }
    }
}

int check_perfect_square(long long n){
    double sqrt1=sqrt(n);
    if(sqrt1==int(sqrt(n))) return 1;
    else return 0;
}

bool is_integer(float k)
{
    return floor(k) == k;
}

bool vis[LIMIT];
int solve(){
    //CODE HERE
    string s; cin>>s;
    memset(vis,0,sizeof(vis));
    stack<int> br;
    vector<pair<int,int>>p;
    int n=s.length();
    forl(i,n){
        if(s[i]=='(' || s[i]=='[') br.push(i);
        else{
            if(br.empty()) continue;
            if((s[i]==')' && s[br.top()]=='(') || (s[i]==']' && s[br.top()]=='[')){
                vis[i]=1;vis[br.top()]=1;
                br.pop();
            }
            else{
                while(!br.empty()) br.pop();
            }
        }
    }
    int ans=0,lef,rig,tmp=0;
    forl(i,n){
        if(!vis[i]) continue;
        int j=i;
        while(vis[j] && j<n){
            if(s[j]=='[') tmp++;
            if(tmp>=ans){
                ans=tmp;
                lef=i;
                rig=j;
            }
            j++;
        }
        p.pb(mp(lef,rig));
        i=j-1;
    }
    cout<<ans<<endl;
    for(int i=0;i<p.size();i++){
        for(int j=p[i].first;j<=p[i].second;j++){
            cout<<s[j];
        }
    }
    cout<<endl;

}

int main()
{
    IOS
    int T;
    T=1;
    fore(t,T)
    {
       // CASE(t);
        solve();
    }
}


#include<bits/stdc++.h>
using namespace std;

#define mp make_pair
#define ll long long int
#define f(i,n) for(ll i=0;i<n;i++)
#define ff(i,n) for(ll i=1;i<=n;i++)
#define  pb push_back
#define a(s) a.size();
#define v() vector<ll>v
#define PI 3.14159265
#define st string
#define ss(a) sort(a.begin(),a.end())

#define mod 1000000007
ll mx(ll a[],ll n)
{
    return *(max_element(a,a+n));
}
ll mi(ll a[],ll n)
{
    return *(min_element(a,a+n));
}
ll power(ll x, ll y)
{
    ll res = 1;	 // Initialize result
    x=x%mod;
    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x)%mod;

        // n must be even now
        y = y>>1; // y = y/2
        x = (x*x)%mod; // Change x to x^2
    }
    return res%mod;
}

void tpm(long long int a[],long long int b[],ll c[],long long int s,long long int m,long long int e)
{
    long long int l1=m-s+1;
    long long int l2=e-m;
    long long int t1[l1],y1[l1],c1[l1];
    long long int t2[l2],y2[l2],c2[l2];
    for(long long int i=0;i<l1;i++)
    {t1[i]=a[s+i];
        y1[i]=b[s+i]; c1[i]=c[s+i];
    }
    for(long long int i=0;i<l2;i++)
    {t2[i]=a[m+1+i];
        y2[i]=b[m+1+i]; c2[i]=c[m+1+i];
    }
    long long int i=0,j=0;
    long long int k=s;

    while(i<l1&&j<l2)
    {
        if(t1[i]<t2[j])
        {a[k]=t1[i];b[k]=y1[i];c[k]=c1[i];
            i++;
            k++;
        }
        else if( t1[i]==t2[j])
        {if(y1[i]>y2[j])
            {  a[k]=t1[i];b[k]=y1[i];c[k]=c1[i];
                i++;
                k++;}
            else
            {  a[k]=t2[j];b[k]=y2[j];c[k]=c2[j];
                j++;
                k++;}

        }
        else
        {a[k]=t2[j];b[k]=y2[j];c[k]=c2[j];
            j++;k++;}
    }

    while(i<l1)
    {a[k]=t1[i];b[k]=y1[i];c[k]=c1[i];
        i++;
        k++;
    }
    while(j<l2)
    {a[k]=t2[j];b[k]=y2[j];c[k]=c2[j];
        j++;
        k++;
    }

}

void sor(long long int a[],long long int b[],ll c[],long long int s,long long int e)
{
    if(s<e)
    {long long int m=(s+e)/2;

        sor(a,b,c,s,m);

        sor(a,b,c,m+1,e);

        tpm(a,b,c,s,m,e);
    }

}

void yes()
{
    cout<<"YES\n";
}
void no()
{
    cout<<"NO\n";
}

ll upperIndex(ll arr[], ll n, ll y)
{
    ll l = 0, h = n;
    while (l <= h) {
        ll mid = (l + h) / 2;
        if (arr[mid] < y)
            l = mid + 1;
        else
            h = mid - 1;
    }
    return h;
}
void pv(vector<ll>a,ll n)
{
    f(i,n)
        cout<<a[i]<<" ";
    cout<<"\n";
}
void ga(ll a[],ll n) {
    f(i, n)
        cin>>a[i];
}

//long long int sum(ll i,ll j,ll l)
//{

//  return a[i][j]+a[i-l][j-l]-a[i][j-l]-a[i-l][j];
//}
//ll n,k,a,b;

//ll pre[1000000];ll suff[1000000];
int main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    st a;
    cin>>a;
    stack<ll>s;
    vector<ll>l;
    vector<ll>r;
    f(i,a.length())
    {
        if(s.size()==0)
            s.push(i);
        else
        {

            if(a[i]==']')
            {
                if(a[s.top()]=='[')
                {

                    l.push_back(s.top());
                    r.push_back(i);
                    s.pop();
                } else
                    s.push(i);
            }
            else if (a[i]==')')
            {
                if(a[s.top()]=='(')
                {
                    l.push_back(s.top());
                    r.push_back(i);
                    s.pop();
                } else
                    s.push(i);
            } else
                s.push(i);

        }

    }
    ll n=l.size();
    if(n==0)
    { cout<<0;exit(0);}
    ll la[n],ra[n];ll p[n]={0};
    f(i,n)
    {
        la[i]=l[i];ra[i]=r[i];
    }
    sor(la,ra,p,0,n-1);vector<ll>fl,fr,left,right;
    ll j=0;
   // pv(l,n);pv(r,n);
    fl.push_back(la[j]);fr.push_back(ra[j]);
    ff(i,n-1)
    {
        if(la[i]>=la[j]&&ra[i]<=ra[j])
        {

        } else
        {
            fl.push_back(la[i]);fr.push_back(ra[i]);
            j=i;
        }

    }
    ll n2=fl.size();
    //pv(fl,n2);pv(fr,n2);
    ll d=1;
    ff(i,n2-1)
    {
        if(fl[i]!=fr[i-1]+1)
        {
            left.push_back(fl[i-1]);right.push_back(fr[i-1]);d=0;
        } else
        {
            fl[i]=fl[i-1];d=1;
        }

    }
    ll n3=left.size();
    if(n3==0 ||
    right[n3-1]!=fr[n2-1])
    left.push_back(fl[n2-1]);right.push_back(fr[n2-1]);
    n3=left.size();
  //  pv(left,n3);pv(right,n3);
   ll ans=-1;ll ansl,ansr;
    ll c=0;
    f(i,n3)
    {

        for(ll k=left[i];k<=right[i];k++)
        {
            if(a[k]=='[')
                c++;
        }
        if(c>ans)
        {
            ansl=left[i];
            ansr=right[i];
        }
    ans=max(ans,c);
c=0;
    }
    cout<<ans;
    if(ans>0) {
        cout << "\n";
        for (ll i = ansl; i <= ansr; i++)
            cout << a[i];
    }
}


#include <bits/stdc++.h>
#define pii pair<int,int>
#define f first
#define s second
#define mp make_pair

using namespace std;
const int N = 100005;
string s;
int nb[N];
stack<pair<char,int>> st;

int main()
{
    //ifstream cin("in.txt");
    cin>>s;
    if(s[0] == '[') nb[0] = 1;
    for(int i=1 ; i<(int)s.size() ; ++i)
    {
        if(s[i] == '[')
        {
            nb[i] = 1 + nb[i-1];
        }
        else nb[i] = nb[i-1];
    }
    st.push(mp(s[0],0));
//    for(int i=0;i<(int)s.size();i++)
//        cout<<nb[i]<<" ";
//    cout<<endl;
    int ans(0);
    int l(0),r(-1);
    for(int i=1; i<(int)s.size(); ++i)
    {
        if(st.size())
        {
            char c = st.top().f;
            int ind = st.top().s;
           // cout<<c<<" "<<s[i]<<endl;
            if((c=='(' && s[i]==')') || (c=='[' && s[i]==']'))
            {
                st.pop();
                if(ind)
                {
                    //cout<<ans<<" "<<nb[i]-nb[ind-1];

                    if(ans < nb[i]-nb[ind-1])
                    {
                        //cout<<"dkhalt "<<ans<<" "<<nb[i] - nb[ind-1]<<endl;
                        ans = nb[i] - nb[ind-1];
                        l = ind;
                        r = i;
                    }
                    //cout<<"tab3a "<<ans<<endl;
                }
                else
                {
                    if(ans < nb[i])
                    {
                        ans = nb[i];
                        l = ind;
                        r = i;
                    }
                }
            }
            else st.push(mp(s[i],i));
        }
        else st.push(mp(s[i],i));
      //  cout<<ans<<endl;
    }
    cout<<ans<<endl;
    for(int i=l; i<=r; i++) cout<<s[i];

    return 0;
}


#include <bits/stdc++.h>
#define pii pair<int,int>
#define f first
#define s second
#define mp make_pair

using namespace std;
const int N = 100005;
string s;
int nb[N];
stack<pair<char,int>> st;

int main()
{
   // ifstream cin("in.txt");
    cin>>s;
    if(s[0] == '[') nb[0] = 1;
    for(int i=1 ; i<(int)s.size() ; ++i)
    {
        if(s[i] == '[')
        {
            nb[i] = 1 + nb[i-1];
        }
        else nb[i] = nb[i-1];
    }
    st.push(mp(s[0],0));
    int ans(0);
    int l(0),r(-1);
    for(int i=1; i<(int)s.size(); ++i)
    {
        char c = st.top().f;
        int ind = st.top().s;
      //  cout<<c<<" "<<s[i]<<endl;
        if((c=='(' && s[i]==')') || (c=='[' && s[i]==']'))
        {
            st.pop();
            // cout<<c<<" "<<ind<<" "<<s[i]<<" "<<i<<endl;
            if(ind)
            {
                if(ans < nb[i]-nb[ind-1]);
                {
                    ans = nb[i] - nb[ind-1];
                    l = ind;
                    r = i;
                }
            }
            else
            {
                if(ans < nb[i])
                {
                    ans = nb[i];
                    l = ind;
                    r = i;
                }
            }
        }
        else st.push(mp(s[i],i));

    }
    cout<<ans<<endl;
    for(int i=l;i<=r;i++) cout<<s[i];

    return 0;
}


#include <bits/stdc++.h>
#define pii pair<int,int>
#define f first
#define s second
#define mp make_pair

using namespace std;
const int N = 100005;
string s;
int nb[N];
stack<pair<char,int>> st;

int main()
{
    //ifstream cin("in.txt");
    cin>>s;
    if(s[0] == '[') nb[0] = 1;
    for(int i=1 ; i<(int)s.size() ; ++i)
    {
        if(s[i] == '[')
        {
            nb[i] = 1 + nb[i-1];
        }
        else nb[i] = nb[i-1];
    }
    st.push(mp(s[0],0));
//    for(int i=0;i<(int)s.size();i++)
//        cout<<nb[i]<<" ";
//    cout<<endl;
    int ans(0);
    int l(0),r(-1);
    for(int i=1; i<(int)s.size(); ++i)
    {
        if(st.size())
        {
            char c = st.top().f;
            int ind = st.top().s;
            //cout<<c<<" "<<s[i]<<endl;
            if((c=='(' && s[i]==')') || (c=='[' && s[i]==']'))
            {
                st.pop();
                if(st.size())
                {
                    if(ind )
                    {
                        //cout<<ans<<" "<<nb[i]-nb[ind-1]<<endl;
                        int lastind = st.top().s;
                        if(ans < nb[i]-nb[lastind])
                        {
                            //cout<<"dkhalt "<<ans<<" "<<nb[i] - nb[ind-1]<<endl;
                            ans = nb[i] - nb[lastind];
                            l = lastind+1;
                            r = i;
                        }
                        //cout<<"tab3a "<<ans<<endl;
                    }
                    else
                    {
                        if(ans < nb[i])
                        {
                            ans = nb[i];
                            l = ind;
                            r = i;
                        }
                    }
                }
                else
                {
                   ans = nb[i];
                   l = 0;
                   r = i;
                }
            }
            else st.push(mp(s[i],i));
        }
        else st.push(mp(s[i],i));
        //cout<<i<<" "<<l<<" "<<r<<" "<<ans<<endl;
        //  cout<<ans<<endl;
    }
    cout<<ans<<endl;
    for(int i=l; i<=r; i++) cout<<s[i];

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

stack<char> st;
int l(0),r,mx(0),cur(0),idx(0),n,ansl,ansr;

void empt(){
    while(!st.empty())st.pop();
    r = l = idx + 1;
    cur = 0;
}

int main()
{
    string s;
    cin>>s;
    n = s.size();
    while(idx<n){
        if(s[idx]=='(' || s[idx]=='[')st.push(s[idx]);

        if(s[idx]==')'){
            if(!st.empty() && st.top()=='('){
                r += 2;
                st.pop();
            }else empt();
        }
        if(s[idx]==']'){
            if(!st.empty() && st.top()=='['){
                r += 2;
                ++cur;
                st.pop();
            }else empt();
        }

        if(cur>mx){
            mx = cur;
            ansl = l;
            ansr = r;
        }

        ++idx;
    }
    cout<<mx<<endl<<string(s.begin()+ansl,s.begin()+ansr);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

stack<char> st;
int r,mx,cur,idx,n,ansl,ansr,tot;

void empt(){
    while(!st.empty())st.pop();
    r = idx + 1;
    cur = 0;
    tot = 0;
}

int main()
{
    string s;
    cin>>s;
//    cout<<string(s.begin()+2,s.begin()+4);
    n = s.size();
    while(idx<n){
        if(s[idx]=='(' || s[idx]=='[')st.push(s[idx]);

        if(s[idx]==')'){
            if(!st.empty() && st.top()=='('){
                ++tot;
                r = idx+1;
                st.pop();
            }else empt();
        }
        if(s[idx]==']'){
            if(!st.empty() && st.top()=='['){
                ++tot;
                ++cur;
                r = idx+1;
                st.pop();
            }else empt();
        }

        if(cur>mx){
            mx = cur;
            ansr = r;
            ansl = r - 2 * tot;
        }

        ++idx;
    }
    cout<<mx<<endl<<string(s.begin()+ansl,s.begin()+ansr);

    return 0;
}


#include <bits/stdc++.h>
#define pci pair<char,int>

using namespace std;

stack<pci> st;
int r,mx,cur,idx,n,ansl,ansr,tot;

void empt(){
    while(!st.empty())st.pop();
    cur = 0;
    tot = 0;
}

int main()
{
    string s;
    cin>>s;
    n = s.size();
    while(idx<n){
        if(s[idx]=='(' || s[idx]=='[')st.push(pci(s[idx],idx));

        if(s[idx]==')'){
            if(!st.empty() && st.top().first=='('){
                ++tot;
                st.pop();
                r = idx+1;
            }else empt();
        }
        if(s[idx]==']'){
            if(!st.empty() && st.top().first=='['){
                ++tot;
                ++cur;
                st.pop();
                r = idx+1;
            }else empt();
        }

        if(cur>=mx && (st.empty() || (st.top().second<r-2*tot) )){
            mx = cur;
            ansr = r;
            ansl = r - 2 * tot;
        }

        ++idx;
    }
    cout<<mx<<endl<<string(s.begin()+ansl,s.begin()+ansr);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

stack<char> st;
int r,mx,cur,idx,n,ansl,ansr,tot;

void empt(){
    while(!st.empty())st.pop();
    cur = 0;
    tot = 0;
}

int main()
{
    string s;
    cin>>s;
//    cout<<string(s.begin()+2,s.begin()+4);
    n = s.size();
    while(idx<n){
        bool test(false);
        if(s[idx]=='(' || s[idx]=='[')st.push(s[idx]);

        if(s[idx]==')'){
            if(!st.empty() && st.top()=='('){
                ++tot;
                st.pop();
                if(st.empty()){
                    r = idx+1;
                    test = true;
                }
            }else empt();
        }
        if(s[idx]==']'){
            if(!st.empty() && st.top()=='['){
                ++tot;
                ++cur;
                st.pop();
                if(st.empty()){
                    r = idx+1;
                    test = true;
                }
            }else empt();
        }

        if(test && cur>mx){
            mx = cur;
            ansr = r;
            ansl = r - 2 * tot;
        }

        ++idx;
    }
    cout<<mx<<endl<<string(s.begin()+ansl,s.begin()+ansr);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

stack<char> st;
int l(0),r,mx(0),cur(0),idx(0),n,ansl,ansr;

void empt(){
    while(!st.empty())st.pop();
    r = l = idx + 1;
    cur = 0;
}

int main()
{
    string s;
    cin>>s;
    n = s.size();
    while(idx<n){
        if(s[idx]=='(' || s[idx]=='[')st.push(s[idx]);

        if(s[idx]==')'){
            if(!st.empty() && st.top()=='('){
                r += 2;
                st.pop();
            }else empt();
        }
        if(s[idx]==']'){
            if(!st.empty() && st.top()=='['){
                r += 2;
                ++cur;
                st.pop();
            }else empt();
        }

        if(cur>=mx){
            mx = cur;
            ansl = l;
            ansr = r;
        }

        ++idx;
    }
    cout<<mx<<endl<<string(s.begin()+ansl,s.begin()+ansr);

    return 0;
}


#include <bits/stdc++.h>
#define pci pair<char,int>

using namespace std;

stack<pci> st;
int r,mx,cur,idx,n,ansl,ansr,tot;
string s;

void empt(){
    while(!st.empty())st.pop();
    cur = 0;
    tot = 0;
}

void e5dem(){
    while(idx<n){
        if(s[idx]=='(' || s[idx]=='[')st.push(pci(s[idx],idx));

        if(s[idx]==')'){
            if(!st.empty() && st.top().first=='('){
                ++tot;
                st.pop();
                r = idx+1;
            }else empt();
        }
        if(s[idx]==']'){
            if(!st.empty() && st.top().first=='['){
                ++tot;
                ++cur;
                st.pop();
                r = idx+1;
            }else empt();
        }

        if(cur>=mx && (st.empty() || (st.top().second<r-2*tot) )){
            mx = cur;
            ansr = r;
            ansl = r - 2 * tot;
        }

        ++idx;
    }
}

int main()
{
    cin>>s;
    n = s.size();
    e5dem();
    if(!st.empty()){
        idx = st.top().second+1;
        empt();
        e5dem();
    }
    cout<<mx<<endl<<string(s.begin()+ansl,s.begin()+ansr);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

stack<char> st;
int l(0),r,mx(0),cur(0),idx(0),n,ansl,ansr;

void empt(){
    while(!st.empty())st.pop();
    r = l = idx + 1;
    cur = 0;
}

int main()
{
    string s;
    cin>>s;
    n = s.size();
    while(idx<n){
        if(s[idx]=='(' || s[idx]=='[')st.push(s[idx]);

        if(s[idx]==')'){
            if(!st.empty() && st.top()=='('){
                r += 2;
                st.pop();
            }else empt();
        }
        if(s[idx]==']'){
            if(!st.empty() && st.top()=='['){
                r += 2;
                ++cur;
                st.pop();
            }else empt();
        }

        if(cur>mx){
            mx = cur;
            ansl = l;
            ansr = r;
        }

        ++idx;
    }

    cout<<mx<<endl<<string(s.begin()+l,s.begin()+r);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    string s;
    cin>>s;
    int cnt(0),nb[100010];
    stack<int> st;
    int lft[100010],n(s.size());
    memset(lft,-1,sizeof lft);
    for(int i=0;i<n;++i){
        nb[i] = cnt;
        if(s[i]=='[')++cnt;
        if(s[i]=='[' || s[i]=='(')st.push(i);
        else if(!st.empty() && (s[i]==']' && s[st.top()]=='[' || s[i]==')' && s[st.top()]=='(')){
            lft[i] = st.top();
            st.pop();
        }else {
            while(!st.empty())st.pop();
        }
    }
    int mx(0),l(0),r(-1);
    char ch('0');
    swap(ch,s[0]);
    for(int i=0;i<n;++i){
        if(lft[i]==-1)continue;
        if(lft[i] && lft[lft[i]-1]!=-1){
            lft[i] = lft[lft[i]-1];
        }
        if(mx <= nb[i] - nb[lft[i]]){
            mx = nb[i] - nb[lft[i]];
            r = i;
            l = lft[i];
        }
    }
    swap(ch,s[0]);
    cout<<mx<<endl<<string(s.begin()+l,s.begin()+r+1);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll inf = 1e9+100, N = 2e5+5;
const ll INF = 1e18+100, MOD = 1e9+7;

int main()
{
    ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0),cout<<setprecision(10)<<fixed;
    string s;
    cin>>s;
    vector< int > arr(s.size()+7,0);
    stack< pair< char,int> > st;

    for(int i=0;i<s.size();i++)
    {
        if(s[i]=='(' || s[i]=='[')
        st.push({s[i],i});
        else
        {
            if( st.empty() || ( st.top().first=='(' && s[i]==']' ) || ( st.top().first=='[' && s[i]==')' ) )
            while(!st.empty())
            st.pop();
            else
            {
                pair< char,int > temp=st.top();
                st.pop();
                arr[temp.second]++;
                arr[i+1]--;
            }
        }
    }
    int cnt(0);

    for(int i=0;i<s.size();i++)
    {
        cnt+=arr[i];
        arr[i]=cnt;
    }

    int ans(0),l(1),r(0);
    for(int i=0;i<s.size();i++)
    {
        if(arr[i]==0)
        continue;
        int j=i;
        cnt=0;
        for(;j<s.size()&&arr[j]!=0;j++)
        {
            if(s[j]=='[')
            cnt++;
        }
        if(cnt>ans)
        {
            ans=cnt;
            l=i;
            r=j-1;
        }
        else if(cnt==ans)
        {
            if(r-l+1<j-i)
            {
                l=i;
                r=j-1;
            }
        }
        i=j-1;
    }

    cout<<ans<<"\n";
    for(int i=l;i<=r;i++)
    cout<<s[i];
return 0;
}


#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>
#include <complex>

using namespace std;

typedef pair<int, int> pii;
typedef long long LL;

/*
#ifdef LOCAL
template <typename T>
auto is_printable_impl(int) -> decltype(cout << declval<T &>(), std::true_type{});
template <typename T>
std::false_type is_printable_impl(...);
template <typename T>
using is_printable = decltype(is_printable_impl<T>(0));

template <typename Tuple, size_t N>
struct TuplePrinter;
struct Debug {
    template <typename T>
    typename enable_if<!is_printable<T>::value, Debug &>::type operator<<(
            const T &x) {
        int i = 0;
        for (auto it = begin(x); it != end(x); ++it)
            *this << "[" << i++ << " : " << *it << "] ";
        return *this;
    }
    template <typename T>
    typename enable_if<is_printable<T>::value, Debug &>::type operator<<(
            const T &x) {
        cout << x;
        return *this;
    }
    template <typename T1, typename T2> // pair-printer
    Debug &operator<<(const pair<T1, T2> &p) {
        *this << "{" << p.first << ", " << p.second << "}";
        return *this;
    }
    template <typename... Args>
    Debug &operator<<(const tuple<Args...> &t) {
        cout << "(";
        TuplePrinter<decltype(t), sizeof...(Args) - 1>::print(t);
        cout << ")";
        return *this;
    }
} debug;
template <typename Tuple, size_t N>
struct TuplePrinter {
    static void print(const Tuple &t) {
        TuplePrinter<Tuple, N - 1>::print(t);
        debug << ", " << get<N>(t);
    }
};
template <typename Tuple>
struct TuplePrinter<Tuple, 0> {
    static void print(const Tuple &t) {
        debug << get<0>(t);
    }
};
#define dbg(x) do { debug << #x << " -> " << (x) << '\n'; } while (0)
#else
#define dbg(x) do {} while (0)
#endif
*/

const int INF = 0x3f3f3f3f;
const LL INF64 = 0x3f3f3f3f3f3f3f3f;

const int N = 1e4 + 10;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    string s;
    cin >> s;
    int n = s.size();
    int mx = 0;
    string ans;
    vector<int> v;
    vector<int> cor(n, -1);
    for (int i = 0; i < n; ++i) {
        if (s[i] == '(' || s[i] == '[') {
            v.push_back(i);
        }
        else {
            if (v.empty()) continue;
            if (s[i] == ')') {
                if (s[v.back()] == '(') {
                    int l = v.back();
                    cor[l] = i;
                    v.pop_back();
                }
                else {
                    v.clear();
                }
            }
            else if (s[i] == ']') {
                if (s[v.back()] == '[') {
                    int l = v.back();
                    cor[l] = i;
                    v.pop_back();
                }
                else {
                    v.clear();
                }
            }
            else assert(false);
        }
    }
    for (int i = 0; i < n; ++i) {
        if (cor[i] != -1) {
            int j = i;
            int cnt = 0;
            while (j < n && cor[j] != -1) {
                int r = cor[j];
                for (; j <= r; ++j) cnt += s[j] == '[';
            }
            if (cnt > mx) {
                mx = cnt;
                ans = s.substr(i, j - i);
            }
            i = j - 1;
        }
    }
    cout << mx << '\n';
    cout << ans << '\n';
    return 0;
}


#include <iostream>
#include <stack>
#include <vector>
using namespace std;

int main() {
	string s;
	cin >> s;
	int sz = s.size();
	stack<pair<char, int> > stk;
	vector<bool> seg(sz,0);
	for (int i = 0; i < sz; i++) {
		if (s[i] == '(' || s[i] == '[')
			stk.push( { s[i], i });
		else if (s[i] == ')') {

			if (!stk.empty()&&stk.top().first == '(') {
				seg[i] = 1;
				seg[stk.top().second] = 1;
				stk.pop();
			} else
				stk = stack<pair<char, int> >();
		} else {
			if (!stk.empty()&&stk.top().first == '[') {
				seg[i] = 1;
				seg[stk.top().second] = 1;
				stk.pop();
			} else
				stk = stack<pair<char, int> >();
		}
	}
	int mx = 0, lastone = 0, coun,i;
	string temp, res;
	for (i = 0; i < sz; i++) {
		if (seg[i])
			lastone++;
		else {
			temp = "";
			coun = 0;
			for (int j = i  - lastone; j < i; j++) {
				temp += s[j];
				coun += s[j] == '[';
			}
			if (coun > mx) {
				mx = coun;
				res = temp;
			}
			lastone=0;
		}
	}i--;
	if (seg[i - 1]) {
		temp = "";
		coun = 0;
		for (int j = (i + 1) - lastone; j <=i; j++) {
			temp += s[j];
			coun += s[j] == '[';
		}
		if (coun > mx) {
			mx = coun;
			res = temp;
		}
	}
	cout<<mx<<"\n"<<res;
	return 0;
}


#include <iostream>
#include <stack>
using namespace std;

int main() {
	string s;
	cin >> s;
	int sz = s.size();
	stack<pair<char, int> > stk;
	pair<char, int> top;
	bool seg[sz];
	for (int i = 0; i < sz; i++) {
		if (s[i] == '(' || s[i] == '[')
			stk.push( { s[i], i });
		else if (s[i] == ')') {

			top = stk.top();
			if (top.first == '(') {
				seg[i] = 1;
				seg[top.second] = 1;
				stk.pop();
			} else
				stk = stack<pair<char, int> >();
		} else {
			top = stk.top();
			if (top.first == '[') {
				seg[i] = 1;
				seg[top.second] = 1;
				stk.pop();
			} else
				stk = stack<pair<char, int> >();
		}
	}
	int mx = 0, lastone = 0, coun,i;
	string temp, res;
	for (i = 0; i < sz; i++) {
		if (seg[i])
			lastone++;
		else {
			temp = "";
			coun = 0;
			for (int j = (i + 1) - lastone; j < i; j++) {
				temp += s[i];
				coun += s[i] == '[';
			}
			if (coun > mx) {
				mx = coun;
				res = temp;
			}
		}
	}i--;
	if (seg[i - 1]) {
		temp = "";
		coun = 0;
		for (int j = (i + 1) - lastone; j <=i; j++) {
			temp += s[j];
			coun += s[j] == '[';
		}
		if (coun > mx) {
			mx = coun;
			res = temp;
		}
	}
	cout<<mx<<"\n"<<res;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int OO = 0x3f3f3f3f ,N = 1e5+5 ,mod = 1e9+7 ;
#define read(FILE)  freopen(FILE, "r", stdin);
#define write(FILE) freopen(FILE, "w", stdout);
#define BeatMeIFUCAN ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);

char str[N] ,res[N]={'\0'} ;
stack<int> s ;
vector<pair<int,int>> v ;
int ans=-1 ,a=-1 ,b=-1 ,C[N] ;
bool flag ,arr[N] ;
bool valid(char s1,char s2){
    return (s1=='('&&s2==')')||(s1=='['&&s2==']');
}
void RES(int f,int t){
    res[t+1-f] = '\0' ;
    for(int i=f;i<=t;++i) res[i-f] = str[i] ;
}
int main(){
    scanf("%s",str);
    for(int i=0;str[i]!='\0';++i){
        C[i] = (str[i]=='['?1:0) + (i?C[i-1]:0) ;
        if(str[i]=='('||str[i]=='[') s.push(i) ;
        else if(s.size()&&valid(str[s.top()],str[i])) arr[i]=arr[s.top()]=1 ,s.pop();
        else while(s.size()) s.pop() ;
    }
    for(int i=0;str[i]!='\0';++i){
        if(arr[i]) b=i ,a=(flag?a:i) ,flag = 1 ;
        else if(flag) v.push_back({a,b}) ,flag=0 ;
    }
    if(flag) v.push_back({a,b});
    for(auto ft:v)
        if(C[ft.second]-(ft.first?C[ft.first-1]:0)>ans)
            ans = C[ft.second]-(ft.first?C[ft.first-1]:0) ,RES(ft.first,ft.second) ;
    printf("%d\n%s",max(0,ans),res);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int OO = 0x3f3f3f3f ,N = 1e5+5 ,mod = 1e9+7 ;
#define read(FILE)  freopen(FILE, "r", stdin);
#define write(FILE) freopen(FILE, "w", stdout);
#define BeatMeIFUCAN ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);

char str[N] ,res[N]={'\0'} ;
stack<int> s ;
vector<pair<int,int>> v ;
int ans=-1 ,a=-1 ,b=-1 ,C[N] ;
bool flag ,arr[N] ;
void RES(int f,int t){
    res[t+1-f] = '\0' ;
    for(int i=f;i<=t;++i) res[i-f] = str[i] ;
}
int main(){
    scanf("%s",str);
    for(int i=0;str[i]!='\0';++i){
        C[i] = (str[i]=='['?1:0) + (i?C[i-1]:0) ;
        if(str[i]=='('||str[i]=='[') s.push(i) ;
        else arr[i] = arr[s.top()] = 1 ,s.pop();
    }
    for(int i=0;str[i]!='\0';++i){
        if(arr[i]) b=i ,a=(flag?a:i) ,flag = 1 ;
        else if(flag) v.push_back({a,b}) ,flag=0 ;
    }
    if(flag) v.push_back({a,b});
    for(auto ft:v)
        if(C[ft.second]-(ft.first?C[ft.first-1]:0)>ans)
            ans = C[ft.second]-(ft.first?C[ft.first-1]:0) ,RES(ft.first,ft.second) ;
    printf("%d\n%s",max(0,ans),res);
    return 0;
}


#include <bits/stdc++.h>
#define endn "\n"
#define ll long long
using namespace std;
bool valid[100005];
stack<pair<int, char>> st;
int main()
{
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  string s; cin>>s;
  int n = s.size();

  //check balanced strings
  for(int i=0; i<n; ++i){
      if(s[i] == '[' || s[i] == '(') st.push({i, s[i]});
      else if((s[i] == ']' && st.top().second == '[') || (s[i] == ')' && st.top().second == '(')){
          valid[i] = true;
          valid[st.top().first] = true;
          st.pop();
      }
      else{
          while(!st.empty()) st.pop();
      }
  }

  //count longest valid subsequence
  int mx = 0, idx1 = -1, idx2 = -1;
  for(int i=0; i<n; ++i){
      if(valid[i]){
          int j = i;
          for(; j<n && valid[j]; ++j);
          int tmp = 0;
          for(int k = i; k<j; ++k){
              tmp += s[k] == '[';
          }
          if(tmp > mx){
              mx = tmp;
              idx1 = i;
              idx2 = j;
          }
      }
  }

  if(mx){
      cout<<mx<<endn;
      for(int i=idx1; i<idx2; ++i){
          cout<<s[i];
      }
      cout<<endn;
  }
  else cout<<0<<endn;
  return 0;
}


#include <bits/stdc++.h>
#define endn "\n"
#define ll long long
using namespace std;
char s[100005];
bool valid[100005];
stack<pair<int, char>> st;
int main()
{
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  scanf("%s", s);
  int n = strlen(s);

  //check balanced strings
  for(int i=0; i<n; ++i){
      if(s[i] == '[' || s[i] == '(') st.push({i, s[i]});
      else if(!st.empty() && ((s[i] == ']' && st.top().second == '[') || (s[i] == ')' && st.top().second == '('))){
          valid[i] = true;
          valid[st.top().first] = true;
          st.pop();
      }
      else{
          while(!st.empty()) st.pop();
      }
  }

  //count longest valid subsequence
  int mx = 0, idx1 = -1, idx2 = -1;
   for(int i = 0; i < n; ++i)
    if(valid[i] == 1) {
      int j = i;
      for(; j < n && valid[j] == 1; ++j);
      int cnt = 0;
      for(int k = i; k < j; ++k)
        cnt += s[k] == '[';
      if(cnt > mx)
        mx = cnt, idx1 = i, idx2 = j;
      i = j;
    }

  if(mx){
      printf("%d\n", mx);
      for(int i=idx1; i<idx2; ++i){
          putchar(s[i]);
      }
      puts("");
  }
  else puts("0\n");
  return 0;
}


#include <bits/stdc++.h>
#define endn "\n"
#define ll long long
using namespace std;
bool valid[100005];
stack<pair<int, char>> st;
int main()
{
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  string s; cin>>s;
  int n = s.size();

  //check balanced strings
  for(int i=0; i<n; ++i){
      if(s[i] == '[' || s[i] == '(') st.push({i, s[i]});
      else if(!st.empty() && ((s[i] == ']' && st.top().second == '[') || (s[i] == ')' && st.top().second == '('))){
          valid[i] = true;
          valid[st.top().first] = true;
          st.pop();
      }
      else{
          while(!st.empty()) st.pop();
      }
  }

  //count longest valid subsequence
  int mx = 0, idx1 = -1, idx2 = -1;
  for(int i=0; i<n; ++i){
      if(valid[i]){
          int j = i;
          for(; j<n && valid[j]; ++j);
          int tmp = 0;
          for(int k = i; k<j; ++k){
              tmp += s[k] == '[';
          }
          if(tmp > mx){
              mx = tmp;
              idx1 = i;
              idx2 = j;
          }
      }
  }

  if(mx){
      cout<<mx<<endn;
      for(int i=idx1; i<idx2; ++i){
          cout<<s[i];
      }
      cout<<endn;
  }
  else cout<<0<<endn;
  return 0;
}


#include <bits/stdc++.h>
#define endn "\n"
#define ll long long
using namespace std;
char s[100005];
bool valid[100005];
stack<pair<int, char>> st;
int main()
{
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  scanf("%s", s);
  int n = strlen(s);

  //check balanced strings
  for(int i=0; i<n; ++i){
      if(s[i] == '[' || s[i] == '(') st.push({i, s[i]});
      else if(!st.empty() && ((s[i] == ']' && st.top().second == '[') || (s[i] == ')' && st.top().second == '('))){
          valid[i] = true;
          valid[st.top().first] = true;
          st.pop();
      }
      else{
          while(!st.empty()) st.pop();
      }
  }

  //count longest valid subsequence
  int mx = 0, idx1 = -1, idx2 = -1;
  for(int i=0; i<n; ++i){
      if(valid[i]){
          int j = i;
          for(; j<n && valid[j]; ++j);
          int tmp = 0;
          for(int k = i; k<j; ++k){
              tmp += s[k] == '[';
          }
          if(tmp > mx){
              mx = tmp;
              idx1 = i;
              idx2 = j;
          }
      }
  }

  if(mx){
      printf("%d\n", mx);
      for(int i=idx1; i<idx2; ++i){
          putchar(s[i]);
      }
      puts("");
  }
  else puts("0\n");
  return 0;
}


#include <bits/stdc++.h>
#define endn "\n"
#define ll long long
using namespace std;
char s[100005];
bool valid[100005];
stack<pair<int, char>> st;
int main()
{
  scanf("%s", s);
  int n = strlen(s);

  //check balanced strings
  for(int i=0; i<n; ++i){
      if(s[i] == '[' || s[i] == '(') st.push({i, s[i]});
      else if(!st.empty() && ((s[i] == ']' && st.top().second == '[') || (s[i] == ')' && st.top().second == '('))){
          valid[i] = true;
          valid[st.top().first] = true;
          st.pop();
      }
      else{
          while(!st.empty()) st.pop();
      }
  }

  //count longest valid subsequence
  int mx = 0, idx1 = -1, idx2 = -1;
  for(int i=0; i<n; ++i){
      if(valid[i]){
          int j = i;
          for(; j<n && valid[j]; ++j);
          int tmp = 0;
          for(int k = i; k<j; ++k){
              tmp += s[k] == '[';
          }
          if(tmp > mx){
              mx = tmp;
              idx1 = i;
              idx2 = j;
          }
      }
  }

  if(mx){
      printf("%d\n", mx);
      for(int i=idx1; i<idx2; ++i){
          putchar(s[i]);
      }
      puts("");
  }
  else puts("0\n");
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int const N = 1e5 + 10;
char s[N];
int n, cs[N];
stack<pair<int, int> > st;

int main() {
  scanf("%s", s);
  n = strlen(s);

  for(int i = 0; i < n; ++i) {
    if(s[i] == '(' || s[i] == '[')
      st.push({i, s[i]});
    else {
      if(!st.empty() &&
         ((s[i] == ']' && '[' == st.top().second) ||
          (s[i] == ')' && '(' == st.top().second))) {
        cs[st.top().first] = 1;
        cs[i] = 1;
        st.pop();
      } else {
        while(!st.empty())
          st.pop();
      }
    }
  }

  int res = 0, start = -1, end = -1;
  for(int i = 0; i < n; ++i)
    if(cs[i] == 1) {
      int j = i;
      for(; j < n && cs[j] == 1; ++j);
      int cnt = 0;
      for(int k = i; k < j; ++k)
        cnt += s[k] == '[';
      if(cnt > res)
        res = cnt, start = i, end = j;
      i = j;
    }

  if(res > 0) {
    printf("%d\n", res);
    for(int i = start; i < end; ++i)
      putchar(s[i]);
    puts("");
  } else
    puts("0\n");

  return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<set>
#include<map>
#include<iterator>
#include<stack>
#include<queue>
#include<utility>
#include<functional>
#include<bitset>
#include<numeric>
#include<cstring>
#include<tuple>
using namespace std;

int n, ans = 0, cur = 0, j = -1, start = 0, len = 0, curlen = 0;
string s;
stack< int > st;
vector< int > dp( 1e5 + 5, 0 );

int main( void )
{
	ios :: sync_with_stdio( 0 );
	cin.tie( 0 );

	cin >> s;
	n = s.length();
	for( int i = 0; i <= n - 1; ++i )
	{
		if( s[ i ] == '(' || s[ i ] == '[' )
		{
			st.push( i );
		}
		else if( !st.empty() )
		{
			if( s[ i ] == ')' && s[ st.top() ] == '(' )
			{
				dp[ i ] = 1;
				dp[ st.top() ] = 1;
				st.pop();
			}
			else if( s[ i ] == ']' && s[ st.top() ] == '[' )
			{
				dp[ i ] = 2;
				dp[ st.top() ] = 2;
				st.pop();
			}
			else
			{
				while( !st.empty() )
				{
					st.pop();
				}
			}
		}
	}
	for( int i = 0; i <= n; ++i )
	{
		if( dp[ i ] )
		{
			++curlen;
			if( dp[ i ] == 2 )
			{
				++cur;
			}
		}
		else
		{
			if( ans < cur )
			{
				ans = cur;
				start = j + 1;
				len = curlen;
			}
			cur = 0;
			j = i;
			curlen = 0;
		}
	}
	cout << ans / 2 << "\n";
	cout << s.substr( start, len ) << "\n";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long int
#define absdiff(a,b) (a>b)?a-b:b-a
#define debug cout<<"debug";
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define fast  ios_base::sync_with_stdio(false);cin.tie(0);
#define MOD 1000000007
#define ll_MAX 19223372036859
#define endl "\n"
ll fast_expo(ll x,ll p){if(p==0) return 1;
else if(p%2==0){ ll t=fast_expo(x,p/2)%MOD;return (t*t)%MOD;} else return (x*(fast_expo(x,p-1))%MOD)%MOD;}
ll gcd(ll a,ll b){if(a==0||b==0) return a+b;  else return gcd(b%a,a); }

int main()
{
    ll t;t=1;
    // cin>>t;
    for(ll x=0;x<t;++x){
        string str;cin>>str;
        ll cnt[str.size()]{};
        if(str[0]=='[') cnt[0]++;

        for(ll i=1;i<str.size();++i){
            if(str[i]=='['){
                cnt[i]=cnt[i-1]+1;
            }
            else cnt[i]=cnt[i-1];
        }

        vector<pair<ll,ll>> vect;

        stack<pair<char,ll>> S;
        ll ans=0;
        for(ll i=0;i<str.size();++i){
            if(str[i]==']'||str[i]==')'){
                if(S.size()>0){
                    pair<char,ll> temp=S.top();
                    if(temp.ff=='['&&str[i]==']'){
                        S.pop();
                        if(vect.size()>0){
                            if(vect[vect.size()-1].ff>=temp.ss) vect.pop_back();
                        }

                        if(vect.size()>0&&vect[vect.size()-1].ss==temp.ss-1){
                            pair<ll,ll> last=vect[vect.size()-1];
                            vect.pop_back();
                            vect.pb(mp(last.ff,i));
                        }
                        else vect.pb(mp(temp.ss,i));
                    }
                    else if(temp.ff=='('&&str[i]==')'){
                        S.pop();
                        if(vect.size()>0){
                            if(vect[vect.size()-1].ff>=temp.ss) vect.pop_back();
                        }

                        if(vect.size()>0&&vect[vect.size()-1].ss==temp.ss-1){
                            pair<ll,ll> last=vect[vect.size()-1];
                            vect.pop_back();
                            vect.pb(mp(last.ff,i));
                        }
                        else vect.pb(mp(temp.ss,i));
                    }
                    else {
                        while(S.size()) S.pop();
                    }
                }
            }
            else S.push(mp(str[i],i));
        }

        ll l=0,r=-1;
        for(ll i=0;i<vect.size();++i){
            if(vect[i].ff==0) {
                ans=max(ans,cnt[vect[i].ss]);
                l=0;r=vect[i].ss;
            }
            else {
                ans=max(ans,cnt[vect[i].ss]-cnt[vect[i].ff-1]);
                l=vect[i].ff;r=vect[i].ss;
            }
        }

        cout<<ans<<endl;
        for(ll i=l;i<=r;++i) cout<<str[i];
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long int
#define absdiff(a,b) (a>b)?a-b:b-a
#define debug cout<<"debug";
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define fast  ios_base::sync_with_stdio(false);cin.tie(0);
#define MOD 1000000007
#define ll_MAX 19223372036859
#define endl "\n"
ll fast_expo(ll x,ll p){if(p==0) return 1;
else if(p%2==0){ ll t=fast_expo(x,p/2)%MOD;return (t*t)%MOD;} else return (x*(fast_expo(x,p-1))%MOD)%MOD;}
ll gcd(ll a,ll b){if(a==0||b==0) return a+b;  else return gcd(b%a,a); }

int main()
{
    ll t;t=1;
    // cin>>t;
    for(ll x=0;x<t;++x){
        string str;cin>>str;
        ll cnt[str.size()]{};
        if(str[0]=='[') cnt[0]++;

        for(ll i=1;i<str.size();++i){
            if(str[i]=='['){
                cnt[i]=cnt[i-1]+1;
            }
            else cnt[i]=cnt[i-1];
        }

        vector<pair<ll,ll>> vect;

        stack<pair<char,ll>> S;
        ll ans=0;
        for(ll i=0;i<str.size();++i){
            if(str[i]==']'||str[i]==')'){
                if(S.size()>0){
                    pair<char,ll> temp=S.top();
                    if(temp.ff=='['&&str[i]==']'){
                        S.pop();
                        if(vect.size()>0){
                            if(vect[vect.size()-1].ff>=temp.ss) vect.pop_back();
                        }

                        if(vect.size()>0&&vect[vect.size()-1].ss==temp.ss-1){
                            pair<ll,ll> last=vect[vect.size()-1];
                            vect.pop_back();
                            vect.pb(mp(last.ff,i));
                        }
                        else vect.pb(mp(temp.ss,i));
                    }
                    else if(temp.ff=='('&&str[i]==')'){
                        S.pop();
                        if(vect.size()>0){
                            if(vect[vect.size()-1].ff>=temp.ss) vect.pop_back();
                        }

                        if(vect.size()>0&&vect[vect.size()-1].ss==temp.ss-1){
                            pair<ll,ll> last=vect[vect.size()-1];
                            vect.pop_back();
                            vect.pb(mp(last.ff,i));
                        }
                        else vect.pb(mp(temp.ss,i));
                    }
                    else {
                        while(S.size()) S.pop();
                    }
                }
            }
            else S.push(mp(str[i],i));
        }

        ll l=0,r=-1;
        for(ll i=0;i<vect.size();++i){
            if(vect[i].ff==0&&ans<cnt[vect[i].ss]){
                ans=cnt[vect[i].ss];
                l=0;r=vect[i].ss;
            }
            else if(ans<cnt[vect[i].ss]-cnt[vect[i].ff-1]){
                ans=cnt[vect[i].ss]-cnt[vect[i].ff-1];
                l=vect[i].ff;r=vect[i].ss;
            }
        }

        cout<<ans<<endl;
        for(ll i=l;i<=r;++i) cout<<str[i];
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long int
#define absdiff(a,b) (a>b)?a-b:b-a
#define debug cout<<"debug";
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define fast  ios_base::sync_with_stdio(false);cin.tie(0);
#define MOD 1000000007
#define ll_MAX 19223372036859
#define endl "\n"
ll fast_expo(ll x,ll p){if(p==0) return 1;
else if(p%2==0){ ll t=fast_expo(x,p/2)%MOD;return (t*t)%MOD;} else return (x*(fast_expo(x,p-1))%MOD)%MOD;}
ll gcd(ll a,ll b){if(a==0||b==0) return a+b;  else return gcd(b%a,a); }

int main()
{
    ll t;t=1;
    // cin>>t;
    for(ll x=0;x<t;++x){
        string str;cin>>str;
        ll cnt[str.size()]{};
        if(str[0]=='[') cnt[0]++;

        for(ll i=1;i<str.size();++i){
            if(str[i]=='['){
                cnt[i]=cnt[i-1]+1;
            }
            else cnt[i]=cnt[i-1];
        }

        vector<pair<ll,ll>> vect;

        stack<pair<char,ll>> S;
        ll ans=0;
        for(ll i=0;i<str.size();++i){
            if(str[i]==']'||str[i]==')'){
                if(S.size()>0){
                    pair<char,ll> temp=S.top();
                    if(temp.ff=='['&&str[i]==']'){
                        S.pop();
                        if(vect.size()>0){
                            if(vect[vect.size()-1].ff>=temp.ss) vect.pop_back();
                        }

                        if(vect.size()>0&&vect[vect.size()-1].ss==temp.ss-1){
                            pair<ll,ll> last=vect[vect.size()-1];
                            vect.pop_back();
                            vect.pb(mp(last.ff,i));
                        }
                        else vect.pb(mp(temp.ss,i));
                    }
                    else if(temp.ff=='('&&str[i]==')'){
                        S.pop();
                        if(vect.size()>0){
                            if(vect[vect.size()-1].ff>=temp.ss) vect.pop_back();
                        }

                        if(vect.size()>0&&vect[vect.size()-1].ss==temp.ss-1){
                            pair<ll,ll> last=vect[vect.size()-1];
                            vect.pop_back();
                            vect.pb(mp(last.ff,i));
                        }
                        else vect.pb(mp(temp.ss,i));
                    }
                    else {
                        while(S.size()) S.pop();
                    }
                }
            }
            else S.push(mp(str[i],i));
        }

        for(ll i=0;i<vect.size();++i){
            if(vect[i].ff==0) ans=max(ans,cnt[vect[i].ss]);
            else ans=max(ans,cnt[vect[i].ss]-cnt[vect[i].ff-1]);
        }
        cout<<ans;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int MN = 1e5+5;
stack<pair<char,int>> st;
int mat[MN], i, j, vis[MN], dp[MN], psa[MN], ans;
string s; int ind;

void fnd(int n){
	if(vis[n]||mat[n]<n) return;
	vis[n] = 1; fnd(mat[n]+1);
	dp[n]=mat[n]-n+1+dp[mat[n]+1];
}

int sum(int x,int y){
	return (x)?psa[y]-psa[x-1]:psa[y];
}

int main(){
	cin >> s;
	for(i=0;i<s.size();i++){
		char ch = s[i];
		if(ch==')'){
			if(!st.size()||st.top().first!='('){
				// reeeeeeeeeeeeeeeeeeeeeeeee
				while(st.size()){
					mat[st.top().second]=-1;
					st.pop();
				}
				mat[i] = -1;
			}
			else{
				mat[st.top().second]=i;
				st.pop();
			}
		}
		else if(ch==']'){
			if(!st.size()||st.top().first!='['){
				while(st.size()){
					mat[st.top().second]=-1;
					st.pop();
				}
				mat[i]=-1;
			}
			else{
				mat[st.top().second]=i;
				st.pop();
			}
		}
		else st.push({ch,i});
		psa[i]=psa[i-1]+(ch=='[');
	}
	while(st.size()){
		mat[st.top().second]=-1;
		st.pop();
	}
	for(i=0;i<s.size();i++){
		fnd(i); if(!dp[i]) continue;
		if(sum(i,i+dp[i]-1)>ans){
			ans = sum(i,i+dp[i]-1);
			ind = i;
		}
	}
	if(ans)	printf("%d\n%s\n",ans,s.substr(ind,dp[ind]).c_str());
	else printf("0\n");
	getchar(); getchar();
	return 0;
}


/*
    Author - linpaws07
*/
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define inf (int)1e9
#define f first
#define s second
#define eps 1e-9
#define PI 3.1415926535897932384626433832795
#define scd(t) scanf("%d",&t)
#define sclld(t) scanf("%lld",&t)
#define scc(t) scanf("%c",&t)
#define scs(t) scanf("%s",t)
#define scf(t) scanf("%f",&t)
#define sclf(t) scanf("%lf",&t)
#define memst(a, b) memset(a, (b), sizeof(a))
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<vi> vvi;
typedef map<int,int> mpii;
typedef set<int> seti;
typedef multiset<int> mseti;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define rp(i,n) for(int i=0;i<(n);i++)
#define fr(i,a,b) for(int i=(a);i<(b);i++)
#define fre(i,a,b) for(int i=(a);i<=(b);i++)
#define frd(i,a,b) for(int i=(a);i>=(b);i--)
inline bool eq(double a, double b) { return fabs(a-b) < 1e-9; }
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n>>b)&1; }
inline void set_bit(int & n, int b) { n |= two(b); }
inline void unset_bit(int & n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) { int res = 0; while(n && ++res) n-=n&(-n); return res; }
auto start = high_resolution_clock::now();
inline void measure();

int main(){
    FAST
    //Do awesome things here
    string s;
    cin >> s;
    int st = 0, num = 0, nd;
    stack<pair<char, int> > ch;
    ch.push({s[0], 0});
    int cnt[100002] = {0};
    cnt[0] = (s[0] == '[' ? 1 : 0);
    fr(i, 1, s.length()){
        cnt[i] = cnt[i - 1] + (s[i] == '[' ? 1 : 0);
    }
    fr(i, 1, s.length()){
        if(ch.empty()){
            ch.push({s[i], i});
        }
        else{
            if(s[i] == ']' && ch.top().f == '[' ||
                s[i] == ')' && ch.top().f == '('){
                    int x, z = i;
                    if(ch.empty()) x = 0;
                    else x = ch.top().s;
                    ch.pop();
                    if(num < cnt[z] - cnt[x - 1 < 0 ? 0 : x - 1]){
                        num = cnt[z] - cnt[x - 1 < 0 ? 0 : x - 1];
                        st = x;
                        nd = z;
                    }
            }
            else{
                ch.push({s[i], i});
            }
        }
    }
    cout << num << endl;
    if(num){
        fre(i, st, nd){
            cout << s[i];
        }
        cout << endl;
    }
    return 0;
}

inline void measure(){
    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<milliseconds>(stop - start);
    cout << duration.count() << endl;
}


/*
    Author - linpaws07
*/
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define inf (int)1e9
#define f first
#define s second
#define eps 1e-9
#define PI 3.1415926535897932384626433832795
#define scd(t) scanf("%d",&t)
#define sclld(t) scanf("%lld",&t)
#define scc(t) scanf("%c",&t)
#define scs(t) scanf("%s",t)
#define scf(t) scanf("%f",&t)
#define sclf(t) scanf("%lf",&t)
#define memst(a, b) memset(a, (b), sizeof(a))
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<vi> vvi;
typedef map<int,int> mpii;
typedef set<int> seti;
typedef multiset<int> mseti;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define rp(i,n) for(int i=0;i<(n);i++)
#define fr(i,a,b) for(int i=(a);i<(b);i++)
#define fre(i,a,b) for(int i=(a);i<=(b);i++)
#define frd(i,a,b) for(int i=(a);i>=(b);i--)
inline bool eq(double a, double b) { return fabs(a-b) < 1e-9; }
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n>>b)&1; }
inline void set_bit(int & n, int b) { n |= two(b); }
inline void unset_bit(int & n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) { int res = 0; while(n && ++res) n-=n&(-n); return res; }
auto start = high_resolution_clock::now();
inline void measure();

int main(){
    FAST
    //Do awesome things here
    string s;
    cin >> s;
    int st = 0, num = 0, nd;
    stack<pair<char, int> > ch;
    ch.push({s[0], 0});
    int cnt[100002] = {0};
    cnt[0] = (s[0] == '[' ? 1 : 0);
    fr(i, 1, s.length()){
        cnt[i] = cnt[i - 1] + (s[i] == '[' ? 1 : 0);
    }
    fr(i, 1, s.length()){
        if(ch.empty()){
            ch.push({s[i], i});
        }
        else{
            if(s[i] == ']' && ch.top().f == '[' ||
                s[i] == ')' && ch.top().f == '('){
                    int x, z = i, y;
                    ch.pop();
                    if(ch.empty()) x = 0;
                    else x = ch.top().s + 1;
                    if(x == 0)  y = cnt[z];
                    else    y = cnt[z] - cnt[x - 1];
                    if(num < y){
                        num = y;
                        st = x;
                        nd = z;
                    }
            }
            else{
                ch.push({s[i], i});
            }
        }
    }
    cout << num << endl;
    if(num){
        fre(i, st, nd){
            cout << s[i];
        }
        cout << endl;
    }
    return 0;
}

inline void measure(){
    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<milliseconds>(stop - start);
    cout << duration.count() << endl;
}


/*
    Author - linpaws07
*/
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define inf (int)1e9
#define f first
#define s second
#define eps 1e-9
#define PI 3.1415926535897932384626433832795
#define scd(t) scanf("%d",&t)
#define sclld(t) scanf("%lld",&t)
#define scc(t) scanf("%c",&t)
#define scs(t) scanf("%s",t)
#define scf(t) scanf("%f",&t)
#define sclf(t) scanf("%lf",&t)
#define memst(a, b) memset(a, (b), sizeof(a))
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<vi> vvi;
typedef map<int,int> mpii;
typedef set<int> seti;
typedef multiset<int> mseti;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define rp(i,n) for(int i=0;i<(n);i++)
#define fr(i,a,b) for(int i=(a);i<(b);i++)
#define fre(i,a,b) for(int i=(a);i<=(b);i++)
#define frd(i,a,b) for(int i=(a);i>=(b);i--)
inline bool eq(double a, double b) { return fabs(a-b) < 1e-9; }
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n>>b)&1; }
inline void set_bit(int & n, int b) { n |= two(b); }
inline void unset_bit(int & n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) { int res = 0; while(n && ++res) n-=n&(-n); return res; }
auto start = high_resolution_clock::now();
inline void measure();

int main(){
    FAST
    //Do awesome things here
    string s;
    cin >> s;
    int st = 0, num = 0, nd;
    stack<pair<char, int> > ch;
    ch.push({s[0], 0});
    int cnt[100002] = {0};
    cnt[0] = (s[0] == '[' ? 1 : 0);
    fr(i, 1, s.length()){
        cnt[i] = cnt[i - 1] + (s[i] == '[' ? 1 : 0);
    }
    fr(i, 1, s.length()){
        if(ch.empty()){
            ch.push({s[i], i});
        }
        else{
            if(s[i] == ']' && ch.top().f == '[' ||
                s[i] == ')' && ch.top().f == '('){
                    int x, z = i;
                    ch.pop();
                    if(ch.empty()) x = 0;
                    else x = ch.top().s + 1;
                    if(num < cnt[z] - cnt[x - 1 < 0 ? 0 : x - 1]){
                        num = cnt[z] - cnt[x - 1 < 0 ? 0 : x - 1];
                        st = x;
                        nd = z;
                    }
            }
            else{
                ch.push({s[i], i});
            }
        }
    }
    cout << num << endl;
    if(num){
        fre(i, st, nd){
            cout << s[i];
        }
        cout << endl;
    }
    return 0;
}

inline void measure(){
    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<milliseconds>(stop - start);
    cout << duration.count() << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m,x,y,z;
string s,s1,s2;
ll mem[200000];
vector<ll>v;
vector<pair<int,string>>vv;
int main()
{
    cin>>s;
    for(int i=0;i<s.size();i++)
    {
        if(s[i]=='['||s[i]=='(')v.push_back(i);
        else if(v.size()==0&&(s[i]==')'||s[i]==']')) continue;
        else if(s[i]==']')
        {
            if(s[v[v.size()-1]]=='(')
                {v.clear();}
            else
            {
                mem[v[v.size()-1]]=i;
                v.pop_back();
            }
        }
        else
        {
            if(s[v[v.size()-1]]=='[')
                {v.clear();}
            else
            {
                mem[v[v.size()-1]]=i;
                v.pop_back();
            }
        }

    }
    for(int i=0;i<s.size();i++)
    {
        if(mem[i]==0)
        {
            vv.push_back({x,s1});
            s1="";
            x=0;
        }
        if(mem[i]>0)
        {
            for(int q=i;q<=mem[i];q++)
            {
                if(s[q]==']')x++;
                 s1+=s[q];
            }
             // cout<<i<<" "<<mem[i]<<endl;
            i=mem[i];
        }
    }
     vv.push_back({x,s1});
    sort(vv.begin(),vv.end());
    cout<<vv[vv.size()-1].first<<endl;
    cout<<vv[vv.size()-1].second<<endl;

    return 0;
}


#include <iostream>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

string s;
int t, ans, l, r;
int st[100100], cnt[100100];

int main () {
    cin >> s;
    s = s + 's';
    for ( int i = s.size(); i >= 1; i--) {
        s[i] = s[i - 1];
    }
    s[0] = ' ';
    t = 1;
    for ( int i = 1; i < s.size(); i++) {
        if ( s[i] == '[' ) {
            cnt[i] = cnt[i - 1] + 1;
        }
        else {
            cnt[i] = cnt[i - 1];
        }
        if ( s[i] == '[' || s[i] == '(' ) {
                st[t] = i; t++;
        }
        else {
            if ( s[i] == ']' && s[i - 1] != '[' || s[i] == '(' && s[i - 1] != ')' ) {
                t = 0; st[t] = i; t++;
            }
            else {
                t--;
                ans = max(ans, cnt[i] - cnt[st[t - 1]]);
                if ( ans == cnt[i] - cnt[st[t - 1]] ) {
                    r = i; l = st[t - 1];
                }
            }
        }
    }
    cout << ans << endl;
    for ( int i = l + 1; i <= r; i++) {
        cout << s[i];
    }
}


import math

class Read:
    @staticmethod
    def string():
        return input()

    @staticmethod
    def int():
        return int(input())

    @staticmethod
    def list(sep=' '):
        return input().split(sep)

    @staticmethod
    def list_int(sep=' '):
        return list(map(int, input().split(sep)))

def solve():
    a, b = Read.list_int()
    print('YES' if (a + b) % 2 == 0 else 'NO')

# query_count = 1
query_count = Read.int()
while query_count:
    query_count -= 1
    solve()


import math

class Read:
    @staticmethod
    def string():
        return input()

    @staticmethod
    def int():
        return int(input())

    @staticmethod
    def list(sep=' '):
        return input().split(sep)

    @staticmethod
    def list_int(sep=' '):
        return list(map(int, input().split(sep)))

def solve():
    a, b = Read.list_int()
    print('YES' if (a + b) % 3 == 0 else 'NO')

# query_count = 1
query_count = Read.int()
while query_count:
    query_count -= 1
    solve()


import math

class Read:
    @staticmethod
    def string():
        return input()

    @staticmethod
    def int():
        return int(input())

    @staticmethod
    def list(sep=' '):
        return input().split(sep)

    @staticmethod
    def list_int(sep=' '):
        return list(map(int, input().split(sep)))

def solve():
    a, b = Read.list_int()
    if (a + b) % 3 == 0:
        if math.fabs(a - b) <= min(a, b):
            print('YES')
            return
    print('NO')

# query_count = 1
query_count = Read.int()
while query_count:
    query_count -= 1
    solve()


for _ in range(int(input())):
	a , b = map(int , input().split())
	if (2*a - b) % 3 == 0 or (2*b-a)%3 == 0:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	a , b = map(int , input().split())

	if max(a,b) == 2*min(a,b):
		print('YES')
	else:
		if (a-1) == 0  and  (b-2) == 0:
			print('YES')
		elif a - 2 == 0 and b -  1 == 0:
			print('YES')
		elif (a - 2*b) % 3 == 0 or (2*a - b)%3 == 0:
			print('YES')
		else:
			print('NO')


import math
for _ in range(int(input())):
	a , b = map(int , input().split())
	flag = False
	if (2*a - b)% 3 == 0:
		y = (2*a - b)/3
		x = a - 2*y
		if math.floor(x) == math.ceil(x) and x >=0 and y >= 0:
			print("Yes")
			flag = True

	if flag != True:
		if (2*b - a)% 3 == 0:
			y = (2*b - a)/3
			x = b - 2*y
			if math.floor(x) == math.ceil(x) and x >=0 and y >= 0:
				print("Yes")
				flag = True

	if flag != True:
		print('No')


n = int(input())
for _ in range(n):
    a, b = list(map(int, input().split()))
    while(a>0)and(b>0):
        ta = min(a,b)
        tb = max(a,b)
        tmp = ta//2
        while(tb-2*tmp < 0):
            tmp-=1
        if tmp == 0:
            tmp = 1
        if a > b:
            a-=2*tmp
            b-=tmp
        else:
            a-=tmp
            b-=2*tmp
    if a == 0 and b == 0:
        print("YES")
    else:
        print("NO")


n = int(input())
for _ in range(n):
    a, b = list(map(int, input().split()))
    ta = max(a,b)
    tb = min(a,b)
    tmpn = tb - ta//2
    if tmpn < 0:
        print("NO")
        continue
    tmpx = ta - (ta//2)*2
    if tmpx == 0:
        print("NO")
        continue
    if tmpn == 0 and tmpx == 0:
        print("YES")
        continue
    if tmpn == tmpx:
        print("NO")
        continue
    if (tmpn%3 == 1)and(tmpx%3 == 2):
        print("YES")
        continue
    if (tmpx%3 == 1)and(tmpn%3 == 2):
        print("YES")
        continue


n = int(input())
for _ in range(n):
    a, b = list(map(int, input().split()))
    ta = max(a,b)
    tb = min(a,b)
    tmpn = tb - ta//2
    if tmpn < 0:
        print("NO")
        continue
    tmpx = ta - (ta//2)*2
    if tmpn == 0 and tmpx == 0:
        print("YES")
        continue
    if tmpn == tmpx:
        print("NO")
        continue
    if (tmpn%3 == 1)and(tmpx%3 == 2):
        print("YES")
        continue
    if (tmpx%3 == 1)and(tmpn%3 == 2):
        print("YES")
        continue
    print("NO")


n = int(input())
for _ in range(n):
    a, b = list(map(int, input().split()))
    while(a>0)and(b>0):
        tmp = min(a,b)
        tmp = tmp//2
        if tmp == 0:
            tmp = 1
        if a > b:
            a-=2*tmp
            b-=tmp
        else:
            a-=tmp
            b-=2*tmp
    if a == 0 and b == 0:
        print("YES")
    else:
        print("NO")


n = int(input())
for _ in range(n):
    a, b = list(map(int, input().split()))
    ta = max(a,b)
    tb = min(a,b)
    if (ta//2<=tb):
        if (ta%3 == 1)and(tb%3 == 2):
            print("YES")
            continue
        if (tb%3 == 1)and(ta%3 == 2):
            print("YES")
            continue
        if (ta%3 == 0)and(tb%3 == 0):
            print("YES")
            continue
    print("NO")


n = int(input())
for _ in range(n):
    a, b = list(map(int, input().split()))
    ta = max(a,b)
    tb = min(a,b)
    tmpn = tb - ta//2
    if tmpn < 0:
        print("NO")
        continue
    tmpx = ta - (ta//2)*2
    if tmpn == 0 and tmpx == 0:
        print("YES")
        continue
    if (tmpn%3 == 1)and(tmpx%3 == 2):
        print("YES")
        continue
    if (tmpx%3 == 1)and(tmpn%3 == 2):
        print("YES")
        continue
    print("NO")


n = int(input())
for _ in range(n):
    a, b = list(map(int, input().split()))
    ta = max(a,b)
    tb = min(a,b)
    tmpn = tb - ta//2
    if tmpn < 0:
        print("NO")
        continue
    tmpx = ta - (ta//2)*2
    if tmpn == 0 and tmpx == 0:
        print("YES")
        continue
    if tmpn == tmpx:
        print("NO")
        continue
    if (min(tmpx, tmpn)%3 == 1)and(max(tmpx, tmpn)%3 == 2):
        print("YES")
        continue


n = int(input())
for _ in range(n):
    a, b = list(map(int, input().split()))
    ta = max(a,b)
    tb = min(a,b)
    tmpn = tb - ta//2
    if tmpn < 0:
        print("NO")
        continue
    tmpx = ta - (ta//2)*2
    print(tmpn, tmpx)
    if tmpn == 0 and tmpx == 0:
        print("YES")
        continue
    if (tmpn%3 == 1)and(tmpx%3 == 2):
        print("YES")
        continue
    if (tmpx%3 == 1)and(tmpn%3 == 2):
        print("YES")
        continue
    if (tmpx%3 == 0)and(tmpn%3 == 0):
        print("YES")
        continue
    print("NO")


n = int (input())
for i in range(n):
    a,b = [int(x) for x in input().split()]
    if a>b:
        tmp=a
        a=b
        b=tmp
    if (a==0) and (b==0):
        print ('YES')
    elif (a==0):
        print ('NO')
    elif ((a+b)%3==0) and (b//a <=2):
        print ('YES')
    else:
        print ('NO')


n = int (input())
for i in range(n):
    a,b = [int(x) for x in input().split()]
    if a>b:
        tmp=a
        a=b
        b=tmp
    if (a==0) and (b==0):
        print ('YES')
    elif (a==0) or (b > 2*a):
        print ('NO')
    elif ((a+b)%3==0):
        print ('YES')
    else:
        print ('NO')


n = int (input())
for i in range(n):
    a,b = [int(x) for x in input().split()]
    if a>b:
        tmp=a
        a=b
        b=tmp
    if (a==0) and (b==0):
        print ('NO')
    elif (a==0):
        print ('NO')
    elif ((a+b)%3==0) and (b/a <=2):
        print ('YES')
    else:
        print ('NO')


n = int (input())
for i in range(n):
    a,b = [int(x) for x in input().split()]
    if a>b:
        tmp=a
        a=b
        b=tmp
    if (a==0) and (b==0):
        print ('NO')
    elif (a==0):
        print ('NO')
    elif ((a+b)%3==0) and (b//a <=2):
        print ('YES')
    else:
        print ('NO')


l = []
for i in range(int(input())):
	a = input().split()
	if (int(a[0])+int(a[1]))%3==0 and abs(int(a[0])-int(a[1]))<=min(int(a[0]),int(a[1])):
		print("YES")
	else:
		print("NO")


for i in range(int(input())):
	a = input().split()
	b = int(a[1])
	a = int(a[0])
	if a==0 and b==0:
		print("YES")
	if max(a,b) > 2*min(a,b) or a==b:
		print("NO")
	else:
		print("YES")


t=int(input())
for i in range (0,t):
    x,y=map(int,input().split())
    if x==0 and y==0:
        print('YES')
    elif (x==0 and y!=0) or (y==0 and x!=0):
        print('NO')
    else:
        if  max(x/y,y/x)>2:
            print('NO')
        else:
            if (x+y)%3==0:
                print('YES')
            else:
                print('NO')


t=int(input())
for i in range (0,t):
    x,y=map(int,input().split())
    if x==0 and y==0:
        print('YES')
    elif (x==0 and y!=0) or (y==0 and x!=0):
        print('NO')
    else:
        if max(x/y,y/x)==1 or max(x/y,y/x)>2:
            print('NO')
        else:
            if (x+y)%3==0:
                print('YES')
            else:
                print('NO')


t=int(input())
for i in range (0,t):
    x,y=map(int,input().split())
    if x==0 and y==0:
        print('YES')
    elif (x==0 and y!=0) or (y==0 and x!=0):
        print('NO')
    else:
        if max(x/y,y/x)==1 or max(x/y,y/x)>2:
            print('NO')
        else:
            print('YES')


t=int(input())
for i in range (0,t):
    x,y=map(int,input().split())
    if max(x/y,y/x)==1 or max(x/y,y/x)>2:
        print('NO')
    else:
        print('YES')


for _ in range(int(input())):
    a,b=map(int,input().split())
    if a>b: a,b=b,a
    if b>(2*a):
        print('NO')
        continue
    a=a%3
    b=b%3
    if a>b: a,b=b,a
    if (a==1 and b==2) or (a==b and a==0): print('YES')
    else : print('NO')


for _ in range(int(input()):
    a,b=map(int,input().split())
    if a>b: a,b=b,a
    if b>(2*a):
        print('NO')
        continue
    a=a%3
    b=b%3
    if a>b: a,b=b,a
    if a==1 and b==2: print('YES')
    else : print('NO')


for _ in range(int(input())):
    a,b=map(int,input().split())
    if a>b: a,b=b,a
    if b>(2*a):
        print('NO')
        continue
    a=a%3
    b=b%3
    if a>b: a,b=b,a
    if a==1 and b==2: print('YES')
    else : print('NO')


for i in range(int(input())):
    t = sorted([int(j) for j in input().split()])
    print("YNEOS"[sum(t) % 3 and t[0] * 2 >= t[1]::2])


for i in range(int(input())):
    t = sorted([int(j) for j in input().split()])
    print("YNEOS"[(sum(t) % 3 > 0) or (t[0] * 2 < t[1])::2])


t=int(input())
for i in range(t):
    a,b=list(map(int,input().split()))
    m=max(a,b)
    n=min(a,b)
    if m-n>=n:
        print('NO')
    else:
        if (m+n)%3==0:
            print('YES')
        else:
            print('NO')


t=int(input())
for i in range(t):
    a,b=list(map(int,input().split()))
    m=max(a,b)
    n=min(a,b)
    if m-n>n:
        print('NO')
    else:
        if (m+n)%3==0:
            print('YES')
        else:
            print('NO')


for i in range(int(input())):
  a,b = [int(x) for x in input().split()]
  print('no' if (a+b)%3 or (a>0 and b==0) or (a==0 and b>0) else 'yes')


for i in range(int(input())):
  a,b = [int(x) for x in input().split()]
  print('yes' if (a+b)%3==0 and a>0 and b>0 else 'no')


for i in range(int(input())):
  a,b = [int(x) for x in input().split()]
  print('yes' if (a+b)%3==0 or a==0 or b==0 else 'no')


for i in range(int(input())):
  a,b = [int(x) for x in input().split()]
  a,b = min(a,b), max(a,b)
  print('yes' if not (a+b)%3 and 2*a>=b else 'no')


def solve(a,b):
    if b > 2*a:
        return "NO"
    if (a+b)%3 == 0:
        return "YES"
    return "NO"

t = int(input())
for _ in range(t):
    a,b = map(int,input().split())
    print(solve(a,b))


def solve(a,b):
    if b > 2*a:
        return "NO"
    if b == 2*a:
        return "YES"
    if (a%2 and b%2) or (a%2==0 and b%2==0):
        return "NO"
    if  (a%2==0 and b%2==1) or (a%2==1 and b%2==0):
        if a - b//2 == 2:
            return "YES"
        else:
            return "NO"

t = int(input())
for _ in range(t):
    a,b = map(int,input().split())
    print(solve(a,b))


def solve(a,b):
    if b > 2*a:
        return "NO"
    if (a+b)%3 == 0:
        return "YES"
    return "NO"

t = int(input())
for _ in range(t):
    a,b = map(int,input().split())
    a,b = min(a,b),max(a,b)
    print(solve(a,b))


n=int(input())
for _ in range(n):
  k=0
  a,b=map(int, input().split())
  if a==0 and b==0: k=1
  while a>0 and b>0:
    if a<b: a,b=b,a
    p=max(1,(a-b)//2)
    a-=2*p; b-=p
    if (a==0 and b==0) or a==b:
      k+=1
      break
  print("YES" if k==1 else "NO")


n=int(input())
for _ in range(n):
  k=0
  a,b=map(int, input().split())
  while a>0 and b>0:
    if a<b: a,b=b,a
    a-=2; b-=1
    if a==0 and b==0:
      k+=1
      break
  print("YES" if k==1 else "NO")


n=int(input())
for _ in range(n):
  k=0
  a,b=map(int, input().split())
  if a==0 and b==0: k=1
  while a>0 and b>0:
    if a<b: a,b=b,a
    p=max(1,(a-b)//2)
    a-=2*p; b-=p
    if a==b and a%3!=0:
      break
    elif a==b:
      k+=1
      break
  print("YES" if k==1 else "NO")


n=int(input())
for _ in range(n):
  k=0
  a,b=map(int, input().split())
  if a==0 and b==0: k=1
  while a>0 and b>0:
    if a<b: a,b=b,a
    p=max(1,(a-b))
    a-=2*p; b-=p
    if (a==b and a%3!=0) or a<0:
      break
    elif a==b:
      k+=1
      break
  print("YES" if k==1 else "NO")


n=int(input())
for _ in range(n):
  k=0
  a,b=map(int, input().split())
  if a==0 and b==0: k=1
  while a>0 and b>0:
    if a<b: a,b=b,a
    a-=2; b-=1
    if (a==0 and b==0) or a==b:
      k+=1
      break
  print("YES" if k==1 else "NO")


n=int(input())
for _ in range(n):
  k=0
  a,b=map(int, input().split())
  if a==0 and b==0: k=1
  while a>0 and b>0:
    if a<b: a,b=b,a
    p=max(1,a-b)
    a-=2*p; b-=p
    if (a==0 and b==0) or a==b:
      k+=1
      break
  print("YES" if k==1 else "NO")


n=int(input())
for _ in range(n):
  k=0
  a,b=map(int, input().split())
  if a==0 and b==0: k=1
  while a>0 and b>0:
    if a<b: a,b=b,a
    p=max(1,(a-b))
    a-=2*p; b-=p
    if a==b and a%3!=0:
      break
    elif a==b:
      k+=1
      break
  print("YES" if k==1 else "NO")


from collections import deque
t = int(input())
for _ in range(t):
    a, b = map(int, input().split())

    x = int(a - 2 * ((2*a - b)/3))
    y = int((2*a - b) / 3)
    if 2*y + x == a and y + (2*x) == b and x>=0 and y>=0:
        print("YES")
    else:
        print("NO")


from collections import deque
t = int(input())
for _ in range(t):
    a, b = map(int, input().split())

    if (a+b)%3 == 0:
        print("YES")
    else:
        print("NO")


from collections import deque
t = int(input())
for _ in range(t):
    a, b = map(int, input().split())

    a_1, b_2 = a-1, b-2
    a_2, b_1 = a-2, b-1
    if (b_2 / 2 == a_1 or a_1 / 2 == b_2) or (b_1/2==a_2 or a_2/2==b_1):
        print("YES")
    else:
        print("NO")


from collections import deque
t = int(input())
for _ in range(t):
    a, b = map(int, input().split())

    x = a - 2 * ((2*a - b)/3)
    y = (2*a - b) / 3
    if 2*y + x == a and y + (2*x) == b:
        print("YES")
    else:
        print("NO")


for _ in range(int(input())):
    a,b=map(int,input().split())
    if(a==b):
        print("NO")
    elif(a>b):
        if(a/b>2):
            print("NO")
        else:
            print("YES")
    elif(a<b):
        if(b/a>2):
            print("NO")
        else:
            print("YES")


import math
for _ in range(int(input())):
    a,b=map(int,input().split())
    if(a>b):
        if(a<=2*b and (a+b)%3==0):
            print("YES")
        else:
            print("NO")
    else:
        if(b<=2*a and (a+b)%3==0):
            print("YES")
        else:
            print("NO")


import math
for _ in range(int(input())):
    a,b=map(int,input().split())
    x=math.gcd(a,b)
    print(x)
    if(a==1 and b==1):
        print("NO")
    elif(a==1 and b==2):
        print("YES")
    elif(a==2 and b==1):
        print("YES")
    else:
        if(a>b):
            if(a>2*b):
                print("NO")
            else:
                if(x%3==0):
                    print("YES")
                else:
                    print("NO")
        else:
            if(b>2*a):
                print("NO")
            else:
                if(x%3==0):
                    print("YES")
                else:
                    print("NO")


import math
for _ in range(int(input())):
    a,b=map(int,input().split())
    x=math.gcd(a,b)
    #print(x)
    if(a==1 and b==1):
        print("NO")
    elif(a==1 and b==2):
        print("YES")
    elif(a==2 and b==1):
        print("YES")
    else:
        if(a>b):
            if(a>2*b):
                print("NO")
            else:
                if(x%3==0):
                    print("YES")
                else:
                    print("NO")
        else:
            if(b>2*a):
                print("NO")
            else:
                if(x%3==0):
                    print("YES")
                else:
                    print("NO")


import math
for _ in range(int(input())):
    a,b=map(int,input().split())
    x=math.gcd(a,b)
    #print(x)
    if(a==1 and b==1):
        print("NO")
    elif(a==1 and b==2):
        print("YES")
    elif(a==2 and b==1):
        print("YES")
    else:
        if(a>b):
            if(a%b==0):
                if(a//b==2):
                    print("YES")
                else:
                    print("NO")
            elif(a>2*b):
                print("NO")
            else:
                if(x%3==0):
                    print("YES")
                else:
                    print("NO")
        else:
            if(b>a):
                if(b%a==0):
                    if(b//a==2):
                        print("YES")
                    else:
                        print("NO")
                elif(b>2*a):
                    print("NO")
                else:
                    if(x%3==0):
                        print("YES")
                    else:
                        print("NO")


import math
for _ in range(int(input())):
    a,b=map(int,input().split())
    x=math.gcd(a,b)
    #print(x)
    if(a==1 and b==1):
        print("NO")
    elif(a==1 and b==2):
        print("YES")
    elif(a==2 and b==1):
        print("YES")
    else:
        if(a>b):
            if(a%b==0):
                if(a//b==2):
                    print("YES")
                else:
                    print("NO")
            else:
                if(a>2*b):
                    print("NO")
                else:
                    if(x%3==0):
                        print("YES")
                    else:
                        print("NO")
        else:

            if(b%a==0):
                if(b//a==2):
                    print("YES")
                else:
                    print("NO")
            else:
                if(b>2*a):
                    print("NO")
                else:
                    if(x%3==0):
                        print("YES")
                    else:
                        print("NO")


import math
for _ in range(int(input())):
    a,b=map(int,input().split())
    x=math.gcd(a,b)
    #print(x)
    if(a==1 and b==1):
        print("NO")
    elif(a==1 and b==2):
        print("YES")
    elif(a==2 and b==1):
        print("YES")
    else:
        if(a>b):
            if(a%b==0):
                if(a//b==2):
                    print("YES")
                else:
                    print("NO")
            else:
                if(a>2*b):
                    print("NO")
                else:
                    if(x%3==0):
                        print("YES")
                    else:
                        print("NO")
        else:
            if(b>a):
                if(b%a==0):
                    if(b//a==2):
                        print("YES")
                    else:
                        print("NO")
                else:
                    if(b>2*a):
                        print("NO")
                    else:
                        if(x%3==0):
                            print("YES")
                        else:
                            print("NO")


for _ in range(int(input())):
    a,b=map(int,input().split())
    if(a==b):
        if(a!=0):
            print("NO")
        else:
            print("YES")
    elif(a>b):
        if(a>2*b):
            print("NO")
        else:
            print("YES")
    elif(a<b):
        if(b>2*a):
            print("NO")
        else:
            print("YES")


for _ in range(int(input())):
    a,b=map(int,input().split())
    if(a==b):
        if(a==0):
            print("YES")
        else:
            if(a%2==0):
                print("YES")
            else:
                print("NO")
    elif(a>b):
        if(a>2*b):
            print("NO")
        else:
            print("YES")
    elif(b>a):
        if(b>2*a):
            print("NO")
        else:
            print("YES")


for _ in range(int(input())):
    a, b = map(int, input().split(" "))
    if a == 0 and b == 0:
        print("YES")
    elif a == 0  or b == 0:
        print("NO")
    elif a == b:
        print("NO")
    elif max(a,b) // min(a,b) == 2:
        print("YES")

    elif max(a,b) - min(a,b) == 1:
        print("NO")

    else:
        mx = max(a, b)
        mn = min(a, b)

        div = mx // 2
        mod = mx % 2
        if mod == 0:
            print("NO")

        elif mn < div:
            print("NO")

        elif mn % div < 2:
            print("NO")

        elif (mn % div) // mod == 2:
            print("YES")
        else:
            print("NO")


for _ in range(int(input())):
    a, b = map(int, input().split(" "))
    if a == 0 and b == 0:
        print("YES")
    elif a == 0  or b == 0:
        print("NO")
    elif a == b:
        print("NO")

    else:
        mx = max(a, b)
        mn = min(a, b)
        div = mx // 2
        mod = mx % 2
        if mod == 0:
            print("NO")

        elif mn < div:
            print("NO")

        elif (mn % div) // mod == 2:
            print("YES")
        else:
            print("NO")


for _ in range(int(input())):
    a, b = map(int, input().split(" "))
    if a == 0 and b == 0:
        print("YES")
    elif a == 0  or b == 0:
        print("NO")
    elif a == b:
        print("NO")
    elif max(a,b) // min(a,b) == 2:
        print("YES")

    elif (max(a, b) - min(a, b)) == 1:
        print("NO")

    else:
        mx = max(a, b)
        mn = min(a, b)

        div = mx // 2
        mod = mx % 2
        if mod == 0:
            print("NO")

        elif mn < div:
            print("NO")

        elif mn % div < 2:
            print("NO")

        elif (mn % div) // mod == 2:
            print("YES")
        else:
            print("NO")


for _ in range(int(input())):
    a, b = map(int, input().split(" "))
    mx = max(a, b)
    mn = min(a, b)
    if (mx+mn) % 3 == 0 or mx >= 2 * mn:
        print("YES")
    else:
        print("NO")


for _ in range(int(input())):
    a, b = map(int, input().split(" "))
    if a == 0 and b == 0:
        print("YES")
    elif a == 0  or b == 0:
        print("NO")
    elif a == b:
        print("NO")
    elif max(a,b) // min(a,b) == 2:
        print("YES")

    else:
        mx = max(a, b)
        mn = min(a, b)
        div = mx // 2
        mod = mx % 2
        if mod == 0:
            print("NO")

        elif mn < div:
            print("NO")

        elif mn % div < 2:
            print("NO")

        elif (mn % div) // mod == 2:
            print("YES")
        else:
            print("NO")


for _ in range(int(input())):
    a, b = map(int, input().split(" "))
    if a == 0  or b == 0:
        print("NO")
    elif a == b:
        print("NO")
    elif max(a,b) // min(a,b) == 2:
        print("YES")

    else:
        mx = max(a, b)
        mn = min(a, b)
        div = mx // 2
        mod = mx % 2

        if (mn % div) // mod == 2:
            print("YES")
        else:
            print("NO")


for _ in range(int(input())):
    a, b = map(int, input().split(" "))
    mx = max(a, b)
    mn = min(a, b)
    if (mx+mn) % 3 == 0 and mx >= 2 * mn:
        print("YES")
    else:
        print("NO")


for _ in range(int(input())):
    a, b = map(int, input().split(" "))
    if a == 0 and b == 0:
        print("YES")
    elif a == 0  or b == 0:
        print("NO")
    elif a == b:
        print("NO")
    elif max(a,b) // min(a,b) == 2:
        print("YES")

    else:
        mx = max(a, b)
        mn = min(a, b)
        div = mx // 2
        mod = mx % 2
        if mod == 0:
            print("NO")

        elif mn < div:
            print("NO")

        elif (mn % div) // mod == 2:
            print("YES")
        else:
            print("NO")


for _ in range(int(input())):
    a, b = map(int, input().split(" "))
    mx = max(a, b)
    mn = min(a, b)
    if (mx+mn) % 3 == 0 and mx <= 2 * mn:
        print("YES")
    else:
        print("NO")


for _ in range(int(input())):
    a, b = map(int, input().split(" "))
    if a == 0  or b == 0:
        print("NO")
    elif a == b:
        print("NO")
    elif max(a,b) // min(a,b) == 2:
        print("YES")

    else:
        mx = max(a, b)
        mn = min(a, b)
        div = mx // 2
        mod = mx % 2

        if mn < div:
            print("NO")

        elif (mn % div) // mod == 2:
            print("YES")
        else:
            print("NO")


for _ in range(int(input())):
    a, b = map(int, input().split(" "))
    if a or b == 0:
        print("NO")
    elif a == b:
        print("NO")

    else:
        mx = max(a, b)
        mn = min(a, b)
        div = mx // 2
        mod = mx % 2
        if (mn % div) // mod == 2:
            print("YES")
        else:
            print("NO")


for _ in range(int(input())):
    a, b = map(int, input().split(" "))
    if a == 0  or b == 0:
        print("NO")
    elif a == b:
        print("NO")
    elif max(a,b) // min(a,b) == 2:
        print("YES")

    else:
        mx = max(a, b)
        mn = min(a, b)
        div = mx // 2
        mod = mx % 2
        if mod == 0:
            print("NO")

        elif mn < div:
            print("NO")

        elif (mn % div) // mod == 2:
            print("YES")
        else:
            print("NO")


for _ in range(int(input())):
    a,b=map(int,input().split())
    if (a+b)%3==0 and a>0 and b>0:
        print('YES')
    else:
        print('NO')


for _ in range(int(input())):
    a,b=map(int,input().split())
    if ((a+b)%3==0 and a>0 and b>0) or (a==0 and b==0):
        print('YES')
    else:
        print('NO')


for _ in range(int(input())):
    a,b=map(int,input().split())
    if (a+b)%3==0:
        print('YES')
    else:
        print('NO')


for _ in range(int(input())):
    a,b=map(int,input().split())
    if ((a+b)%3==0 and a>2 and b>2) or (a==0 and b==0) or (a==b and a>2):
        print('YES')
    else:
        print('NO')


for _ in range(int(input())):
    a,b=map(int,input().split())
    print('YES' if (a+b)%3==0 and abs(a-b)<=(a+b)//3 else 'NO')


def main():
    t = int(input())
    for i in range(t):
        solve()

def solve():
    a, b = list(map(int, input().split(" ")))
    if (a + b) % 3 == 0 and (a * 1.5 != b or b * 1.5 != a):
        print("YES")
        return

    print("NO")

main()


def main():
    t = int(input())
    for i in range(t):
        solve()

def solve():
    a, b = list(map(int, input().split(" ")))
    min_val = min(a, b)
    max_val = max(a, b)
    if (a + b) % 3 == 0 and min_val * 2 >= max_val:
        print("YES")
        return

    print("NO")

main()


for i in range(int(input())):
    a,b=map(int,input().split())
    if a>b:
        a,b=b,a
    print(["NO","YES"][(a+b)%3==0 and 2*a>=b])


for i in range(int(input())):
    a,b=map(int,input().split())
    print(["NO","YES"][(a+b)%3==0])


for i in range(int(input())):
    a,b=map(int,input().split())
    print(["NO","YES"][(a+b)%3==0 and 2*a>=b])


for i in range(int(input())):
    a,b=map(int,input().split())
    if a==0 and b==0:
        print("yes")
    elif a>0 and b>0:
        print(["NO","YES"][(a+b)%3==0])
    else:
        print("NO")


n=int(input())
for i in range(n):
    a,b=list(map(int,input().split()))
    if ((2*b-a)%3==0 and 2*b-a>0) or ((2*a-b)%3==0 and 2*a-b>0):
        print("YES")
    else:
        print("NO")


n=int(input())
for i in range(n):
    a,b=list(map(int,input().split()))
    if (2*b-a)%3==0:
        print("YES")
    else:
        print("NO")


n=int(input())
for i in range(n):
    a,b=list(map(int,input().split()))
    if (a+b)%3==0 and min(a,b)*2>=max(a,b):
        print("YES")
    else:
        print("NO")


t = int(input())
for test in range(t):
    a, b = map(int, input().split())
    if (a + b) % 3 == 0:
        print("YES")
        continue
    print("NO")


t = int(input())
for test in range(t):
    a, b = map(int, input().split())
    t = a
    a = max(a, b)
    b = min(t, b)
    if (a + b) % 3 == 0 and a <= 2 * b:
        print("YES")
        continue
    print("NO")


t = int(input())
for test in range(t):
    a, b = map(int, input().split())
    while a > 0 and b > 0:
        if a < b:
            a -= 1
            b -= 2
        else:
            b -= 1
            a -= 2
    if a == b == 0:
        print("YES")
    else:
        print("NO")
    """
    if (a + b) % 3 == 0:
        print("YES")
        continue
    print("NO")
    """


t = int(input())
ans = []
for _ in range(t):
    a, b = map(int, input().split())
    if ((a + b) % 3) == 0:
        ans.append('YES')
    else:
        ans.append('NO')
print('\n'.join(ans))


t = int(input())
ans = []
for _ in range(t):
    a, b = map(int, input().split())
    if a * 2 < b or b * 2 < a:
        ans.append('NO')
    elif ((a + b) % 3) == 0 and a and b:
        ans.append('YES')
    else:
        ans.append('NO')
print('\n'.join(ans))


t = int(input())
ans = []
for _ in range(t):
    a, b = map(int, input().split())
    if ((a + b) % 3) == 0 and a and b:
        ans.append('YES')
    else:
        ans.append('NO')
print('\n'.join(ans))


t = int(input())
ans = []
for _ in range(t):
    a, b = map(int, input().split())
    if (a * 2) < b or (b * 2) < a or (a + b) % 3 != 0:
        ans.append('NO')
    else:
        if a and b:
            ans.append('YES')
        elif not a and not b:
            ans.append('YES')
        else:
            ans.append('NO')
print('\n'.join(ans))


t = int(input())
for case_num in range(t):
    a, b = map(int, input().split(' '))
    if a > b:
        tmp = a
        a = b
        b = tmp
    print("YES" if (a + b) % 3 == 0 and a * 2 >= b else "NO")


t = int(input())
for case_num in range(t):
    a, b = map(int, input().split(' '))
    print("YES" if (a + b) % 3 == 0 else "NO")


t=int(input())
for _ in range(t):
    a,b=map(int,input().split())
    a,b=max(a,b),min(a,b)
    if 2*b==a:
        print("YES")
    elif 2*b<a:
        print("NO")
    else:
        while 2*b>a:
            b-=2
            a-=1
        if 2*b==a:
            print("YES")
        else:
            print("NO")


t=int(input())
for _ in range(t):
    a,b=map(int,input().split())
    a,b=max(a,b),min(a,b)
    if 2*b==a:
        print("YES")
    elif 2*b<a:
        print("NO")
    else:
        if (2*b-a)%3==0:
            print("YES")
        else:
            print("NO")


n = int(input())
for i in range(n):
	a,b = map(int,input().split())
	t = b-a
	t1 = max(a,b)
	t2 = min(a,b)
	a,b = t2,t1
	if (b-a) > a:
		print("NO")
	elif (b-2*(b-a))%3 == 0 and 2*a-b >= 0:
		print("YES")
	else:
		print("NO")


n = int(input())
for i in range(n):
	a,b = map(int,input().split())
	r = max(a,b)
	s = min(a,b)
	if r > 2*s or r == s and r == 1 or r == s and r == 2:
		print("NO")
	elif r <= 2*s:
		print("YES")


n = int(input())
for i in range(n):
	a,b = map(int,input().split())
	r = max(a,b)
	s = min(a,b)
	if r >= 3 and s >= 3:
		if r == s and r%3 == 0:
			print("YES")
		elif r != s and (r-2*(r-s))%3 == 0:
			print("YES")
		else:
			print("NO")
	elif r == 2 and s == 1:
		print("YES")
	else:
		print("NO")


n = int(input())
for i in range(n):
	a,b = map(int,input().split())
	t = b-a
	if (b-a) > a:
		print("NO")
	elif (b-2*(b-a))%3 == 0 and 2*a-b >= 0;
		print("YES")
	else:
		print("NO")


import java.io.*;
import java.util.*;
public class HelloWorld
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0)
        {
            int n=sc.nextInt();
            int i=n;
            if(n<4)
                System.out.print(-1);
                else{
            while(i>0)
            {
                if(i%2==0)
                {
                    System.out.print(i+" ");
                }i--;
            }
            if(n>4)
            System.out.print("5 1 3 ");

            else
            System.out.print("1 3");i=7;
            while(i<=n){
            System.out.print(i+ " ");i+=2;}

        }

            System.out.println();
        }
}}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Objects;
import java.util.StringTokenizer;

/**
 * Problem CF1352G
 */
@SuppressWarnings("unchecked")
public class CF1352G {

    static class Task extends IOHandler {
        public void run() {
            int t = in.nextInt();
            while(t--!=0){
                int n = in.nextInt();
                if(n<4) out.println(-1);
                else {
                    for(int i = n-(n+1)%2; i >=1; i-=2)
                        out.print(i + " ");
                    out.print("4 2 ");
                    for( int i = 6; i <= n; i+=2)
                        out.print(i + " ");
                    out.println();
                }
            }
        }
    }

    /***********************************************************
     *                        COMMONS                          *
     ***********************************************************/
    static class Pair<A, B> implements Comparable<Pair<A, B>> {
        public A a;
        public B b;
        public Pair(Pair<A, B> p) {
            this(p.a, p.b);
        }

        public Pair(A a, B b) {
            this.a = a;
            this.b = b;
        }

        public String toString() {
            return a+" "+b;
        }

        public int hashCode() {
            return Objects.hash(a, b);
        }

        public boolean equals(Object o) {
            if(o instanceof Pair) {
                Pair<A,B> p = (Pair<A,B>) o;
                return a.equals(p.a)&&b.equals(p.b);
            }
            return false;
        }

        @Override
        public int compareTo(Pair<A, B> p) {
            int cmp = ((Comparable<A>) a).compareTo(p.a);
            if(cmp==0) {
                return ((Comparable<B>) b).compareTo(p.b);
            }
            return cmp;
        }
    }

    /***********************************************************
     *                      BOILERPLATE                        *
     ***********************************************************/
    public static void main(String[] args) {
        Task task = new Task();
        task.run();
        task.cleanup();
    }

    static class IOHandler {
        public InputReader in = new InputReader(System.in);
        public PrintWriter out = new PrintWriter(System.out);

        public void cleanup() {
            out.close();
        }
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }
    }

}


/*
 *created by Kraken on 09-05-2020 at 21:47
 */
//package com.kraken.cf.cf640;

import java.util.*;
import java.io.*;

public class G {

  public static void main(String[] args) {
    FastReader sc = new FastReader();
    int t = sc.nextInt();
    while (t-- > 0) {
      int n = sc.nextInt();
      if (n < 4) {
        System.out.println("-1");
        continue;
      }
      StringBuilder sb = new StringBuilder();
      int lo = n % 2 == 0 ? n - 1 : n;
      for (int i = lo; i >= 1; i -= 2) sb.append(i + " ");
      sb.append("4 2 ");
      for (int i = 6; i <= n; i += 2) sb.append(i + " ");
      System.out.println(sb);
    }
  }

  static class FastReader {

    BufferedReader br;

    StringTokenizer st;

    public FastReader() {
      br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
      while (st == null || !st.hasMoreElements()) {
        try {
          st = new StringTokenizer(br.readLine());
        }
        catch (IOException e) {
          e.printStackTrace();
        }
      }
      return st.nextToken();
    }

    int nextInt() {
      return Integer.parseInt(next());
    }

    long nextLong() {
      return Long.parseLong(next());
    }

    double nextDouble() {
      return Double.parseDouble(next());
    }

    String nextLine() {
      String str = "";
      try {
        str = br.readLine();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
      return str;
    }
  }
}