#include<cstdio>
#include<iostream>
using namespace std;
long long x1,x2,y1,y2,x,y;
long long a,b,c,d;
int yes(){
	if(a&&b) if(x==x1&&x==x2) return 0;
	if(c&&d) if(y==y1&&y==y2) return 0;
	x-=a,x+=b,y-=c,y+=d;
	return x1<=x&&x<=x2&&y1<=y&&y<=y2;
}
int main()
{   long long  t;
    scanf("%lld",&t);
    for(int i=1;i<=t;i++)
    {
    scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
    scanf("%lld%lld%lld%lld%lld%lld",&x,&y,&x1,&y1,&x2,&y2);

    if(yes())
    printf("Yes\n");
    else
    {
        printf("No\n");
    }
    }
    return 0;
}


#include<cstdio>
#include<iostream>
using namespace std;
long long x1,x2,y4,y2,x,y;
long long a,b,c,d;
int main()
{   long long  t;
    scanf("%lld",&t);
    for(int i=1;i<=t;i++)
    {
    scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
    scanf("%lld%lld%lld%lld%lld%lld",&x,&y,&x1,&y4,&x2,&y2);
    x+=b-a;
    y+=d-c;
    int p=0;
    if(x1<=x&&x2>=x&&y4<=y&&y2>=y)
    p=1;
    if(x1==x2&&!a||!b||y2==y4&&!c||!d)
    {
        p=0;
    }
    if(p)
    printf("Yes\n");
    else
    {
        printf("No\n");
    }
    }
    return 0;
}


#include<cstdio>
#include<iostream>
using namespace std;
long long x1,x2,y1,y2,x,y;
long long a,b,c,d;
int yes(){
	if(a&&b) if(x==x1&&x==x2) return 0;
	if(c&&d) if(y==y1&&y==y2) return 0;
	x-=a,x+=b,y-=c,y+=d;
	return x1<=x&&x<=x2&&y1<=y&&y<=y2;
}
int main()
{   long long  t;
    scanf("%lld",&t);
    for(int i=1;i<=t;i++)
    {
    scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
    scanf("%lld%lld%lld%lld%lld%lld",&x,&y,&x1,&y1,&x2,&y2);
    x+=b-a;
    y+=d-c;

    if(yes())
    printf("Yes\n");
    else
    {
        printf("No\n");
    }
    }
    return 0;
}


#include<cstdio>
#include<iostream>
using namespace std;
int x1,x2,y4,y2,x,y;
int a,b,c,d;
int main()
{   int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
    scanf("%d%d%d%d",&a,&b,&c,&d);
    scanf("%d%d%d%d%d%d",&x,&y,&x1,&y4,&x2,&y2);

    x+=b-a;
    y+=d-c;
    if(x1<=x&&x2>=x&&y4<=y&&y2>=y||x1==x2||y2==y4)
    printf("Yes\n");
    else
    {
        printf("No\n");
    }
    }
    return 0;
}


#include<cstdio>
#include<iostream>
using namespace std;
long long x1,x2,y4,y2,x,y;
long long a,b,c,d;
int main()
{   long long  t;
    scanf("%lld",&t);
    for(int i=1;i<=t;i++)
    {
    scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
    scanf("%lld%lld%lld%lld%lld%lld",&x,&y,&x1,&y4,&x2,&y2);
    x+=b-a;
    y+=d-c;
    int p=0;
    if(x1<=x&&x2>=x&&y4<=y&&y2>=y)
    p=1;
    if(((x1==x2)&&a||b)||((y2==y4)&&(c||d)))
    {
        p=0;
    }
    if(p)
    printf("Yes\n");
    else
    {
        printf("No\n");
    }
    }
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b)  for(int i = a; i <= b; i++)
#define ll long long
#define inf 1e6
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;

const int N=1e6+10;
const ll mod=998244353;

inline int read()
{
	int X=0; bool flag=1; char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
	while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
	if(flag) return X;
	return ~(X-1);
}
inline int write(int X)
{
	if(X<0) {putchar('-'); X=~(X-1);}
	int s[20],top=0;
	while(X) {s[++top]=X%10; X/=10;}
	if(!top) s[++top]=0;
	while(top) putchar(s[top--]+'0');
}

struct H
{
    int p,id;
    /*friend bool operator <(H a,H b)
    {
        return a.p<b.p;
    }*/

}he[N];

int n,ar[N];

int m,k;
 int a,b,c,d;
 int x,y,x1,y1,x2,y2;
 ll sm;
int main()
{
    // gbtb;

 // freopen("input.txt","r",stdin);

   // while(~scanf("%d %d",&n,&m))

   int T;cin>>T;while(T--)
    {
        a=read();b=read();c=read();d=read();
        x=read();y=read();x1=read();y1=read();x2=read();y2=read();
        if(x1>x2) swap(x1,x2);
        if(y1>y2) swap(y1,y2);
        a-=b;
        c-=d;
       x-=a;
       y-=c;
       if(x<x1||x>x2||y<y1||y>y2||(x1==x2&&a)||(y1==y2&&c))
       {
           cout<<"NO"<<endl;
       }
       else
       {
           cout<<"YES"<<endl;
       }

    }
    return 0;

}


#include<bits/stdc++.h>
#define rep(i,a,b)  for(int i = a; i <= b; i++)
#define ll long long
#define inf 1e6
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;

const int N=1e6+10;
const ll mod=998244353;

inline ll read()
{
	ll X=0; bool flag=1; char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
	while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
	if(flag) return X;
	return ~(X-1);
}
inline ll write(ll X)
{
	if(X<0) {putchar('-'); X=~(X-1);}
	int s[20],top=0;
	while(X) {s[++top]=X%10; X/=10;}
	if(!top) s[++top]=0;
	while(top) putchar(s[top--]+'0');
}

ll qpow(ll base ,ll n)
{
    ll ans=1;
    while(n)
    {
        if(n&1)
            ans=(ans*base)%mod;
        n>>=1;
        base=base*base%mod;
    }
    return ans;
}

struct H
{
    int p,id;
    /*friend bool operator <(H a,H b)
    {
        return a.p<b.p;
    }*/

}he[N];

int n,ar[N],x,br[N],m;
ll ans;

int main()
{
    // gbtb;

 // freopen("input.txt","r",stdin);

   // while(~scanf("%d %d",&n,&m))

   int T;cin>>T;while(T--)
    {
        n=read();
        int a,b,c,d,x1,x2,y1,y2,x,y;
        a=read();b=read();c=read();d=read();x=read();y=read();x1=read();y1=read();x2=read();y2=read();
        if(x1>x2) swap(x1,x2); if(y1>y2) swap(y1,y2);
        a-=b;
        c-=d;
        x+=a;
        y+=c;
        if(x<x1||x>x2||y<y1||y>y2) printf("NO\n");
        printf("YES\n");
    }
    return 0;

}


#include<bits/stdc++.h>
#define rep(i,a,b)  for(int i = a; i <= b; i++)
#define ll long long
#define inf 1e6
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;

const int N=1e6+10;
const ll mod=998244353;

inline ll read()
{
	ll X=0; bool flag=1; char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
	while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
	if(flag) return X;
	return ~(X-1);
}
inline ll write(ll X)
{
	if(X<0) {putchar('-'); X=~(X-1);}
	int s[20],top=0;
	while(X) {s[++top]=X%10; X/=10;}
	if(!top) s[++top]=0;
	while(top) putchar(s[top--]+'0');
}

ll qpow(ll base ,ll n)
{
    ll ans=1;
    while(n)
    {
        if(n&1)
            ans=(ans*base)%mod;
        n>>=1;
        base=base*base%mod;
    }
    return ans;
}

struct H
{
    int p,id;
    /*friend bool operator <(H a,H b)
    {
        return a.p<b.p;
    }*/

}he[N];

int n,ar[N],x,br[N],m;
ll ans;

int main()
{
    // gbtb;

 // freopen("input.txt","r",stdin);

   // while(~scanf("%d %d",&n,&m))

   int T;cin>>T;while(T--)
    {
        n=read();
        int a,b,c,d,x1,x2,y1,y2,x,y;
        a=read();b=read();c=read();d=read();x=read();y=read();x1=read();y1=read();x2=read();y2=read();
        if(x1>x2) swap(x1,x2); if(y1>y2) swap(y1,y2);
        a-=b;
        c-=d;
        x+=a;
        y+=c;
        if(x<x1||x>x2||y<y1||y>y2) printf("NO\n");
        printf("YES\n");
    return 0;

}


#include<bits/stdc++.h>
#define rep(i,a,b)  for(int i = a; i <= b; i++)
#define ll long long
#define inf 1e6
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;

const int N=1e6+10;
const ll mod=998244353;

inline int read()
{
	int X=0; bool flag=1; char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
	while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
	if(flag) return X;
	return ~(X-1);
}
inline int write(int X)
{
	if(X<0) {putchar('-'); X=~(X-1);}
	int s[20],top=0;
	while(X) {s[++top]=X%10; X/=10;}
	if(!top) s[++top]=0;
	while(top) putchar(s[top--]+'0');
}

struct H
{
    int p,id;
    /*friend bool operator <(H a,H b)
    {
        return a.p<b.p;
    }*/

}he[N];

int n,ar[N];

int m,k;
 int a,b,c,d;
 int x,y,x1,y1,x2,y2;
 ll sm;
int main()
{
    // gbtb;

 // freopen("input.txt","r",stdin);

   // while(~scanf("%d %d",&n,&m))

   int T;cin>>T;while(T--)
    {
        a=read();b=read();c=read();d=read();
        x=read();y=read();x1=read();y1=read();x2=read();y2=read();
        if(x1>x2) swap(x1,x2);
        if(y1>y2) swap(y1,y2);
        a-=b;
        c-=d;
       x-=a;
       y-=c;
       if(x<x1||x>x2||y<y1||y>y2||(x1==x2&&y1==y2))
       {
           cout<<"NO"<<endl;
       }
       else
       {
           cout<<"YES"<<endl;
       }

    }
    return 0;

}


#include<bits/stdc++.h>
#define rep(i,a,b)  for(int i = a; i <= b; i++)
#define ll long long
#define inf 1e6
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;

const int N=1e6+10;
const ll mod=998244353;

inline ll read()
{
	ll X=0; bool flag=1; char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
	while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
	if(flag) return X;
	return ~(X-1);
}
inline ll write(ll X)
{
	if(X<0) {putchar('-'); X=~(X-1);}
	int s[20],top=0;
	while(X) {s[++top]=X%10; X/=10;}
	if(!top) s[++top]=0;
	while(top) putchar(s[top--]+'0');
}

ll qpow(ll base ,ll n)
{
    ll ans=1;
    while(n)
    {
        if(n&1)
            ans=(ans*base)%mod;
        n>>=1;
        base=base*base%mod;
    }
    return ans;
}

struct H
{
    int p,id;
    /*friend bool operator <(H a,H b)
    {
        return a.p<b.p;
    }*/

}he[N];

int n,ar[N],x,br[N],m;
ll ans;

int main()
{
    // gbtb;

 // freopen("input.txt","r",stdin);

   // while(~scanf("%d %d",&n,&m))

   int T;cin>>T;while(T--)
    {

        int a,b,c,d,x1,x2,y1,y2,x,y;
        a=read();b=read();c=read();d=read();x=read();y=read();x1=read();y1=read();x2=read();y2=read();
        if(x1>x2) swap(x1,x2); if(y1>y2) swap(y1,y2);
        a-=b;
        c-=d;
        x-=a;
        y-=c;
        if(x<x1||x>x2||y<y1||y>y2||(a!=0&&x2-x1<=1)||(b!=0&&y2-y1<=1)) printf("NO\n");
        else
        printf("YES\n");
    }
    return 0;

}


#include<bits/stdc++.h>
#define rep(i,a,b)  for(int i = a; i <= b; i++)
#define ll long long
#define inf 1e6
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;

const int N=1e6+10;
const ll mod=998244353;

inline ll read()
{
	ll X=0; bool flag=1; char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
	while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
	if(flag) return X;
	return ~(X-1);
}
inline ll write(ll X)
{
	if(X<0) {putchar('-'); X=~(X-1);}
	int s[20],top=0;
	while(X) {s[++top]=X%10; X/=10;}
	if(!top) s[++top]=0;
	while(top) putchar(s[top--]+'0');
}

ll qpow(ll base ,ll n)
{
    ll ans=1;
    while(n)
    {
        if(n&1)
            ans=(ans*base)%mod;
        n>>=1;
        base=base*base%mod;
    }
    return ans;
}

struct H
{
    int p,id;
    /*friend bool operator <(H a,H b)
    {
        return a.p<b.p;
    }*/

}he[N];

int n,ar[N],x,br[N],m;
ll ans;

int main()
{
    // gbtb;

 // freopen("input.txt","r",stdin);

   // while(~scanf("%d %d",&n,&m))

   int T;cin>>T;while(T--)
    {

        int a,b,c,d,x1,x2,y1,y2,x,y;
        a=read();b=read();c=read();d=read();x=read();y=read();x1=read();y1=read();x2=read();y2=read();
        if(x1>x2) swap(x1,x2); if(y1>y2) swap(y1,y2);
        if(x==x1&&x==x2&&(a>0||b>0)) {printf("NO\n");continue;}
        if(y==y1&&y==y2&&(c>0||d>0)) {printf("NO\n");continue;}
        a-=b;
        c-=d;
        x-=a;
        y-=c;

        if(x<x1||x>x2||y<y1||y>y2) printf("NO\n");
        else
        printf("YES\n");
    }
    return 0;

}


#include <bits/stdc++.h>
#include <set>
#define ll long long
#define nl cout<<endl;
#define ina(arr,b) for(ll i=0;i<b;i++)cin>>arr[i];
#define outa(arr,a,b) for(ll i=a;i<b;i++)cout<<arr[i]<<" "; nl
#define loop(a,b) for(ll i=a;i<b;i++)
#define loop2(a,b) for(ll j=a;j<b;j++)
#define sloop(a,s) for(ll i=a;s[i]!='\0';i++)
#define rloop(a,b) for(ll i=a-1;i>=b;i--)
using namespace std;
void solve(){
    ll a,b,c,d,x,y,x1,x2,y1,y2,n=0;
    cin>>a>>b>>c>>d>>x>>y>>x1>>y1>>x2>>y2;
    x2-=x1;y2-=y1;
    x-=x1;y-=y1;
    x1=0;y1=0;
    x+=b-a;y+=d-c;
    if(x<0||y<0||x>x2||y>y2)n=1;
    if((x2-x1==0&&(a>0||b>0))||(y2-y1==0&&(c>0||d>0)))n=1;
    if(n)cout<<"NO";
    else cout<<"YES"; nl
}
int main()
{
    int t;
    cin>>t;
    while(t--)solve();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
  int x,x1,x2,y,y1,y2,a,b,c,d,t,xa,ya,xg,yg;
  cin>>t;
  while(t--){
        cin>>a>>b>>c>>d>>x>>y>>x1>>y1>>x2>>y2;
        xa=x1<x2?x1:x2;
        x+=abs(xa);x1+=abs(xa);x2+=abs(xa);
        ya=y1<y2?y1:y2;
        y+=abs(ya);y1+=abs(ya);y2+=abs(ya);
        xa=abs(x1-x2); ya=abs(y1-y2);
        xg=a>b?a:b; yg=c>d?c:d;
        if(xa>=xg&&ya>=yg)cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#include <set>
#define ll long long
#define nl cout<<endl;
#define ina(arr,b) for(ll i=0;i<b;i++)cin>>arr[i];
#define outa(arr,a,b) for(ll i=a;i<b;i++)cout<<arr[i]<<" "; nl
#define loop(a,b) for(ll i=a;i<b;i++)
#define loop2(a,b) for(ll j=a;j<b;j++)
#define sloop(a,s) for(ll i=a;s[i]!='\0';i++)
#define rloop(a,b) for(ll i=a-1;i>=b;i--)
using namespace std;
void solve(){
    ll a,b,c,d,x,y,x1,x2,y1,y2,n=0;
    cin>>a>>b>>c>>d>>x>>y>>x1>>y1>>x2>>y2;
    x2-=x1;y2-=y1;
    x-=x1;y-=y1;
    cout<<x<<" "<<y<<" "<<x2<<" "<<y2<<" ";nl
    x1=0;y1=0;
    x+=b-a;y+=d-c;
    cout<<x<<" "<<y<<" "<<x2<<" "<<y2<<" ";nl
    if(x<0||y<0||x>x2||y>y2)n=1;
    if((x2-x1==0&&(a>0||b>0))||(y2-y1==0&&(c>0||d>0)))n=1;
    if(n)cout<<"NO";
    else cout<<"YES"; nl
}
int main()
{
    int t;
    cin>>t;
    while(t--)solve();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
  long long int x,x1,x2,y,y1,y2,a,b,c,d,t,xa,ya,xg,yg;
  cin>>t;
  while(t--){
        cin>>a>>b>>c>>d>>x>>y>>x1>>y1>>x2>>y2;
        xa=x1<x2?x1:x2;
        x+=abs(xa);x1+=abs(xa);x2+=abs(xa);
        ya=y1<y2?y1:y2;
        y+=abs(ya);y1+=abs(ya);y2+=abs(ya);
        xa=abs(x1-x2); ya=abs(y1-y2);
        xg=a>b?a:b; yg=c>d?c:d;
        if(xa>=xg&&ya>=yg)cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
  int x,x1,x2,y,y1,y2,a,b,c,d,t,xa,ya,xg,yg;
  cin>>t;
  while(t--){
        cin>>a>>b>>c>>d>>x>>y>>x1>>y1>>x2>>y2;
        xa=x1<x2?x1:x2;
        x+=abs(xa);x1+=abs(xa);x2+=abs(xa);
        ya=y1<y2?y1:y2;
        y+=abs(ya);y1+=abs(ya);y2+=abs(ya);
        xa=abs(x1-x2); ya=abs(y1-y2);
        xg=a<b?a:b; yg=c<d?c:d;
        if(xa>=xg&&ya>=yg)cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;

ll pow(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1;
    temp = pow(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}
int main()
{
    ll t,i,j,c,d,f=0,even,odd,x,y,zero,one,sorted,n,k,m,C,b,curr,ans,p,ind,x1,x2,y1,y2,a;
    cin>>t;
    while(t--)
    {
        cin>>a>>b>>c>>d;
        cin>>x>>y>>x1>>y1>>x2>>y2;
        int hf=0,vf=0;
        if(b>a)
        {
            x=x+b-a;
            if( x>=x1 && x<=x2 )
                hf=1;
        }
        else if(a>b)
        {
            x=x-(a-b);
            if( x>=x1 && x<=x2 )
                hf=1;
        }
        else if(a==b)
        {
            if((a==0) || (x+1>=x1 && x+1<=x2)|| (x-1>=x1 && x-1<=x2) )
                hf=1;
        }
        if(d>c)
        {
            y=y+d-c;
            if( y>=y1 && y<=y2 )
                vf=1;
        }
        else if(c>d)
        {
            y= y-(c-d);
            if( y>=y1 && y<=y2 )
                vf=1;
        }
        else if(d==c)
        {
            if((d==0)|| (y+1>=y1 && y+1<=y2)|| (y-1>=y1 && y-1<=y2) )
                vf=1;
        }
        if(vf==1 && hf==1)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        long long int a,b,c,d;
        cin>>a>>b>>c>>d;
        long long int x,y,x1,y1,x2,y2;
        cin>>x>>y>>x1>>y1>>x2>>y2;
        long long int rex=x-a+b;
        long long int rey=y-c+d;
        int f1=0,f2=0;
        if(rex>=x1 && rex<=x2 && x1<x2 || (a+b)==0){
            f1=1;
        }
        if(rey>=y1 && rey<=y2 && y1<y2 || (c+d)==0){
            f2=1;
        }
        if(f1==1 && f2==1){
            cout<<"Yes"<<endl;
        }
        else{
            cout<<"No"<<endl;
        }
    }
}


n = int(input())
l = [int(x) for x in input().split()]

i = 1
while(True):
  if (i == len(l)):
    break;

  ce = l[i]
  pe = l[i-1]
  if(ce == pe):
    l[i-1] += 1
    del l[i]
    i = 1
  else:
    i += 1

l = list(map(str, l))
print(" ".join(l))


n = int(input())
l = [int(x) for x in input().split()]

i = 1
while(True):
  if (i == len(l)):
    break;

  ce = l[i]
  pe = l[i-1]
  if(ce == pe):
    l[i-1] += 1
    del l[i]
    if (i != 1):
      i -= 1
  else:
    i += 1

l = list(map(str, l))
print(len(l))
print(" ".join(l))


n = int(input())
l = [int(x) for x in input().split()]

d = {}
for i in range(0, n):
    d[i] = l[i];

i = 0
le = len(l)
tl = len(l)
c = tl
ni = 0
while(True):
    if (i+1 == c):
        break;

    while(i not in d):
        i += 1
        if (i == tl):
            break;
    ni = i+1
    while(ni not in d):
        ni += 1
        if (ni == tl):
            break;
    else:
        if (d[i] == d[ni]):
            d[i] += 1
            d.pop(ni)
            c -= 1
            if (i != 0):
                i -= 1
                while(i not in d):
                    i -= 1
        else:
            i += 1

print(c)
for k in sorted(d.keys()):
    print(d[k])


n = input()
a = list(map(int, input().split()))
b = []
for x in a:
    while b and x == b[-1]:
        x += 1
        b.pop()
    else:
        b.append(x)
print(len(b))
print(*b)


n = int(input())
p = 0
a = input().split(" ")
b = []
for i in range(0, n):
  b.append(int(a[i]))
  b[p] = int(a[i])
  while p > 0 and b[p - 1] == b[p]:
    b[p - 1] = b[p - 1] + 1
    p = p - 1
  p = p + 1
print(p)
ans = ""
for i in range(0, p):
  ans += str(b[i]) + " "
print(ans)


n = int(input())
p = 0
arr = [n]
a = [int(x) for x in input().split()]
b = [n]
for i in range(n):
  b[p] = a[i]
  while p > 0 and b[p - 1] == b[p]:
    b[p - 1] = b[p-1] + 1
    p = p - 1
  p = p + 1
print(p)
print(b[p::])


from collections import deque

n = int(input())
a= list(map(int, input().split()))
dq = deque()
for i in range(n):
    x = a[i]
    while dq and x == dq[-1]:
        dq.pop()
        x += 1
    dq.append(x)
print(len(dq))
print(*dq)


n = int(input())
a = input().split()
a = [int(x) for x in a]
b = []
for u in a:
    b.append(u)
    if len(b) > 0 or b[-1] != u:
        while (len(b) > 1 and b[-1] == b[-2]):
            b[-2] += 1
            b.pop()

print(len(b))
print(' '.join([str(x) for x in b]))


n = int(input())
a = input().split()
a = [int(x) for x in a]
b = []
for u in a:
    b.append(u)
    if len(b) > 0 or b[-1] != u:
        while (len(b) > 1 and b[-1] == b[-2]):
            b[-2] += 1
            b.pop()

print(len(b))
if len(b) == 1:
    print(b[0])
else:
    print(' '.join(b))


n = gets.chomp.to_i
c = gets.split.map(&:to_i)

cur1 = 1
cur = 0
ans = []

for i in c
  ans << i
  while ans.size > 1 and ans[-1] == ans[-2]
    cur = ans[-1]
    cur += 1
    ans.pop
    ans.pop
    ans << cur
  end
end

print "#{ans.size}"
print "\n"

for i in ans
  print "#{i} "
end


n = gets.to_i
a = gets.split.map(&:to_i)
b=[]
for x in a
	b<<x
	while b.size>1 and b[-1]==b[-2]
		b.pop
		b[-1]+=1
	end
end

print "#{b.size}"
print "\n"

for x in b
  print "#{x} "
end


# help from stackoverflow
n = gets.to_i
arr = gets.split.map(&:to_i)
i = 1
filled = 1
ans = []
prev = arr[0]
tmp = arr[0]
ans << arr[i]
while i<n
    if prev == -1
        ans << arr[i]
        prev = arr[i]
    if arr[i] == prev
        ans.pop
        ans << prev+prev
        filled = filled -1
        if filled == 0
            prev = -1
        else
            prev = prev+prev
    else
        ans << arr[i]
        prev = arr[i]

    i= i+1
end

puts filled

for i in (0..filled)
    print ans[i]
    print " "
end

puts n


# help from stackoverflow
n = gets.to_i
arr = gets.split.map(&:to_i)
i = 1
filled = 1
ans = []
tmp = arr[0]
ans << arr[0]
while i<n
    if filled == 0
        ans << arr[i]
        filled = filled +1
    elsif arr[i] == ans[-1]
        ans.pop
        ans << arr[i]+1
        while ans.length > 1 && ans[-1]==ans[-2]
            tmp = ans.pop
            ans.pop
            ans << tmp+1
            filled = filled -1
        end
    else
        ans << arr[i]
        filled= filled+1
    end

    i= i+1
end

puts filled

for i in (0..filled)
    print ans[i]
    print " "
end


n = gets.chomp.to_i
c = gets.split.map(&:to_i)
cur1 = 1
cur = 0
ans = []
for i in c
  ans << i
  while ans.size > 1 and ans[-1] == ans[-2]
    cur = ans[-1]
    ans.pop
    ans.pop
    cur += 1
    ans << cur
  end
end
print "#{ans.size}\n"
for i in ans
  print "#{i} "
end


n = gets.chomp.to_i
c = gets.split.map(&:to_i)
cur1 = 1
cur = 0
ans = []
for i in c
  ans += [i]
  while ans.size > 1 and ans[ans.size - 1] == ans[ans.size - 2]
    cur = ans[ans.size - 1]
    ans.pop
    ans.pop
    cur += 1
    ans += [cur]
  end
end
print "#{ans.size}\n"
for i in ans
  print "#{i} "
end


n = gets.to_i
arr = gets.split(/[ \n]/);

st = Array.new(n) { |x|  }
len = 0
for x in arr
    x = x.to_i
    if (len != 0 && st[len - 1] == x)
        st[len - 1] += 1
    else
        st[len] = x
        len += 1
    end
    while (len > 1 && st[len - 2] == st[len - 1]) do
        len -= 1;
        st[len - 1] += 1
    end
end
puts len
for i in (0...len) do
    print "#{st[i]} ";
end


r=->{gets.split.map &:to_i}
n = r[]
n = n[0]
v = r[]
a = Array.new
n.times {|i|
    x = v[i]
    while a.length != 0 and a[a.length - 1] == x do
        a.pop()
        x = x + 1
    end
    a.push(x)
}
puts(a.length)
for x in a do
    print(x)
    print(" ")
end


r=->{gets.split.map &:to_i}
n = r[]
n = n[0]
v = r[]
a = Array.new
n.times {|i|
    x = v[i]
    while a.length != 0 and a[a.length - 1] == x do
        a.pop()
        x = x + 1
    end
    a.push(x)
}
puts(a.length)
s = ""
for x in a do
    s = s + String(x) + " "
end
puts(s)


n = map(int, raw_input().split())
a = map(int, raw_input().split())
b = [0] * len(a)
k = 0

prev = 0
for x in a:
	b[k] = x
	k += 1

	while k > 1 and b[k-1] == b[k-2]:
		k -= 1
		b[k-1] += 1

print(k)

for i in range(0, k):
	print b[i] ,


import sys

n = input()
a = map(int, sys.stdin.readline().split())

stack = []

for z in a:
    stack.append(z)
    while len(stack) > 1 and stack[len(stack)-2] == stack[len(stack)-1]:
        tmp = stack[len(stack)-2]
        del stack[-1]
        del stack[-1]
        stack.append(tmp+1)

print len(stack)

for z in stack:
    print z,


_ = int(raw_input())
a = map(int, raw_input().split())
b = []
for x in a:
    b.append(x)
    while len(b) >= 2 and b[-2] == b[-1]:
        b.pop()
        b[-1] += 1
print len(b)
print ' '.join(map(str, b))


class Stack:
     def __init__(self):
         self.items = []

     def isEmpty(self):
         return self.items == []

     def push(self, item):
         self.items.append(item)

     def pop(self):
         return self.items.pop()

     def peek(self):
         return self.items[len(self.items)-1]

     def size(self):
         return len(self.items)
n = input()
str_arr = raw_input().split(' ')
arr = [int(num) for num in str_arr]
s = Stack()
for i in range(0,n):
    if s.isEmpty():
        s.push(arr[i])

    else:
        x = arr[i]
        while not s.isEmpty() and s.peek()==x:
            s.pop()
            x = x+1
        s.push(x)
l = []
while not s.isEmpty():
    l.append(s.peek())
    s.pop()
l.reverse()
print len(l)
print(' '.join(str(x) for x in l))


class Stack:
     def __init__(self):
         self.items = []

     def isEmpty(self):
         return self.items == []

     def push(self, item):
         self.items.append(item)

     def pop(self):
         return self.items.pop()

     def peek(self):
         return self.items[len(self.items)-1]

     def size(self):
         return len(self.items)
n = input()
str_arr = raw_input().split(' ')
arr = [int(num) for num in str_arr]
s = Stack()
for i in range(0,n):
    if s.isEmpty():
        s.push(arr[i])

    else:
        x = arr[i]
        while not s.isEmpty() and s.peek()==x:
            s.pop()
            x = x+1
        s.push(x)
l = []
while not s.isEmpty():
    l.append(s.peek())
    s.pop()
l.reverse()
print(' '.join(str(x) for x in l))


class Stack:
     def __init__(self):
         self.items = []

     def isEmpty(self):
         return self.items == []

     def push(self, item):
         self.items.append(item)

     def pop(self):
         return self.items.pop()

     def peek(self):
         return self.items[len(self.items)-1]

     def size(self):
         return len(self.items)
n = input()
str_arr = raw_input().split(' ')
arr = [int(num) for num in str_arr]
s = Stack()
for i in range(0,n):
    if s.isEmpty():
        s.push(arr[i])

    else:
        x = arr[i]
        while not s.isEmpty() and s.peek()==x:
            s.pop()
            x = x+1
        s.push(x)
l = []
while not s.isEmpty():
    l.append(s.peek())
    s.pop()
l.reverse()
print l


#include <stdio.h>

int main() {
    int n;
    scanf("%d", &n);

    int a[n];
    for (int i = 0; i < n; i++) {
        scanf("%d", a+i);
    }

    int ans[n];
    int pos = 0;
    for (int i = 0; i < n; i++) {
        while (pos > 0 && a[i] == ans[pos-1]) {
            a[i]++;
            pos--;
        }
        ans[pos++] = a[i];
    }

    printf("%d\n", pos);
    for (int i = 0; i < pos; i++) {
        printf("%d ", ans[i]);
    }
    printf("\n");

    return 0;
}


#include <stdio.h>

int main() {
    int n;
    scanf("%d", &n);

    int a[n];
    for (int i = 0; i < n; i++) {
        scanf("%d", a+i);
    }

    int ans[n];
    int pos = 0;
    for (int i = 0; i < n; i++) {
        while (pos > 0 && a[i] == ans[pos-1]) {
            a[i]++;
            pos--;
        }
        ans[pos++] = a[i];
    }

    printf("%d", ans[0]);
    for (int i = 1; i < pos; i++) {
        printf(" %d", ans[i]);
    }
    printf("\n");

    return 0;
}


#include <stdio.h>

int main() {
    int n;
    scanf("%d", &n);

    int a[n];
    for (int i = 0; i < n; i++) {
        scanf("%d", a+i);
    }

    int ans[n];
    int pos = 0;
    for (int i = 0; i < n; i++) {
        while (pos > 0 && a[i] == ans[pos-1]) {
            a[i]++;
            pos--;
        }
        ans[pos++] = a[i];
    }

    for (int i = 0; i < pos; i++) {
        printf("%d ", ans[i]);
    }
    printf("\n");

    return 0;
}


#include <stdio.h>

const int maxn = (int)2e5+3;

int n;
int a[maxn];

int main() {
    int ptr = 0;
    scanf("%d", &n);
    for(int i = 0; i < n; ++i) {
        int x;
        scanf("%d", &x);
        a[ptr++] = x;
        while (ptr > 1 && a[ptr-1] == a[ptr-2]) {
            ptr -= 2;
            a[ptr++]++;
        }
    }
    printf("%d\n", ptr);
    for(int i = 0; i < ptr; ++i) {
        printf("%d ", a[i]);
    }

    return 0;
}


#include <stdio.h>

int n;
int a[200003];

int main() {
    int ptr = 0;
    // int* a = new int[maxn];
    scanf("%d", &n);
    for(int i = 0; i < n; ++i) {
        int x;
        scanf("%d", &x);
        a[ptr++] = x;
        while (ptr > 1 && a[ptr-1] == a[ptr-2]) {
            ptr -= 2;
            a[ptr++]++;
        }
    }
    printf("%d\n", ptr);
    for(int i = 0; i < ptr; ++i) {
        printf("%d ", a[i]);
    }

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

typedef struct node_s node_t;

struct node_s{
    node_t *prev;
    node_t *next;
    int val;
};

node_t nodes[2222222];

int main() {
    int n;
    scanf("%i", &n);
    nodes[0].prev = NULL;
    scanf("%i", &nodes[0].val);
    for (int i = 1; i < n; i++) {
        nodes[i - 1].next = &nodes[i];
        nodes[i].prev = &nodes[i - 1];
        scanf("%i", &nodes[i].val);
    }
    node_t *node = &nodes[0];

    while (node != NULL) {
        if (node->prev) {
            if (node->prev->val == node->val) {
                node->prev->next = node->next;
                if (node->next) {
                    node->next->prev = node->prev;
                }
                node = node->prev;
                node->val++;
                continue;
            }
        }
        node = node->next;
    }
    int m = 0;
    node = &nodes[0];
    while (node) {
        m++;
        node = node->next;
    }
    printf("%i\n", m);
    node = &nodes[0];
    while (node) {
        printf("%i ", node->val);
        node = node->next;
    }
}


#include <stdio.h>

#define MAXN 200200

int n;
int sz;
int arr[MAXN];

int main() {
  scanf("%d", &n);

  int i;
  for (i = 0; i < n; i++) {
    scanf("%d", &arr[sz++]);

    while (sz >= 2 && arr[sz-1] == arr[sz-2]) {
      arr[sz-2]++;
      sz--;
    }
  }

  printf("%d\n", sz);
  for (i = 0; i < sz; i++) printf("%d ", arr[i]);
  printf("\n");

  return 0;
}


#include<stdio.h>
int a[200001];int size=0;
int main(){
   int n;scanf("%d",&n);
   while(n-->0){
      scanf("%d",&a[size]);++size;
      while(size>=2&&a[size-1]==a[size-2]){
         --size;
         ++a[size-1];
      }
   }
   printf("%d\n",size);
   for(int i=0;i<size;++i)printf("%d ",a[i]);
   printf("\n");
}


#include<stdio.h>
#include<stdlib.h>
int LAST[200010],NEXT[200010];
int HEAD;
int v[200010];
int main(){
	int n,to;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&v[i]);
	}
	for(int i=1;i<=n;i++)
		LAST[i]=i-1,NEXT[i]=i+1;
	HEAD=1;
	int now=HEAD;
	NEXT[0]=1;
	LAST[n+1]=n;
	int ans=0;
	while(now<=n){
		if(LAST[now]&&v[LAST[now]]==v[now]){
			to=LAST[now];
			NEXT[to]=NEXT[now];
			LAST[NEXT[now]]=to;
			v[to]++;
			now=to;
			continue;
		}
		now=NEXT[now];

	}
	now=NEXT[0];
	while(now<n){
		now=NEXT[now];
		ans++;
	}
	printf("%d\n",ans);
	now=NEXT[0];
	while(now<n){
		printf("%d ",v[now]);
		now=NEXT[now];

	}
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
int LAST[200010],NEXT[200010];
int HEAD;
int v[200010];
int main(){
	int n,to;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&v[i]);
	}
	for(int i=1;i<=n;i++)
		LAST[i]=i-1,NEXT[i]=i+1;
	HEAD=1;
	int now=HEAD;
	NEXT[0]=1;
	LAST[n+1]=n;
	int ans=0;
	while(now<=n){
		if(LAST[now]&&v[LAST[now]]==v[now]){
			to=LAST[now];
			NEXT[to]=NEXT[now];
			LAST[NEXT[now]]=to;
			v[to]++;
			now=to;
			continue;
		}
		now=NEXT[now];

	}
	now=NEXT[0];
	while(now<=n){
		now=NEXT[now];
		ans++;
	}
	printf("%d\n",ans);
	now=NEXT[0];
	while(now<=n){
		printf("%d ",v[now]);
		now=NEXT[now];

	}
	return 0;
}


using System;
using System.Linq;

public static class Program {
  public static void Main() {
    int n = Convert.ToInt32( Console.ReadLine() );
    int[] ans = new int[300000];
	int[] a = new int[300000];
	var nn = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
    for( int i = 0; i < n; ++i ) {
      a[i] = Convert.ToInt32(nn[i]);
    }
    int last = 0;
	ans[0]= a[0];
	for(int i = 1; i<n; i++){
		if (a[i]!=ans[last]){
			last++;
			ans[last] = a[i];
		}
		else{
			ans[last]++;
			while(last>0 && ans[last]==ans[last-1]){
				last--;
				ans[last]++;
			}
		}

	}
	Console.WriteLine( last+1);
	for(int i = 0; i<last+1; i++){
		Console.Write(ans[i]+" ");
	}
  }
}


using System;
using System.Linq;
using System.Collections.Generic;

public class Test
{
	public static void Main()
	{
		var nn = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
		int n = nn[0];
		var a = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
		List<int> res = new List<int>();
		int sz = 0;
		for(int i = 0; i < n; ++i) {
			res.Add(a[i]); ++sz;
			while(sz >= 2 && res[sz-2] == res[sz-1]) {
				int x = res[sz-1];
				res.RemoveAt(sz-1);
				res.RemoveAt(sz-2);
				res.Add(x+1);
				sz--;
			}

		}
		Console.WriteLine(sz);
		for(int i = 0; i < sz; ++i) {Console.Write(res[i]); Console.Write(' ');}
	}
}


using System;
using System.Collections.Generic;
using System.IO;

class Solution {
    static void Main(String[] args) {
        int n = Convert.ToInt32(Console.ReadLine());
        string[] tokens = Console.ReadLine().Split();
        int[] a = new int[n];
        for (int i=0;i<n;i++) a[i] = Convert.ToInt32(tokens[i]);
        int p = 0;
        int p1 = 1;
        int N = n;

        while (p1 < N){
            if (a[p1] == a[p]){

            //Console.WriteLine("Merging " + a[p1]);
            a[p+1] = a[p];
            p++;
            while (p > 0 && a[p] == a[p-1]){
              //      Console.WriteLine("Increment " + p + " with "+ a[p]);
                    a[--p]++;

                }

                p1++;
            } else{
                a[++p]= a[p1++];
            }
            //Console.WriteLine("Now p = " + p);
        }
        Console.WriteLine(p+1);
        for (int i=0;i<=p;i++)
            Console.Write(a[i] + " ");
    }
}
/*
 7 6 5 4 3 2 2

  5 2 1 1 2 2
  5 2 1 1

*/


using System;
using System.Collections;
class Program
{
    static void Main()
    {
        int n = Convert.ToInt32(Console.ReadLine());
        var t = Console.ReadLine().Split(' ');
        int [] a = new int[n];
        for(int i=0; i<n; i++)
            a[i] = Convert.ToInt32(t[i]);

        Stack s = new Stack();
        for(int i=0; i<n; i++) {
        	int cur = a[i];
        	while(check(s) > 0 && (int)s.Peek()==cur) {
        		s.Pop();
        		cur++;
        	}
        	s.Push(cur);
        }

        Stack s2 = new Stack();
        while(check(s) > 0) {
            s2.Push(s.Peek());
            s.Pop();
        }

        Console.WriteLine("{0}", count(s2));
        foreach(Object o in s2)
        	Console.Write("{0} ", o);
        Console.WriteLine();
    }

    static int check(Stack s) {
        int cnt = 0;
        foreach(Object o in s) {
            cnt++;
            break;
        }
        return cnt;
    }

    static int count(Stack s) {
        int cnt = 0;
        foreach(Object o in s) {
            cnt++;
        }
        return cnt;
    }
}


using System;
using System.Linq;

public static class Program
{

    public static void Main()
    {
		int [] b=new int[200005];
		b[0]=0;
        var nn = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        var n = nn[0];
		var top=0;
        var a = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        for(int i=0;i<n;i++)
		{

			var now=a[i];
			//Console.WriteLine(now);
			while(b[top]==now&&top>0)
			{
				now++;top--;
			}
			top++;
			b[top]=now;
			//Console.WriteLine(top);
		}
        Console.WriteLine(top);
        for(int i=1;i<=top;i++)
        {
            Console.Write(b[i]);
            Console.Write(" ");
        }
    }
}


using System;
using System.Linq;

public static class Program
{

    public static void Main()
    {
         var temp = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
         var n=temp[0];
         var a = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
         int[] ans=new int[200001];
         int r=1,curr=0;
         ans[0]=a[0];
         for (; r < n; ++r)
         {
         	if(ans[curr]!=a[r]){
         		curr++;
         		ans[curr]=a[r];
         	}
         	else{
         		ans[curr]++;
         		while(curr>0){
         			if(ans[curr-1]==ans[curr]){
         				curr--;
         				ans[curr]++;
         			}
         			else{
         				break;
         			}
         		}
         	}
         }
         Console.WriteLine(curr+1);
         for(int i=0;i<=curr;++i){
         	Console.Write(ans[i]);
         	Console.Write(" ");
         }
    }
}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;

namespace ReadWriteTemplate
{
    public static class Solver
    {
        private static void SolveCase()
        {
            int n = ReadInt();
            var a = ReadLongArray().ToList();
            while (true)
            {
                var b = new List<long>();
                var d = a[a.Count - 1];
                for (int i = a.Count - 2; i >= 0; i--)
                {
                    if (d != a[i])
                    {
                        b.Add(d);
                        d = a[i];
                    }
                    else
                    {
                        d++;
                    }
                }
                b.Add(d);

                b.Reverse();

                if (a.Count == b.Count)
                {
                    break;
                }

                a = b;

                /*for (int i = 0; i < a.Length; i++)
                {
                    if (i == a.Length - 1 || a[i] != a[i + 1])
                    {
                        b.Add(a[i]);
                    }
                    else
                    {
                        b.Add();
                    }
                }*/
            }

            Writer.WriteLine(a.Count);
            Writer.WriteLine(string.Join(" ", a));
        }

        public static void Solve()
        {
            // int T = ReadInt();
            // for (int i = 0; i < T; i++)
            // {
            //     Writer.Write("Case #{0}: ", i + 1);
                SolveCase();
            // }
        }

        public class Point
        {
            public Point(int x, int y)
            {
                X = x;
                Y = y;
            }

            public int X;

            public int Y;

            public double Length { get { return Math.Sqrt(X * X + Y * Y); } }

            protected bool Equals(Point other)
            {
                return X == other.X && Y == other.Y;
            }

            public override bool Equals(object obj)
            {
                if (ReferenceEquals(null, obj))
                {
                    return false;
                }
                if (ReferenceEquals(this, obj))
                {
                    return true;
                }
                /*if (obj.GetType() != this.GetType())
                {
                    return false;
                }*/
                return Equals((Point) obj);
            }

            public override int GetHashCode()
            {
                unchecked
                {
                    return (X * 397) ^ Y;
                }
            }

            public int Dist2(Point p)
            {
                return (p.X - X) * (p.X - X) + (p.Y - Y) * (p.Y - Y);
            }

            public static Point operator +(Point a, Point b)
            {
                return new Point(a.X + b.X, a.Y + b.Y);
            }

            public static Point operator -(Point a, Point b)
            {
                return new Point(a.X - b.X, a.Y - b.Y);
            }
        }

        public static class Standard
        {
            public static long Power(long x, long p, long mod)
            {
                long result = 1;
                while (p > 0)
                {
                    if ((p & 1) == 0)
                    {
                        x = (x * x) % mod;
                        p >>= 1;
                    }
                    else
                    {
                        result = (result * x) % mod;
                        p--;
                    }
                }
                return result;
            }

            public static long Gcd(long a, long b)
            {
                return b == 0 ? a : Gcd(b, a % b);
            }

            public static List<int> GetPrimes(int n)
            {
                bool[] isPrime = Enumerable.Repeat(true, n + 1).ToArray();
                isPrime[0] = false;
                isPrime[1] = false;
                for (int i = 2; i <= n; i++)
                {
                    if (isPrime[i])
                    {
                        for (int j = i + i; j <= n; j += i)
                        {
                            isPrime[j] = false;
                        }
                    }
                }
                List<int> primes = new List<int>();
                for (int i = 0; i <= n; i++)
                {
                    if (isPrime[i])
                    {
                        primes.Add(i);
                    }
                }
                return primes;
            }
        }

        public static void Main()
        {
            //Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

#if DEBUG
            //Reader = Console.In; Writer = Console.Out;
            Reader = File.OpenText("input.txt"); Writer = File.CreateText("output.txt");
#else
            Reader = Console.In; Writer = Console.Out;
#endif

            Solve();

            Reader.Close();
            Writer.Close();
        }

        public static IOrderedEnumerable<TSource> OrderByWithShuffle<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
        {
            return source.Shuffle().OrderBy(keySelector);
        }

        public static T[] Shuffle<T>(this IEnumerable<T> source)
        {
            T[] result = source.ToArray();
            Random rnd = new Random();
            for (int i = result.Length - 1; i >= 1; i--)
            {
                int k = rnd.Next(i + 1);
                T tmp = result[k];
                result[k] = result[i];
                result[i] = tmp;
            }
            return result;
        }

        #region Read/Write

        private static TextReader Reader;

        private static TextWriter Writer;

        private static Queue<string> CurrentLineTokens = new Queue<string>();

        private static string[] ReadAndSplitLine()
        {
            return Reader.ReadLine().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
        }

        public static string ReadToken()
        {
            while (CurrentLineTokens.Count == 0)
                CurrentLineTokens = new Queue<string>(ReadAndSplitLine());
            return CurrentLineTokens.Dequeue();
        }

        public static string ReadLine()
        {
            return Reader.ReadLine();
        }

        public static int ReadInt()
        {
            return int.Parse(ReadToken());
        }

        public static long ReadLong()
        {
            return long.Parse(ReadToken());
        }

        public static double ReadDouble()
        {
            return double.Parse(ReadToken(), CultureInfo.InvariantCulture);
        }

        public static int[] ReadIntArray()
        {
            return ReadAndSplitLine().Select(int.Parse).ToArray();
        }

        public static long[] ReadLongArray()
        {
            return ReadAndSplitLine().Select(long.Parse).ToArray();
        }

        public static double[] ReadDoubleArray()
        {
            return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray();
        }

        public static int[][] ReadIntMatrix(int numberOfRows)
        {
            int[][] matrix = new int[numberOfRows][];
            for (int i = 0; i < numberOfRows; i++)
                matrix[i] = ReadIntArray();
            return matrix;
        }

        public static string[] ReadLines(int quantity)
        {
            string[] lines = new string[quantity];
            for (int i = 0; i < quantity; i++)
                lines[i] = Reader.ReadLine().Trim();
            return lines;
        }

        public static void WriteArray<T>(IEnumerable<T> array)
        {
            Writer.WriteLine(string.Join(" ", array));
        }

        #endregion
    }
}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;

namespace ReadWriteTemplate
{
    public static class Solver
    {
        private static void SolveCase()
        {
            int n = ReadInt();
            var v = ReadLongArray().ToList();
            var a = new LinkedList<long>(v);
            var node = a.First;
            while (node.Next != null)
            {
                if (node.Value == node.Next.Value)
                {
                    a.Remove(node.Next);
                    node.Value += 1;
                    if (node.Previous != null)
                    {
                        node = node.Previous;
                    }
                }
                else
                {
                    node = node.Next;
                }
            }

            Writer.WriteLine(a.Count);
            Writer.WriteLine(string.Join(" ", a));
        }

        public static void Solve()
        {
            // int T = ReadInt();
            // for (int i = 0; i < T; i++)
            // {
            //     Writer.Write("Case #{0}: ", i + 1);
                SolveCase();
            // }
        }

        public class Point
        {
            public Point(int x, int y)
            {
                X = x;
                Y = y;
            }

            public int X;

            public int Y;

            public double Length { get { return Math.Sqrt(X * X + Y * Y); } }

            protected bool Equals(Point other)
            {
                return X == other.X && Y == other.Y;
            }

            public override bool Equals(object obj)
            {
                if (ReferenceEquals(null, obj))
                {
                    return false;
                }
                if (ReferenceEquals(this, obj))
                {
                    return true;
                }
                /*if (obj.GetType() != this.GetType())
                {
                    return false;
                }*/
                return Equals((Point) obj);
            }

            public override int GetHashCode()
            {
                unchecked
                {
                    return (X * 397) ^ Y;
                }
            }

            public int Dist2(Point p)
            {
                return (p.X - X) * (p.X - X) + (p.Y - Y) * (p.Y - Y);
            }

            public static Point operator +(Point a, Point b)
            {
                return new Point(a.X + b.X, a.Y + b.Y);
            }

            public static Point operator -(Point a, Point b)
            {
                return new Point(a.X - b.X, a.Y - b.Y);
            }
        }

        public static class Standard
        {
            public static long Power(long x, long p, long mod)
            {
                long result = 1;
                while (p > 0)
                {
                    if ((p & 1) == 0)
                    {
                        x = (x * x) % mod;
                        p >>= 1;
                    }
                    else
                    {
                        result = (result * x) % mod;
                        p--;
                    }
                }
                return result;
            }

            public static long Gcd(long a, long b)
            {
                return b == 0 ? a : Gcd(b, a % b);
            }

            public static List<int> GetPrimes(int n)
            {
                bool[] isPrime = Enumerable.Repeat(true, n + 1).ToArray();
                isPrime[0] = false;
                isPrime[1] = false;
                for (int i = 2; i <= n; i++)
                {
                    if (isPrime[i])
                    {
                        for (int j = i + i; j <= n; j += i)
                        {
                            isPrime[j] = false;
                        }
                    }
                }
                List<int> primes = new List<int>();
                for (int i = 0; i <= n; i++)
                {
                    if (isPrime[i])
                    {
                        primes.Add(i);
                    }
                }
                return primes;
            }
        }

        public static void Main()
        {
            //Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

#if DEBUG
            //Reader = Console.In; Writer = Console.Out;
            Reader = File.OpenText("input.txt"); Writer = File.CreateText("output.txt");
#else
            Reader = Console.In; Writer = Console.Out;
#endif

            Solve();

            Reader.Close();
            Writer.Close();
        }

        public static IOrderedEnumerable<TSource> OrderByWithShuffle<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
        {
            return source.Shuffle().OrderBy(keySelector);
        }

        public static T[] Shuffle<T>(this IEnumerable<T> source)
        {
            T[] result = source.ToArray();
            Random rnd = new Random();
            for (int i = result.Length - 1; i >= 1; i--)
            {
                int k = rnd.Next(i + 1);
                T tmp = result[k];
                result[k] = result[i];
                result[i] = tmp;
            }
            return result;
        }

        #region Read/Write

        private static TextReader Reader;

        private static TextWriter Writer;

        private static Queue<string> CurrentLineTokens = new Queue<string>();

        private static string[] ReadAndSplitLine()
        {
            return Reader.ReadLine().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
        }

        public static string ReadToken()
        {
            while (CurrentLineTokens.Count == 0)
                CurrentLineTokens = new Queue<string>(ReadAndSplitLine());
            return CurrentLineTokens.Dequeue();
        }

        public static string ReadLine()
        {
            return Reader.ReadLine();
        }

        public static int ReadInt()
        {
            return int.Parse(ReadToken());
        }

        public static long ReadLong()
        {
            return long.Parse(ReadToken());
        }

        public static double ReadDouble()
        {
            return double.Parse(ReadToken(), CultureInfo.InvariantCulture);
        }

        public static int[] ReadIntArray()
        {
            return ReadAndSplitLine().Select(int.Parse).ToArray();
        }

        public static long[] ReadLongArray()
        {
            return ReadAndSplitLine().Select(long.Parse).ToArray();
        }

        public static double[] ReadDoubleArray()
        {
            return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray();
        }

        public static int[][] ReadIntMatrix(int numberOfRows)
        {
            int[][] matrix = new int[numberOfRows][];
            for (int i = 0; i < numberOfRows; i++)
                matrix[i] = ReadIntArray();
            return matrix;
        }

        public static string[] ReadLines(int quantity)
        {
            string[] lines = new string[quantity];
            for (int i = 0; i < quantity; i++)
                lines[i] = Reader.ReadLine().Trim();
            return lines;
        }

        public static void WriteArray<T>(IEnumerable<T> array)
        {
            Writer.WriteLine(string.Join(" ", array));
        }

        #endregion
    }
}


    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

      int n = Convert.ToInt32(Console.ReadLine());
		string s = Console.ReadLine();
		string [] a = s.Split(' ');
		int [] arr =new int[200040];

            int i;
            for (i=0; i < n; i++)
            {
                arr[i] =  Convert.ToInt32(a[i]);
               // Console.WriteLine(arr[i]);

                if(i>0 )
                {
            	while(i>0&&arr[i] == arr[i-1])
            	{
            		arr[i-1] = arr[i] +1;
            		i--;n--;
            	}
                }
            }
            Console.WriteLine(n);
            for (int k = 0; k < n; k++)
            {
                Console.Write(arr[k]);
                Console.Write(" ");
            }

        }

    }
}


    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

      int n = Convert.ToInt32(Console.ReadLine());
		string s = Console.ReadLine();
		string [] a = s.Split(' ');
		int [] arr =new int[200040];

            int j,i;
            for (i=0,j=0; i < n; j++,i++)
            {
                arr[i] =  Convert.ToInt32(a[j]);
              //  Console.WriteLine(n+" "+i+" "+arr[i]);

                if(i>0 )
                {
                  	while(i>0&&arr[i] == arr[i-1])
                  	{
                  		arr[i-1] = arr[i] +1;
                  		i--;n--;
                  	}
                }
            }
            Console.WriteLine(n);
            for (int k = 0; k < n; k++)
            {
                Console.Write(arr[k]);
                Console.Write(" ");
            }

        }

    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace Codeforces
{
   class A
   {
      static void Main(string[] args)
      {
         int n = int.Parse(Console.ReadLine());
         string[] token = Console.ReadLine().Split();

         int[] a = new int[n];

         for(int i=0;i<n;i++){
           a[i] = int.Parse(token[i]);
         }

         Stack<int> st = new Stack<int>();
         for (int i = 0; i < n; i++) {
             int x = a[i];
             while (st.Count > 0 && (int)st.Peek() == x) {
                 x = x + 1;
                 st.Pop();
             }
             st.Push(x);
         }

         int[] arr = st.ToArray();
         n = arr.Length;

         Console.WriteLine(n);

            for (int i = 0; i < arr.Length / 2; i++)
            {
                int tmp = arr[i];
                arr[i] = arr[arr.Length - i - 1];
                arr[arr.Length - i - 1] = tmp;
            }

        Console.WriteLine(string.Join(" ", arr));

      }

   }

}


using System;
using System.Linq;

public static class Program
{

	 public static void Main()
	 {
			int n = Console.ReadLine().Split(' ').Select(int.Parse).ToArray()[0];
			int[] a = new int[200005];
			int[] b = new int[200005];
			int[] c = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();

			int ind = 1;
			for(int i = n;i > 0;i--)a[i] = c[i - 1];
			b[1] = a[1];
			for(int i = 2;i <= n;i++){
			      int x = a[i];
				if(ind > 0 && x == b[ind]){
					while(ind > 0 && x == b[ind]){
						x++;
						b[ind] = x;
						ind--;
					}
					ind++;
				}
				else {
					ind++;
					b[ind] = a[i];
				}
			}
			Console.WriteLine(ind);
			for(int i = 1;i <= ind;i++){
				Console.Write(b[i]);
				Console.Write(' ');
			}
			Console.Write('\n');
	 }
}


using System;

class Program {
    static void Main() {
        int n = Convert.ToInt32(Console.ReadLine());
        string[] s = Console.ReadLine().Split(new char[] { ' ' });
        int[] a = new int[s.Length];
        int[] st = new int[s.Length];
        int sti = 0;
        for (int i = 0; i < n; ++i)
            a[i] = Convert.ToInt32(s[i]);
        for (int i = 0; i < n; ++i) {
            st[sti] = a[i];
            ++sti;
            while (sti >= 2 && st[sti - 2] == st[sti - 1]) {
                --sti;
                ++st[sti - 1];
            }
        }
        Console.WriteLine(sti);
        for (int i = 0; i < sti; ++i)
            Console.Write(st[i] + " ");
    }
}


package main
import "fmt"

func main(){
  var n int
  fmt.Scan(&n)
	a := make([]int, n+1, n+1)

  ptr := 0

  for i := 0; i < n; i++ {
    ptr++
    fmt.Scan(&a[ptr])
    for j := 0; ptr >= 2 && a[ptr] == a[ptr-1]; j++ {
      ptr--

      a[ptr]++
    }

  }

  fmt.Println(ptr)

  for i := 1; i <= ptr; i++ {
    fmt.Println(a[i])
  }

}


package main
import (
    "bufio"
	"fmt"
	"os"
    "strconv"
	)

func main(){
  var n int
  input := bufio.NewReader(os.Stdin)
  fmt.Fscan(input, &n)
  var a[200005] int

  ptr := 0

  for i := 0; i < n; i++ {
    ptr++
    fmt.Fscan(input, &a[ptr])
    for j := 0; ptr >= 2 && a[ptr] == a[ptr-1]; j++ {
      ptr--
      a[ptr]++
    }

  }

  out := bufio.NewWriter(os.Stdout)

  out.WriteString(strconv.Itoa(ptr) + "\n")

  for i := 1; i <= ptr; i++ {

    out.WriteString(strconv.Itoa(a[i]) + "\n")

  }
  out.Flush()

}


package main
import (
    "bufio"
	"fmt"
	"os"
	"bytes"
	)

func main(){
  var n int
  input := bufio.NewReader(os.Stdin)
  fmt.Fscan(&n)
  aa := make([]int, n)

  ptr := 0

  for i := 0; i < n; i++ {
    ptr++
    fmt.Fscan(&a[ptr])
    for j := 0; ptr >= 2; j++ {
      ptr--
      a[ptr]++
    }

  }

  fmt.FPrintln(ptr)

  for i := 1; i <= n; i++ {
    fmt.FPrintln(a[i])
  }

}


package main

import (
    "io/ioutil"
    "bufio"
    "os"
    "strconv"
)

func main() {
    out := bufio.NewWriter(os.Stdout)
    var n int
    ints := getInts()
    n, ints = ints[0], ints[1:]
    ret := make([]int, n, n)
    ridx := 0
    for i := 0; i < n; i++ {
        for ridx > 0 && ret[ridx-1] == ints[i] {
            ints[i]++
            ridx--
        }
        ret[ridx] = ints[i]
        ridx += 1
    }

    out.WriteString(strconv.Itoa(ridx) + "\n")
    for i := 0; i < ridx; i++ {
        out.WriteString(strconv.Itoa(ret[i]) + "\n")
    }
    out.Flush()
}

func getInts() []int {
    var buf []byte
    buf, _ = ioutil.ReadAll(os.Stdin)
    var ints []int
    num := int(0)
    found := false
    for _, v := range buf {
        if '0' <= v && v <= '9' {
            num = 10*num + int(v - '0')
            found = true
        } else if found {
            ints = append(ints, num)
            found = false
            num = 0
        }
    }
    if found {
        ints = append(ints, num)
        found = false
        num = 0
    }
    return ints
}


package main

import "fmt"

func main() {
    var n int
    fmt.Scan(&n)
    ss := make([]int, n, n)
    for i := 0; i < n; i++ {
        fmt.Scan(&ss[i])
    }
    ret := make([]int, n, n)
    ridx := 0
    for i := 0; i < n; i++ {
        for ridx > 0 && ret[ridx-1] == ss[i] {
            ss[i] += 1
            ridx -= 1
        }
        ret[ridx] = ss[i]
        ridx += 1
    }

    fmt.Println(ridx);
    for i := 0; i < ridx; i++ {
        fmt.Println(ret[i])
    }
}


package main

import "fmt"

func main() {
    var n int
    fmt.Scan(&n)
    ret := make([]int, n, n)
    ridx := 0
    for i := 0; i < n; i++ {
        var x int
        fmt.Scan(&x)
        for ridx > 0 && ret[ridx-1] == x {
            x++
            ridx--
        }
        ret[ridx] = x
        ridx += 1
    }

    fmt.Println(ridx);
    for i := 0; i < ridx; i++ {
        fmt.Println(ret[i])
    }
}


package main

import "fmt"

const N = 200100

func main() {
	var n, x, k int
	var q [N]int

	fmt.Scan(&n)

	k = 0
	for i := 0; i < n; i++ {
		fmt.Scan(&x)

		q[k] = x
		k++

		for k > 1 && q[k-2] == q[k-1] {
			q[k-2]++
			k--
		}
	}

	fmt.Println(k)
	for i := 0; i < k; i++ {
		fmt.Printf("%d ", q[i])
	}
	fmt.Print("\n")
}


package main

import (
	"bufio"
	"fmt"
	"os"
)

const N = 200100

func main() {
	var n, x, k int
	var q [N]int

	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n)

	k = 0
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &x)

		q[k] = x
		k++

		for k > 1 && q[k-2] == q[k-1] {
			q[k-2]++
			k--
		}
	}

	out := bufio.NewWriter(os.Stdout)

	fmt.Fprintln(out, k)
	for i := 0; i < k; i++ {
		fmt.Fprintf(out, "%d ", q[i])
	}
	fmt.Fprint(out, "\n")
	out.Flush()
}


package main

import "fmt"

func main() {
	q := make([]int, 0)

	var n, x int

	fmt.Scan(&n)

	for i := 0; i < n; i++ {
		fmt.Scan(&x)

		for len(q) > 0 && x == q[len(q)-1] {
			// fmt.Println(x)
			q = q[:len(q)-1]
			x++
		}

		q = append(q, x)
		// fmt.Println(q)
	}

	fmt.Println(len(q))
	for _, y := range q {
		fmt.Printf("%d ", y)
	}

	fmt.Print("\n")
}


package main

import (
	"bufio"
	"fmt"
	"os"
)

const N = 200100

func main() {
	var n, x, k int
	var q [N]int

	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n)

	k = 0
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &x)

		q[k] = x
		k++

		for k > 1 && q[k-2] == q[k-1] {
			q[k-2]++
			k--
		}
	}

	fmt.Println(k)
	for i := 0; i < k; i++ {
		fmt.Printf("%d ", q[i])
	}
	fmt.Print("\n")
}


package main

import "fmt"

func main(){
	var n int
	var arr[200005] int
	var arr2[200005] int
	var ind int
	fmt.Scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &arr[i])
	}
	ind = 0
	for i := 0; i < n; i++ {
	    arr2[ind] = arr[i]
	    ind++
	    for j := ind-1; j > 0; j-- {
	        if(arr2[j] == arr2[j-1]) {
	            arr2[j-1]++
	            ind--
	        } else {
	            break
	        }
	    }
	}
	fmt.Print(ind)
	fmt.Print("\n")
	for i := 0; i < ind; i++ {
	    fmt.Print(arr2[i]);
	    fmt.Print(" ")
	}
}


package main

import (
    "bufio"
	"fmt"
	"os"
	)

func main(){
	var n int
	var arr[200005] int
	var arr2[200005] int
	var ind int
	input := bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &n)
	for i := 0; i < n; i++ {
	fmt.Fscan(input, &arr[i])
	}
	ind = 0
	for i := 0; i < n; i++ {
	    arr2[ind] = arr[i]
	    ind++
	    for j := ind-1; j > 0; j-- {
	        if(arr2[j] == arr2[j-1]) {
	            arr2[j-1]++
	            ind--
	        } else {
	            break
	        }
	    }
	}
	fmt.Print(ind)
	fmt.Print("\n")
	for i := 0; i < ind; i++ {
	    fmt.Print(arr2[i]);
	    fmt.Print(" ")
	}
}


package main

import (
    "bufio"
	"fmt"
	"os"
	)

func main(){
	var n int
	var arr[200005] int
	var arr2[200005] int
	var ind int
	input := bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &n)
	for i := 0; i < n; i++ {
	fmt.Fscan(input, &arr[i])
	}
	ind = 0
	for i := 0; i < n; i++ {
	    arr2[ind] = arr[i]
	    ind++
	    for j := ind-1; j > 0; j-- {
	        if(arr2[j] == arr2[j-1]) {
	            arr2[j-1]++
	            ind--
	        } else {
	            break
	        }
	    }
	}
	var buf bytes.Buffer
	fmt.Fprint(&buf, ind)
	fmt.Fprint(&buf,"\n")
	for i := 0; i < ind; i++ {
	    fmt.Fprint(&buf,arr2[i]);
	    fmt.Fprint(&buf," ")
	}
	fmt.Println(buf.String())
}


package main

import (
    "bufio"
	"fmt"
	"os"
	"bytes"
	)

func main(){
	var n int
	var arr[200005] int
	var arr2[200005] int
	var ind int
	input := bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &n)
	for i := 0; i < n; i++ {
	fmt.Fscan(input, &arr[i])
	}
	ind = 0
	for i := 0; i < n; i++ {
	    arr2[ind] = arr[i]
	    ind++
	    for j := ind-1; j > 0; j-- {
	        if(arr2[j] == arr2[j-1]) {
	            arr2[j-1]++
	            ind--
	        } else {
	            break
	        }
	    }
	}
	var buf bytes.Buffer
	fmt.Fprint(&buf, ind)
	fmt.Fprint(&buf,"\n")
	for i := 0; i < ind; i++ {
	    fmt.Fprint(&buf,arr2[i]);
	    fmt.Fprint(&buf," ")
	}
	fmt.Println(buf.String())
}


[n] = map(int, input().split())
xs = map(int, input().split())

stk = []

def push(x):
    if(len(stk) == 0 or stk[len(stk)-1] != x):
        stk.append(x)
    else:
        stk.pop()
        push(x+1)

for x in xs:
    push(x)

print(len(stk))
for x in stk:
    print(x,end=" ")
print("")


[n] = map(int, input().split())

try:
    xs = map(int, input().split())
except:
    xs = []

stk = []

def push(x):
    while(stk and stk[-1] == x):
        stk.pop()
        x += 1
    stk.append(x)

for x in xs:
    push(x)

print(len(stk))
for x in stk:
    print(x,end=" ")
print("")


[n] = map(int, input().split())

try:
    xs = map(int, input().split())
except:
    xs = []

stk = []

def push(x):
    if(not stk or stk[-1] != x):
        stk.append(x)
    else:
        stk.pop()
        push(x+1)

for x in xs:
    push(x)

print(len(stk))
for x in stk:
    print(x,end=" ")
print("")


n = int(input())
xs = map(int, input().strip().split(" "))

res = []
currentNum = -1

for i in xs:
    if i == currentNum:
        currentNum += 1
        while len(res) > 0 and res[-1] == currentNum:
            currentNum += 1
            res.pop()
    else:
        if currentNum > 0:
            res.append(currentNum)
        currentNum = i

res.append(currentNum)

print(len(res))
print(' '.join(map(str, res)))


#include<stdio.h>
int n;
int prv[200002],nxt[200001];
int a[200001];
int main(){
	scanf("%d",&n);
	for(int i=1; i<=n ;i++){
		scanf("%d",a+i);
		nxt[i]=i+1;
		prv[i]=i-1;
	}
	int cur=1;
	int ans=n;
	while(nxt[cur]!=n+1){

		if(a[cur]!=a[nxt[cur]]){
			cur=nxt[cur];
			continue;
		}
		a[cur]++;
		nxt[cur]=nxt[nxt[cur]];
		prv[nxt[cur]]=cur;
		if(cur!=1) cur=prv[cur];
		ans--;
	}
	printf("%d\n",ans);
	cur=1;
	while(cur!=n+1){
		printf("%d ",a[cur]);
		cur=nxt[cur];
	}
}


#include <stdio.h>
const int MAX = 2*1e5+1;
int main (){
int i,n;
struct list{ struct list *next; int a;}a[MAX], *ptr, *cur;

scanf("%d",&n);
for (i=0;i<n;i++){
	scanf("%d", &(a[i].a) );
	a[i].next = &a[i+1];
	}
a[n-1].next = NULL;
ptr = cur = &a[0];
while (ptr->next != NULL )
	for (ptr = &a[0]; ptr->next != NULL; cur = ptr, ptr = ptr->next)
		if (ptr->a ==  ptr->next->a ){
			ptr->a++;
			ptr->next = ptr->next->next;
			n--;
			ptr = cur;
			break;
		}

printf("%d\n",n);
ptr=&a[0];
for (;ptr != NULL ;ptr=ptr->next,i++){
	printf ("%i ",ptr->a);
	}

printf("\n");
return 0;
}


#include <stdio.h>
const int MAX = 2*1e5+1;
int main (){
int i,n;
struct list{ struct list *next, *prev; int a;}a[MAX], *ptr;

scanf("%d",&n);
for (i=0;i<n;i++){
	scanf("%d", &(a[i].a) );
	a[i].next = &a[i+1];
	a[i+1].prev = &a[i];
	}
a[0].prev = NULL;
a[n-1].next = NULL;
ptr = &a[0];
while (ptr->next != NULL && ptr != NULL && n>1 ){
	if (ptr->a ==  ptr->next->a ){
		ptr->a++;
		ptr->next = ptr->next->next;
		if( ptr->next!=NULL)
			ptr->next->prev = ptr;
		n--;
		if (ptr != &a[0])
			ptr = ptr->prev;
		}
	else
		ptr = ptr->next;
}

printf("%d\n",n);
ptr=&a[0];
for (;ptr != NULL ;ptr=ptr->next){
	printf ("%i ",ptr->a);
	}

printf("\n");
return 0;
}


#include <stdio.h>
const int MAX = 2*1e5+1;
int main (){
int i,n;
struct list{ struct list *next; int a;}a[MAX], *ptr, *cur;

scanf("%d",&n);
for (i=0;i<n;i++){
	scanf("%d", &(a[i].a) );
	a[i].next = &a[i+1];
	}
a[n-1].next = NULL;
ptr = cur = &a[0];
while (ptr->next != NULL )
	for (ptr = &a[0]; ptr->next != NULL; cur = ptr, ptr = ptr->next)
		if (ptr->a ==  ptr->next->a ){
			ptr->a++;
			ptr->next = ptr->next->next;
			n--;
			//if (ptr != &a[0]) ptr = cur;
			break;
		}

printf("%d\n",n);
ptr=&a[0];
for (;ptr != NULL ;ptr=ptr->next,i++){
	printf ("%i ",ptr->a);
	}

printf("\n");
return 0;
}


#include <stdio.h>
const int MAX = 2*1e5+1;
int main (){
int i,n;
struct list{ struct list *next; int a;}a[MAX], *ptr, *cur;

scanf("%d",&n);
for (i=0;i<n;i++){
	scanf("%d", &(a[i].a) );
	a[i].next = &a[i+1];
	}
a[n-1].next = NULL;
ptr = cur = &a[0];
while (ptr->next != NULL )
	for (; ptr->next != NULL; cur = ptr, ptr = ptr->next)
		if (ptr->a ==  ptr->next->a ){
			ptr->a++;
			ptr->next = ptr->next->next;
			n--;
			if (ptr != &a[0]) ptr = cur;
			break;
		}

printf("%d\n",n);
ptr=&a[0];
for (;ptr != NULL ;ptr=ptr->next,i++){
	printf ("%i ",ptr->a);
	}

printf("\n");
return 0;
}


#include <stdio.h>

const int maxn = 200005;

int main(){
    int n;
    scanf("%d",&n);

    int input[maxn];
    for (int i = 0; i < n; ++i) {
        scanf("%d", &input[i]);
    }

    int sz = 0;
    int stack[maxn];

    for (int i = 0; i < n; ++i) {
        stack[sz] = input[i];
        sz++;

        while (sz > 1 && stack[sz - 1] == stack[sz - 2]) {
            sz--;
            stack[sz - 1]++;
        }
    }

    printf("%d\n", sz);
    for (int i = 0; i < sz; ++i) {
        if (i) printf(" ");
        printf("%d", stack[i]);
    }
    printf("\n");

    return 0;
}


#include<stdio.h>

int main()
{
    int n;
    scanf("%d",&n);
    int a[n];
    int last = 0;
    int i;
    for(i=0; i<n; i++)
    {
    	int x;
        scanf("%d", &x);
        a[last++] = x;
        while (last > 1 && a[last-1] == a[last-2]) {
        	a[last-2]++;
        	last--;
        }

    }
    printf("%d\n", last);
    for (i=0;i<last;i++)
    {
	    printf("%d ",a[i]);
    }
}


#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main() {

    int k;
    scanf("%i", &k);
    int* numbers = calloc(k,sizeof(int));
    int* next = calloc(k,sizeof(int));
    int* prev = calloc(k,sizeof(int));

    for (int i=0; i<k; ++i)
    {
        scanf("%i",&numbers[i]);
        next[i] = i+1;
        prev[i] = i-1;
    }
    next[k-1] = -1;
    int j = 0;
    while (next[j]!=-1)
    {
        if (numbers[j] == numbers[next[j]])
        {
            numbers[j] += 1;
            if (next[next[j]]!=-1)
                prev[next[next[j]]]=j;
            next[j] = next[next[j]];

            if (prev[j]!=-1)
                j = prev[j];
        }else{
            j = next[j];
        }

    }
    int count = 1;
    j=0;
    while (next[j]!=-1)
    {
        j = next[j];
        count += 1;
    }
    printf( "%i\n",count );
    j = 0;
    printf("%i",numbers[j]);
    while (next[j]!=-1)
    {
        j = next[j];
        printf(" %i",numbers[j]);
        //cout << " " << numbers[j];
    }
}


#include <stdio.h>
#include <iostream>
#include <stdlib.h>

using namespace std;

bool check(int t1 , int t2 , int t3);

long long int n , k ,d1 ,d2;

int main(){

int t;
cin>>t;

for(int game=1;game<=t ;game++){

    cin>>n>>k>>d1>>d2;

    if( check(d1,0ll,d2) ){cout<<"yes"<<endl; continue;}
    else if( check(0ll,d1,d1+d2) ){cout<<"yes"<<endl; continue;}
    else if( check(d1+d2,d2,0ll) ){cout<<"yes"<<endl; continue;}

    else if( check(abs(d2-d1),d2,0ll) ){cout<<"yes"<<endl; continue;}
    else if( check(0ll,d1,abs(d1-d2))){cout<<"yes"<<endl; continue;}

    cout<<"no"<<endl;

}

return 0 ;

}

bool check(int t1 , int t2 , int t3){

    if( k< t1+t2+t3 || (k-(t1+t2+t3))%3!=0  ) return false;

    long long int maxim=max(t1,max(t2,t3));
    long long cal= (n-k)-(3*maxim-(t1+t2+t3));

    if(  0<=cal && cal%3==0) return true;

    return false;
}


#include <stdio.h>
#include <iostream>
#include <stdlib.h>

using namespace std;

bool check( long long int t1 , long long int t2 , long  long int t3);

long long int n , k ,d1 ,d2;

int main(){

int t;
cin>>t;

for(int game=1;game<=t ;game++){

    cin>>n>>k>>d1>>d2;

    if( check(d1,0ll,d2) ){cout<<"yes"<<endl; continue;}
    else if( check(0ll,d1,d1+d2) ){cout<<"yes"<<endl; continue;}
    else if( check(d1+d2,d2,0ll) ){cout<<"yes"<<endl; continue;}
    else if( check(d2-d1,d2,0ll) ){cout<<"yes"<<endl; continue;}
    else if( check(0ll,d1,d1-d2)){cout<<"yes"<<endl; continue;}

    cout<<"no"<<endl;

}

return 0 ;

}

bool check(  long long int t1 , long long int t2 , long long int t3){

    if(t1<0 || t2<0 || t3<0) return false;
    if( k<t1+t2+t3 || (k-(t1+t2+t3))%3!=0  ) return false;

    long long int maxim=max(t1,max(t2,t3));
    long long cal= (n-k)-(3*maxim-(t1+t2+t3));

    if(  0<=cal && cal%3==0) return true;

    return false;
}


#include <stdio.h>
#include <iostream>
#include <stdlib.h>

using namespace std;

bool check( long long int t1 , long long int t2 , long  long int t3);

long long int n , k ,d1 ,d2;

int main(){

int t;
cin>>t;

for(int game=1;game<=t ;game++){

    cin>>n>>k>>d1>>d2;

    if( check(d1,0ll,d2) ){cout<<"yes"<<endl; continue;}
    else if( check(0ll,d1,d1+d2) ){cout<<"yes"<<endl; continue;}
    else if( check(d1+d2,d2,0ll) ){cout<<"yes"<<endl; continue;}
    else if( check(d2-d1,d2,0ll) ){cout<<"yes"<<endl; continue;}
    else if( check(0ll,d1,d1-d2)){cout<<"yes"<<endl; continue;}

    cout<<"no"<<endl;

}

return 0 ;

}

bool check(  long long int t1 , long long int t2 , long long int t3){

    if( k<t1+t2+t3 || (k-(t1+t2+t3))%3!=0  ) return false;

    long long int maxim=max(t1,max(t2,t3));
    long long int cal= (n-k)-(3*maxim-(t1+t2+t3));

    if(  0<=cal && cal%3==0) return true;

    return false;
}


#include <stdio.h>
#include <iostream>
#include <stdlib.h>

using namespace std;

bool check( long long int t1 , long long int t2 , long  long int t3);

long long int n , k ,d1 ,d2;

int main(){

int t;
cin>>t;

for(int game=1;game<=t ;game++){

    cin>>n>>k>>d1>>d2;

    long long int dm=max(d1,d2);

    if( check(d1,0ll,d2) ){cout<<"yes"<<endl; continue;}
    else if( check(0ll,d1,d1+d2) ){cout<<"yes"<<endl; continue;}
    else if( check(d1+d2,d2,0ll) ){cout<<"yes"<<endl; continue;}
    else if( check(dm-d1,dm,dm-d2) ){cout<<"yes"<<endl; continue;}

    cout<<"no"<<endl;

}

return 0 ;

}

bool check(  long long int t1 , long long int t2 , long long int t3){

    if( k<t1+t2+t3 || (k-(t1+t2+t3))%3!=0  ) return false;

    long long int maxim=max(t1,max(t2,t3));
    long long int cal= (n-k)-(3*maxim-(t1+t2+t3));

    if(  0<=cal && cal%3==0) return true;

    return false;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <list>
#include <stack>
#include <algorithm>
#include <string>
#include <cstdint>

using namespace std;

bool T( uint64_t n, uint64_t k, uint64_t v, uint64_t w )
{
	if( k < w ||
		( k - w ) % 3 )
	{
		return false;
	}

	uint64_t m = n - k;

	if( m < v )
	{
		return false;
	}

	m -= v;
	return m % 3 == 0;
}

uint32_t main()
{
	uint32_t t;
	cin >> t;

	while( t-- )
	{
		uint64_t n, k, d1, d2;
		cin >> n >> k >> d1 >> d2;

		uint64_t dmax = max( d1, d2 );
		uint64_t dmin = min( d1, d2 );

		if( k >= dmax )
		{
			vector< bool > v;

			if( !dmin )
			{
				v.push_back( T( n, k, 2 * dmax, dmax ) );
				v.push_back( T( n, k, dmax, 2 * dmax ) );
			}
			else
			{
				v.push_back( T( n, k, dmin + 2 * ( dmax - dmin ), dmin + dmax ) );
				v.push_back( T( n, k, dmin + dmax, dmax + dmax - dmin ) );
				v.push_back( T( n, k, dmin + 2 * dmax, 2 * dmin + dmax ) );
				v.push_back( T( n, k, 2 * dmin + dmax, dmin + 2 * dmax ) );
			}

			for( uint32_t i = 0; i < v.size(); i++ )
			{
				if( v[i] )
				{
					cout << "yes" << endl;
					goto NEXT;
				}
			}
		}

		cout << "no" << endl;
NEXT:
		;
	}

	return EXIT_SUCCESS;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <list>
#include <stack>
#include <algorithm>
#include <string>
#include <cstdint>

using namespace std;

bool T( uint64_t n, uint64_t k, uint64_t v, uint64_t w )
{
	if( k < w )
	{
		return false;
	}

	uint64_t m = n - k;

	if( m < v )
	{
		return false;
	}

	m -= v;
	return m % 3 == 0;
}

uint32_t main()
{
	uint32_t t;
	cin >> t;

	while( t-- )
	{
		uint64_t n, k, d1, d2;
		cin >> n >> k >> d1 >> d2;

		uint64_t dmax = max( d1, d2 );
		uint64_t dmin = min( d1, d2 );

		if( k >= dmax )
		{
			vector< pair< uint64_t, uint64_t > > v;

			if( !dmin )
			{
				v.push_back( make_pair( 2 * dmax, dmax ) );
				v.push_back( make_pair( dmax, 2 * dmax ) );
			}
			else
			{
				v.push_back( make_pair( dmin + 2 * ( dmax - dmin ), dmin + dmax ) );
				v.push_back( make_pair( dmin + dmax, dmax + dmax - dmin ) );
				v.push_back( make_pair( dmin + 2 * dmax, 2 * dmin + dmax ) );
				v.push_back( make_pair( 2 * dmin + dmax, dmin + 2 * dmax ) );
			}

			for( uint32_t i = 0; i < v.size(); i++ )
			{
				if( T( n, k, v[i].first, v[i].second ) )
				{
					cout << "yes" << endl;
					goto NEXT;
				}
			}
		}

		cout << "no" << endl;
NEXT:
		;
	}

	return EXIT_SUCCESS;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <list>
#include <stack>
#include <algorithm>
#include <string>
#include <cstdint>

using namespace std;

bool T( uint64_t n, uint64_t k, uint64_t v )
{
	uint64_t m = n - k;

	if( m < v )
	{
		return false;
	}

	m -= v;
	return m % 3 == 0;
}

uint32_t main()
{
	uint32_t t;
	cin >> t;

	while( t-- )
	{
		uint64_t n, k, d1, d2;
		cin >> n >> k >> d1 >> d2;

		if( k < d1 || k < d2 )
		{
			cout << "no" << endl;
		}
		else
		{
			if( !d1 || !d2 )
			{
				uint64_t v = 2 * max( d1, d2 );
				cout << ( T( n, k, v ) ? "yes" : "no" ) << endl;
			}
			else
			{
				uint64_t v1 = max( d1, d2 ) + max( d1, d2 ) - min( d1, d2 );
				uint64_t v2 = 2 * d1 + d2;
				uint64_t v3 = d1 + 2 * d2;
				uint64_t v4 = d1 + d2;

				bool r1 = T( n, k, v1 );
				bool r2 = T( n, k, v2 );
				bool r3 = T( n, k, v3 );
				bool r4 = T( n, k, v4 );

				cout << ( ( r1 || r2 || r3 || r4 ) ? "yes" : "no" ) << endl;
			}
		}
	}

	return EXIT_SUCCESS;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <list>
#include <stack>
#include <algorithm>
#include <string>
#include <cstdint>

using namespace std;

bool T( uint64_t n, uint64_t k, uint64_t v )
{
	uint64_t m = n - k;

	if( m < v )
	{
		return false;
	}

	m -= v;
	return m % 3 == 0;
}

uint32_t main()
{
	uint32_t t;
	cin >> t;

	while( t-- )
	{
		uint64_t n, k, d1, d2;
		cin >> n >> k >> d1 >> d2;

		if( k < d1 || k < d2 )
		{
			cout << "no" << endl;
		}
		else
		{
			if( !d1 || !d2 )
			{
				uint64_t v1 = max( d1, d2 );
				uint64_t v2 = 2 * max( d1, d2 );

				bool r1 = T( n, k, v1 );
				bool r2 = T( n, k, v2 );

				cout << ( ( r1 || r2 ) ? "yes" : "no" ) << endl;
			}
			else
			{
				uint64_t v1 = max( d1, d2 ) + max( d1, d2 ) - min( d1, d2 );
				uint64_t v2 = 2 * d1 + d2;
				uint64_t v3 = d1 + 2 * d2;
				uint64_t v4 = d1 + d2;

				bool r1 = T( n, k, v1 );
				bool r2 = T( n, k, v2 );
				bool r3 = T( n, k, v3 );
				bool r4 = T( n, k, v4 );

				cout << ( ( r1 || r2 || r3 || r4 ) ? "yes" : "no" ) << endl;
			}
		}
	}

	return EXIT_SUCCESS;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <list>
#include <stack>
#include <algorithm>
#include <string>
#include <cstdint>

using namespace std;

bool T( uint64_t m, uint64_t v )
{
	if( m < v )
	{
		return false;
	}

	m -= v;
	return m % 3 == 0;
}

uint32_t main()
{
	uint32_t t;
	cin >> t;

	while( t-- )
	{
		uint64_t n, k, d1, d2;
		cin >> n >> k >> d1 >> d2;

		uint64_t m = n - k;

		if( !d1 || !d2 )
		{
			uint64_t v = 2 * max( d1, d2 );
			cout << ( T( m, v ) ? "yes" : "no" ) << endl;
		}
		else
		{
			uint64_t v1 = max( d1, d2 ) + max( d1, d2 ) - min( d1, d2 );
			uint64_t v2 = 2 * d1 + d2;
			uint64_t v3 = d1 + 2 * d2;

			bool r1 = T( m, v1 );
			bool r2 = T( m, v2 );
			bool r3 = T( m, v3 );

			cout << ( ( r1 || r2 || r3 ) ? "yes" : "no" ) << endl;
		}
	}

	return EXIT_SUCCESS;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <list>
#include <stack>
#include <algorithm>
#include <string>
#include <cstdint>

using namespace std;

bool T( uint64_t n, uint64_t k, uint64_t v )
{
	if( k < v )
	{
		return false;
	}

	uint64_t m = n - k;

	if( m < v )
	{
		return false;
	}

	m -= v;
	return m % 3 == 0;
}

uint32_t main()
{
	uint32_t t;
	cin >> t;

	while( t-- )
	{
		uint64_t n, k, d1, d2;
		cin >> n >> k >> d1 >> d2;

		if( !d1 || !d2 )
		{
			uint64_t v = 2 * max( d1, d2 );
			cout << ( T( n, k, v ) ? "yes" : "no" ) << endl;
		}
		else
		{
			uint64_t v1 = max( d1, d2 ) + max( d1, d2 ) - min( d1, d2 );
			uint64_t v2 = 2 * d1 + d2;
			uint64_t v3 = d1 + 2 * d2;
			uint64_t v4 = d1 + d2;

			bool r1 = T( n, k, v1 );
			bool r2 = T( n, k, v2 );
			bool r3 = T( n, k, v3 );
			bool r4 = T( n, k, v4 );

			cout << ( ( r1 || r2 || r3 || r4 ) ? "yes" : "no" ) << endl;
		}
	}

	return EXIT_SUCCESS;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <list>
#include <stack>
#include <algorithm>
#include <string>
#include <cstdint>

using namespace std;

uint32_t main()
{
	uint32_t t;
	cin >> t;

	while( t-- )
	{
		uint64_t n, k, d1, d2;
		cin >> n >> k >> d1 >> d2;

		uint64_t m = n - k;
		uint32_t d = max( d1, d2 ) + max( d1, d2 ) - min( d1, d2 );

		if( m < d )
		{
			cout << "no";
		}
		else
		{
			m -= d;
			if( m % 3 == 0 )
			{
				cout << "yes";
			}
			else
			{
				cout << "no";
			}
		}

		cout << endl;
	}

	return EXIT_SUCCESS;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <list>
#include <stack>
#include <algorithm>
#include <string>
#include <cstdint>

using namespace std;

bool T( uint64_t n, uint64_t k, uint64_t v )
{
	if( k < v )
	{
		return false;
	}

	uint64_t m = n - k;

	if( m < v )
	{
		return false;
	}

	m -= v;
	return m % 3 == 0;
}

uint32_t main()
{
	uint32_t t;
	cin >> t;

	while( t-- )
	{
		uint64_t n, k, d1, d2;
		cin >> n >> k >> d1 >> d2;

		if( !d1 || !d2 )
		{
			uint64_t v = 2 * max( d1, d2 );
			cout << ( T( n, k, v ) ? "yes" : "no" ) << endl;
		}
		else
		{
			uint64_t v1 = max( d1, d2 ) + max( d1, d2 ) - min( d1, d2 );
			uint64_t v2 = 2 * d1 + d2;
			uint64_t v3 = d1 + 2 * d2;

			bool r1 = T( n, k, v1 );
			bool r2 = T( n, k, v2 );
			bool r3 = T( n, k, v3 );

			cout << ( ( r1 || r2 || r3 ) ? "yes" : "no" ) << endl;
		}
	}

	return EXIT_SUCCESS;
}


//#define _CRT_SECURE_NO_WARNINGS
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <map>
#include <set>
#include <queue>
#include <memory.h>
#include <time.h>
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
#include <map>
#include <algorithm>
#include <cstdio>
#include <set>
#include <queue>
#include <cassert>
#include <climits>
#include <deque>
#include <limits>
#define sz(x) int((x).size())
#define FOR(i,a,b) for(ll(i) = (a); (i) <= (b); ++(i))
#define ROF(i,a,b) for(ll(i) = (a); (i) >= (b); --(i))
#define rep(i,n) for (int(i) = 0; (i) < (n); ++(i))
#define fe(i,a) for (ll(i) = 0; (i) < int((a).size()); ++(i))
#define C(a) memset((a),0,sizeof(a))
#define pb push_back
#define ppb pop_back
#define all(c) (c).begin(), (c).end()
#define sqr(a) (a)*(a)
#define mp(a,b) make_pair((a), (b))
#define X first
#define Y second
#define file freopen("input.txt","r",stdin)
#define file2 freopen("output.txt", "w",stdout)
#define text freopen("input.txt","r",stdin); freopen("output.txt", "w",stdout)
using namespace std;
typedef vector<int> vint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
#define y1 vamdlsfmvldf
const int mod = (int)1e9 + 7;
const ll INF = (int)(1e9);
const int N = 100005;
ll n, k, d1, d2;
ll etal;
ll a, b, c;
bool is_cor(ll dop){
    return (dop <= etal && dop >= 0);
}
int main(){
    int t;
    cin >> t;
    rep(qw, t){
        scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2);
        if(n % 3 != 0){
            printf("no\n");
            continue;
        }
        etal = n / 3;
        a = (k + 2 * d1 + d2) / 3;
        b = a - d1;
        c = a - d1 - d2;
        if(is_cor(a) && is_cor(b) && is_cor(c)){
            printf("yes\n");
            continue;
        }
        a = (2 * d1 - d2 + k) / 3;
        b = a - d1;
        c = b + d2;
        if(is_cor(a) && is_cor(b) && is_cor(c)){
                printf("yes\n");
                continue;
        }
        b = (k + d1 + d2) / 3;
        a = b - d1;
        c = b - d2;
        if(is_cor(a) && is_cor(b) && is_cor(c)){
                printf("yes\n");
                continue;
        }
        c = (d1 + 2 * d2 + k) / 3;
        b = c - d2;
        a = b + d1;
        if(is_cor(a) && is_cor(b) && is_cor(c)){
                printf("yes\n");
                continue;
        }
        printf("no\n");
    }
    return 0;
}


//#define _CRT_SECURE_NO_WARNINGS
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <map>
#include <set>
#include <queue>
#include <memory.h>
#include <time.h>
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
#include <map>
#include <algorithm>
#include <cstdio>
#include <set>
#include <queue>
#include <cassert>
#include <climits>
#include <deque>
#include <limits>
#define sz(x) int((x).size())
#define FOR(i,a,b) for(ll(i) = (a); (i) <= (b); ++(i))
#define ROF(i,a,b) for(ll(i) = (a); (i) >= (b); --(i))
#define rep(i,n) for (int(i) = 0; (i) < (n); ++(i))
#define fe(i,a) for (ll(i) = 0; (i) < int((a).size()); ++(i))
#define C(a) memset((a),0,sizeof(a))
#define pb push_back
#define ppb pop_back
#define all(c) (c).begin(), (c).end()
#define sqr(a) (a)*(a)
#define mp(a,b) make_pair((a), (b))
#define X first
#define Y second
#define file freopen("input.txt","r",stdin)
#define file2 freopen("output.txt", "w",stdout)
#define text freopen("input.txt","r",stdin); freopen("output.txt", "w",stdout)
using namespace std;
typedef vector<int> vint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
#define y1 vamdlsfmvldf
const int mod = (int)1e9 + 7;
const ll INF = (int)(1e9);
const int N = 100005;
ll n, k, d1, d2;
ll et;
ll a, b, c;
bool is_cor(ll dop){
    if((dop >= 0)&&(dop <= et))
        return 1;
    return 0;
}
bool check(ll a, ll b, ll c){
    if(abs(a - b) != d1 || (abs(b - c) != d2) || (a + b + c != k)){
        return 0;
    }
    return 1;
}
int main(){
    int t;
    cin >> t;
    rep(qw, t){
        scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2);
        int flag = 0;
        et = n / 3;
        a = (k + 2 * d1 + d2) / 3;
        b = a - d1;
        c = a - d1 - d2;
        if(is_cor(a) && is_cor(b) && is_cor(c)){
            if(check(a, b, c))
            flag = 1;
        }
        a = (2 * d1 - d2 + k) / 3;
        b = a - d1;
        c = b + d2;
        if(is_cor(a) && is_cor(b) && is_cor(c)){
            if(check(a, b, c))
            flag = 1;
        }
        b = (k + d1 + d2) / 3;
        a = b - d1;
        c = b - d2;
        if(is_cor(a) && is_cor(b) && is_cor(c)){
            if(check(a, b, c))
            flag = 1;
        }
        c = (d1 + 2 * d2 + k) / 3;
        b = c - d2;
        a = b - d1;
        check(a, b, c);
        if(is_cor(a) && is_cor(b) && is_cor(c)){
            if(check(a, b, c))
            flag = 1;
        }
        if(n % 3 != 0){
            flag = 0;
        }
        if(flag) puts("yes");
        else puts("no");
    }
    return 0;
}


//#define _CRT_SECURE_NO_WARNINGS
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <map>
#include <set>
#include <queue>
#include <memory.h>
#include <time.h>
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
#include <map>
#include <algorithm>
#include <cstdio>
#include <set>
#include <queue>
#include <cassert>
#include <climits>
#include <deque>
#include <limits>
#define sz(x) int((x).size())
#define FOR(i,a,b) for(ll(i) = (a); (i) <= (b); ++(i))
#define ROF(i,a,b) for(ll(i) = (a); (i) >= (b); --(i))
#define rep(i,n) for (int(i) = 0; (i) < (n); ++(i))
#define fe(i,a) for (ll(i) = 0; (i) < int((a).size()); ++(i))
#define C(a) memset((a),0,sizeof(a))
#define pb push_back
#define ppb pop_back
#define all(c) (c).begin(), (c).end()
#define sqr(a) (a)*(a)
#define mp(a,b) make_pair((a), (b))
#define X first
#define Y second
#define file freopen("input.txt","r",stdin)
#define file2 freopen("output.txt", "w",stdout)
#define text freopen("input.txt","r",stdin); freopen("output.txt", "w",stdout)
using namespace std;
typedef vector<int> vint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
#define y1 vamdlsfmvldf
const int mod = (int)1e9 + 7;
const ll INF = (int)(1e9);
const int N = 100005;
ll n, k, d1, d2;
ll etal;
ll a, b, c;
bool is_cor(ll dop){
    return (dop <= etal && dop >= 0);
}
int main(){
    int t;
    cin >> t;
    rep(qw, t){
        scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2);
        if(n % 3 != 0){
            printf("no\n");
            continue;
        }
        etal = n / 3;
        a = (k + 2 * d1 + d2) / 3;
        b = a - d1;
        c = a - d1 - d2;
        if(is_cor(a) && is_cor(b) && is_cor(c)){
            printf("yes\n");
            continue;
        }
        a = (2 * d1 - d2 + k) / 3;
        b = a - d1;
        c = b + d2;
        if(is_cor(a) && is_cor(b) && is_cor(c)){
                printf("yes\n");
                continue;
        }
        b = (k + d1 + d2) / 3;
        a = b - d1;
        c = b - d2;
        if(is_cor(a) && is_cor(b) && is_cor(c)){
                printf("yes\n");
                continue;
        }
        c = (d1 + 2 * d2 + k) / 3;
        b = c - d2;
        a = b - d1;
        if(is_cor(a) && is_cor(b) && is_cor(c)){
                printf("yes\n");
                continue;
        }
        printf("no\n");
    }
    return 0;
}


//#define _CRT_SECURE_NO_WARNINGS
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <map>
#include <set>
#include <queue>
#include <memory.h>
#include <time.h>
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
#include <map>
#include <algorithm>
#include <cstdio>
#include <set>
#include <queue>
#include <cassert>
#include <climits>
#include <deque>
#include <limits>
#define sz(x) int((x).size())
#define FOR(i,a,b) for(ll(i) = (a); (i) <= (b); ++(i))
#define ROF(i,a,b) for(ll(i) = (a); (i) >= (b); --(i))
#define rep(i,n) for (int(i) = 0; (i) < (n); ++(i))
#define fe(i,a) for (ll(i) = 0; (i) < int((a).size()); ++(i))
#define C(a) memset((a),0,sizeof(a))
#define pb push_back
#define ppb pop_back
#define all(c) (c).begin(), (c).end()
#define sqr(a) (a)*(a)
#define mp(a,b) make_pair((a), (b))
#define X first
#define Y second
#define file freopen("input.txt","r",stdin)
#define file2 freopen("output.txt", "w",stdout)
#define text freopen("input.txt","r",stdin); freopen("output.txt", "w",stdout)
using namespace std;
typedef vector<int> vint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
#define y1 vamdlsfmvldf
const int mod = (int)1e9 + 7;
const ll INF = (int)(1e9);
const int N = 100005;
ll n, k, d1, d2;
ll etal;
ll a, b, c;
bool is_cor(ll dop){
    return (dop <= etal && dop >= 0);
}
int main(){
    int t;
    cin >> t;
    rep(qw, t){
        scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2);
        if(n % 3 != 0){
            printf("yes\n");
            continue;
        }
        etal = n / 3;
        a = (k + 2 * d1 + d2) / 3;
        b = a - d1;
        c = a - d1 - d2;
        if(is_cor(a) && is_cor(b) && is_cor(c)){
            printf("yes\n");
            continue;
        }
        a = (2 * d1 - d2 + k) / 3;
        b = a - d1;
        c = b + d2;
        if(is_cor(a) && is_cor(b) && is_cor(c)){
                printf("yes\n");
                continue;
        }
        b = (k + d1 + d2) / 3;
        a = b - d1;
        c = b - d2;
        if(is_cor(a) && is_cor(b) && is_cor(c)){
                printf("yes\n");
                continue;
        }
        c = (d1 + 2 * d2 + k) / 3;
        b = c - d2;
        a = b + d1;
        if(is_cor(a) && is_cor(b) && is_cor(c)){
                printf("yes\n");
                continue;
        }
        printf("no\n");
    }
    return 0;
}


//#define _CRT_SECURE_NO_WARNINGS
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <map>
#include <set>
#include <queue>
#include <memory.h>
#include <time.h>
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
#include <map>
#include <algorithm>
#include <cstdio>
#include <set>
#include <queue>
#include <cassert>
#include <climits>
#include <deque>
#include <limits>
#define sz(x) int((x).size())
#define FOR(i,a,b) for(ll(i) = (a); (i) <= (b); ++(i))
#define ROF(i,a,b) for(ll(i) = (a); (i) >= (b); --(i))
#define rep(i,n) for (int(i) = 0; (i) < (n); ++(i))
#define fe(i,a) for (ll(i) = 0; (i) < int((a).size()); ++(i))
#define C(a) memset((a),0,sizeof(a))
#define pb push_back
#define ppb pop_back
#define all(c) (c).begin(), (c).end()
#define sqr(a) (a)*(a)
#define mp(a,b) make_pair((a), (b))
#define X first
#define Y second
#define file freopen("input.txt","r",stdin)
#define file2 freopen("output.txt", "w",stdout)
#define text freopen("input.txt","r",stdin); freopen("output.txt", "w",stdout)
using namespace std;
typedef vector<int> vint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
#define y1 vamdlsfmvldf
const int mod = (int)1e9 + 7;
const ll INF = (int)(1e9);
const int N = 100005;
ll n, k, d1, d2;
ll etal;
ll a, b, c;
bool is_cor(ll dop){
    return (dop <= etal && dop >= 0);
}
int main(){
    int t;
    cin >> t;
    rep(qw, t){
        scanf("%I64d%I64d%I64d%I64d", &n, &k, &d1, &d2);
        if(n % 3 != 0){
            printf("yes\n");
            continue;
        }
        etal = n / 3;
        a = (k + 2 * d1 + d2) / 3;
        b = a - d1;
        c = a - d1 - d2;
        if(is_cor(a) && is_cor(b) && is_cor(c)){
            printf("yes\n");
            continue;
        }
        a = (2 * d1 - d2 + k) / 3;
        b = a - d1;
        c = b + d2;
        if(is_cor(a) && is_cor(b) && is_cor(c)){
                printf("yes\n");
                continue;
        }
        printf("no\n");
    }
    return 0;
}


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

long long n, k, d1, d2, res;

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        cin >> n >> k >> d1 >> d2;

        res = (n - k) - (2 * d1 + d2);
        if (res >= 0ll && res % 3ll == 0 && (2 * d2 + d1) <= k) {
            cout << "yes" << endl;
            continue;
        }

        res = (n - k) - (2 * d2 + d1);
        if (res >= 0ll && res % 3ll == 0 && (2 * d1 + d2) <= k) {
            cout << "yes" << endl;
            continue;
        }

        res = (n - k) - (d1 + d2);
        if (res >= 0ll && res % 3ll == 0 && (2 * d1 - d2) <= k && (2 * d2 - d1) <= k) {
            cout << "yes" << endl;
            continue;
        }

        if (d1 < d2)
            swap(d1, d2);
        res = (n - k) - (2 * d1 - d2);
        if (res >= 0ll && res % 3ll == 0 && (d1 + d2) <= k) {
            cout << "yes" << endl;
            continue;
        }

        else {
            cout << "no" << endl;
        }
    }
}


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

long long n, k, d1, d2, res;

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        cin >> n >> k >> d1 >> d2;

        res = (n - k) - (2 * d1 + d2);
        if (res >= 0ll && res % 3ll == 0) {
            cout << "yes" << endl;
            continue;
        }

        res = (n - k) - (2 * d2 + d1);
        if (res >= 0ll && res % 3ll == 0) {
            cout << "yes" << endl;
            continue;
        }

        res = (n - k) - (d1 + d2);
        if (res >= 0ll && res % 3ll == 0) {
            cout << "yes" << endl;
            continue;
        }

        if (d1 < d2)
            swap(d1, d2);
        res = (n - k) - (2 * d1 - d2);
        if (res >= 0ll && res % 3ll == 0) {
            cout << "yes" << endl;
            continue;
        }

        else {
            cout << "no" << endl;
        }
    }
}


#include <iostream>
using namespace std;

long long n, k, d1, d2;

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        cin >> n >> k >> d1 >> d2;
        long long res = (n - k) - (2 * d1 + d2);
        if (res >= 0ll && res % 3ll == 0) {
            cout << "yes" << endl;
        }
        else {
            cout << "no" << endl;
        }
    }
}


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

long long n, k, d1, d2, res;

bool constructable(long long a, long long b, long long c) {
    return (k >= (a + b + c) && (k - a - b - c) % 3 == 0);
}

bool fillable(long long a, long long b, long long c) {
    long long left = max(a, max(b, c)) * 3 - (a + b + c);
    return ((left <= n - k) && (n - k - left) % 3 == 0);
}

bool valid(long long a, long long b, long long c) {
    long long low = min(a, min(b, c));
    a -= low; b -= low; c -= low;

    return constructable(a, b, c) && fillable(a, b, c);
}

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        cin >> n >> k >> d1 >> d2;

        if (valid(-d1, 0, d2) || valid(-d1, 0, - d2) || valid(d1, 0, d2) || valid(d1, 0, - d2)) {
            cout << "yes" << endl;
        }
        else {
            cout << "no" << endl;
        }
    }
}


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

long long n, k, d1, d2, res;

bool constructable(long long a, long long b, long long c) {
    return (k >= (a + b + c) && (k - a - b - c) % 3 == 0);
}

bool fillable(long long a, long long b, long long c) {
    long long left = max(a, max(b, c)) * 3 - (a + b + c);
    return (left <= n - k) && (n - k - left) % 3 == 0);
}

bool valid(long long a, long long b, long long c) {
    long long low = min(a, min(b, c));
    a += low; b += low; c += low;

    return constructable(a, b, c) && fillable(a, b, c);
}

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        cin >> n >> k >> d1 >> d2;

        if (valid(-d1, 0, d2) || valid(-d1, 0, - d2) || valid(d1, 0, d2) || valid(d1, 0, - d2)) {
            cout << "yes" << endl;
        }
        else {
            cout << "no" << endl;
        }
    }
}


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

long long n, k, d1, d2, res;

bool constructable(long long a, long long b, long long c) {
    return (k >= (a + b + c) && (k - a - b - c) % 3 == 0);
}

bool fillable(long long a, long long b, long long c) {
    long long left = max(a, max(b, c)) * 3 - (a + b + c);
    return ((left <= n - k) && (n - k - left) % 3 == 0);
}

bool valid(long long a, long long b, long long c) {
    long long low = min(a, min(b, c));
    a += low; b += low; c += low;

    return constructable(a, b, c) && fillable(a, b, c);
}

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        cin >> n >> k >> d1 >> d2;

        if (valid(-d1, 0, d2) || valid(-d1, 0, - d2) || valid(d1, 0, d2) || valid(d1, 0, - d2)) {
            cout << "yes" << endl;
        }
        else {
            cout << "no" << endl;
        }
    }
}


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

long long n, k, d1, d2, res;

bool valid(long long a, long long b, long long c) {
    long long low = min(a, min(b, c));
    a += low; b += low; c += low;

    long long left = max(a, max(b, c)) * 3ll - (a + b + c);
    return (a + b + c <= k && (k - a - b - c) % 3 == 0 && left <= n - k && (n - k - left) % 3ll == 0);
}

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        cin >> n >> k >> d1 >> d2;

        if (valid(0, d1, d1 + d2) || valid(0, d1, d1 - d2) || valid(0, -d1, -d1 + d2) || valid(0, -d1, -d1 - d2)) {
            cout << "yes" << endl;
        }
        else {
            cout << "no" << endl;
        }
    }
}


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

long long n, k, d1, d2, res;

bool valid(long long a, long long b, long long c) {
    long long low = min(a, min(b, c));
    a += low; b += low; c += low;

    long long left = max(a, max(b, c)) * 3ll - (a + b + c);
    return (a + b + c <= k && left <= n - k && (n - k - left) % 3ll == 0);
}

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        cin >> n >> k >> d1 >> d2;

        if (valid(0, d1, d1 + d2) || valid(0, d1, d1 - d2) || valid(0, -d1, -d1 + d2) || valid(0, -d1, -d1 - d2)) {
            cout << "yes" << endl;
        }
        else {
            cout << "no" << endl;
        }
    }
}