//CLOCKS_PER_SEC
using namespace std;
#include<bits/stdc++.h>
#define sqr(x) (x)*(x)
//#define sort stable_sort
#define ll long long
#define mk make_pair
#define pb push_back
#define in insert
#define mtr(x,y,z) mk(mk(x,y),z)
#define fi first
#define se second
#define lch(x) ((x)<<1)
#define rch(x) (((x)<<1)|1)
#define all(x) (x).begin(),(x).end()
#define titose CLOCKS_PER_SEC
#define fpi(x) freopen(x,"r",stdin);
#define fpo(x) freopen(x,"w",stdout);
#define fprio fpi("in.txt");fpo("out.txt");
#define fast ios_base::sync_with_stdio(false);
inline void read(int &x){int v=0,f=1;char c=getchar();while (!isdigit(c)&&c!='-') c=getchar();if (c=='-') f=-1; else v=(c&15);while (isdigit(c=getchar())) v=(v<<1)+(v<<3)+(c&15);x=v*f;}
inline void read(ll &x){ll v=0ll,f=1ll;char c=getchar();while (!isdigit(c)&&c!='-') c=getchar();if (c=='-') f=-1; else v=(c&15);while (isdigit(c=getchar())) v=(v<<1)+(v<<3)+(c&15);x=v*f;}
inline void readc(char &x){char c;while (((c=getchar())==' ')||c=='\n');x=c;}
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define si set<int>
#define sl set<ll>
#define mii map<int,int>
#define mll map<ll,ll>
#define msi map<string,int>
#define msl map<string,ll>
#define piii pair<int,pii >
#define piipi pair<pii,int>
#define plll pair<ll,pll >
#define pllpl pair<pll,ll>
#define pqi priority_queue<int>
#define pql priority_queue<ll>
#define npqi priority_queue<int,vector<int>,greater<int> >
#define npql priority_queue<ll,vector<ll>,greater<ll> >
#define forup(i,a,b) for ((i)=(a);(i)<=(b);(i)++)
#define fordo(i,a,b) for ((i)=(a);(i)>=(b);(i)--)
#define rep(i,x) forup ((i),1,(x))
#define repd(i,x) fordo ((i),(x),1)
#define rep0(i,x) forup ((i),0,(x)-1)
#define rep0d(i,x) fordo ((i),((int)(x))-1,0)
#define itr iterator
#define fe(itcalc,c) for(__typeof((c).begin()) itcalc=(c).begin();itcalc!=(c).end();itcalc++)
#define NO {cout<<"NO";return 0;}
#define YES {cout<<"YES";return 0;}
#define y0 y000000000000000000000000000
#define y1 y111111111111111111111111111
#define j0 j000000000000000000000000000
#define j1 j111111111111111111111111111
#define cl0(a) memset((a),(0),(sizeof((a))))
#define clz(a) memset((a),(0x16),(sizeof((a))))
#define clf(a) memset((a),(-(0x16)),(sizeof((a))))
#define inf 0x3bbbbbbb
#define lnf 0x1bbbbbbbbbbbbbbbll
#define sqrt divi
#define p2(i) (1ll<<(i))
#define readi read
#define readll read
/*************************************************/
int a,b,c,d,f[4],i;
int main()
{
	read(a);read(b);read(c);read(d);
	if(abs(a+c-b-d)>1){
		puts("NO");
		return 0;
	}
	if(a&&!b&&c){
		puts("NO");
		return 0;
	}
	if(b&&!c&&d){
		puts("NO");
		return 0;
	}
	if(a&&(!b||!c)&&d){
		puts("NO");
		return 0;
	}
	if(max(a-b,0)+max(d-c,0)>1){
		puts("NO");
		return 0;
	}
	if(a-b==1){
		if(c||d){
			puts("NO");
			return 0;
		}
		puts("YES");
		putchar('0');
		rep(i,b) printf(" 1 0");
		puts("");return 0;
	}
	if(d-c==1){
		if(a||b){
			puts("NO");
			return 0;
		}
		puts("YES");
		putchar('3');
		rep(i,c) printf(" 2 3");
		puts("");return 0;
	}
	f[0]=a;f[1]=b;f[2]=c;f[3]=d;
	puts("YES");
	if(a+c>b+d){
		int t=0;if(!f[t]) t=2;
		for(;;){
			if(f[t]){
				printf("%d ",t);
				f[t]--;
			}
			if(t>0&&f[t-1]) t--; else t++;
			if(t>3) break;
		}
	}
	else{
		int t=3;if(!f[t]) t=1;
		for(;;){
			if(f[t]){
				printf("%d ",t);
				f[t]--;
			}
			if(t<3&&f[t+1]) t++; else t--;
			if(t<0) break;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int a,b,c,d,ans[200010],s=5;
bool solve(){
    int cnt=s,t=min(a,b);
    for(int i=1;i<=t;i++){
        ans[++cnt]=0;a--;
        ans[++cnt]=1;b--;
    }
    if(a&&(c||d))return false;
    else if(a==1){
        ans[++cnt]=0;
        return true;
    }
    else if(a)return false;
    if(!c||b==c){
        ans[s]=1;s--;b--;
    }
    t=min(b,c-1);
    if(b&&c-t<d&&ans[s+1]==0){
        ans[s]=1;s--;b--;
        t=min(b,c-1);
    }
    for(int i=1;i<=t;i++){
        ans[++cnt]=2;c--;
        ans[++cnt]=1;b--;
    }
    if(c){ans[++cnt]=2;c--;}
    else if(d)return false;
    if(b&&d)return false;
    else if(b&&(ans[cnt]==0||ans[cnt]==2)){
        ans[++cnt]=1;
        b--;
    }
    if(b&&(ans[s+1]==0||ans[s+1]==2)){
        ans[s]=1;s--;b--;
    }
    //cout<<c<<' '<<d<<endl;
    if(b)return false;
    t=min(c,d);
    for(int i=1;i<=t;i++){
        ans[++cnt]=3;d--;
        ans[++cnt]=2;c--;
    }
    if(d==1)ans[++cnt]=3;
    else if(d==2&&(ans[s+1]==2)){
        ans[++cnt]=3;
        ans[s]=3;s--;d--;
    }
    else if(d)return false;
    if(c==1)ans[++cnt]=2;
    else if(c&&(ans[s+1]==1||ans[s+1]==3)){
        ans[s]=2;s--;c--;
    }
    else if(c)return false;
    return true;
}
int main(){
    scanf("%d%d%d%d",&a,&b,&c,&d);
    int sum=a+b+c+d;
    if(!solve())printf("NO\n");
    else{
        printf("YES\n");
        for(int i=s+1;i<=s+sum;i++)printf("%d ",ans[i]);
    }
}
/*
2 2 2 1
0 0 1 2
0 0 2 1
0 1 2 0
0 2 1 0
1 2 0 0
1 3 0 0
3 1 0 0
1 1 1 1

*/


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int a,b,c,d,ans[200010],s=5;
bool solve(){
    int cnt=s,t=min(a,b);
    for(int i=1;i<=t;i++){
        ans[++cnt]=0;a--;
        ans[++cnt]=1;b--;
    }
    if(a&&(c||d))return false;
    else if(a==1){
        ans[++cnt]=0;
        return true;
    }
    else if(a)return false;
    if(!c||b==c){
        ans[s]=1;s--;b--;
    }
    t=min(b,c-1);
    for(int i=1;i<=t;i++){
        ans[++cnt]=2;c--;
        ans[++cnt]=1;b--;
    }
    if(c){ans[++cnt]=2;c--;}
    else if(d)return false;
    if(b&&d)return false;
    else if(b&&(ans[cnt]==0||ans[cnt]==2)){
        ans[++cnt]=1;
        b--;
    }
    if(b&&(ans[s+1]==0||ans[s+1]==2)){
        ans[s]=1;s--;b--;
    }
    if(b)return false;
    t=min(c,d);
    for(int i=1;i<=t;i++){
        ans[++cnt]=3;d--;
        ans[++cnt]=2;c--;
    }
    if(d==1)ans[++cnt]=3;
    else if(d==2&&(ans[s+1]==2)){
        ans[++cnt]=3;
        ans[s]=3;s--;d--;
    }
    else if(d)return false;
    if(c==1)ans[++cnt]=2;
    else if(c&&(ans[s+1]==1||ans[s+1]==3)){
        ans[s]=2;s--;c--;
    }
    else if(c)return false;
    return true;
}
int main(){
    scanf("%d%d%d%d",&a,&b,&c,&d);
    int sum=a+b+c+d;
    if(!solve())printf("NO\n");
    else{
        printf("YES\n");
        for(int i=s+1;i<=s+sum;i++)printf("%d ",ans[i]);
    }
}
/*
2 2 2 1
0 0 1 2
0 0 2 1
0 1 2 0
0 2 1 0
1 2 0 0
1 3 0 0
3 1 0 0
1 1 1 1
*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<stack>
#include<map>
#include<deque>
#include<set>
#define inf 1e9
#define eps 1e-6
#define N 100010
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline ll read()
{
	char ch=getchar();
	ll s=0,w=1;
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getchar();}
	return s*w;
}
int p[101];
int *a=p+20;
int answer[N],ans;
int main()
{
	a[1]=read(),a[2]=read(),a[3]=read(),a[4]=read();
	int pos=0;
	for(register int i=1;i<=4;i++)
	{
		if(a[i]>a[i-1]+a[i+1]+1){printf("NO\n");return 0;}
		if(a[i]==a[i-1]+a[i+1]+1)
		{
			if((a[i-3]||a[i-2]||a[i+2]||a[i+3])){printf("NO\n");return 0;}
			else {pos=i;}
		}
	}
	if(pos){a[pos]--;answer[++ans]=pos-1;}
	for(register int i=1;i<=3;i++)
	{
		while(a[i])
		{
			answer[++ans]=i-1;a[i]--;
			if(a[i+1]){a[i+1]--;answer[++ans]=i;}
		}
	}
//	cout<<"WTF:"<<a[4]<<endl;
	if(a[4]>1){printf("NO\n");}
	else
	{
		printf("YES\n");
		for(register int i=1;i<=ans;i++)printf("%d ",answer[i]);
		printf("\n");
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<stack>
#include<map>
#include<deque>
#include<set>
#define inf 1e9
#define eps 1e-6
#define N 100010
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline ll read()
{
	char ch=getchar();
	ll s=0,w=1;
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getchar();}
	return s*w;
}
int p[101],q[101];
int *a=p+20,*b=q+20;
int answer[N],ans;
int flag=0;
int main()
{
	a[1]=read(),a[2]=read(),a[3]=read(),a[4]=read();
	for(register int i=1;i<=4;i++)
	{
		if(a[i]>a[i-1]+a[i+1]+1){printf("NO\n");return 0;}
		if(a[i]==a[i-1]+a[i+1]+1)
		{
			if((a[i-3]||a[i-2]||a[i+2]||a[i+3])){printf("NO\n");return 0;}
		}
	}
	for(register int begin=1;begin<=4;begin++)
	{
		b[1]=a[1],b[2]=a[2],b[3]=a[3],b[4]=a[4];
		int now=begin,s=1;ans=0;
		while(true)
		{
			answer[++ans]=now-1;b[now]--;
			if(b[now+s])now+=s;
			else if(!b[now-s]){break;}
			else s=-s,now+=s;
		}
		int p=0;
		for(register int j=1;j<=4;j++)
		{
			if(b[j]){p=1;break;}
		}
		if(!p){flag=1;break;}
	}
	if(flag){printf("YES\n");for(register int i=1;i<=ans;i++)printf("%d ",answer[i]);printf("\n");return 0;}
	for(register int begin=1;begin<=4;begin++)
	{
		b[1]=a[1],b[2]=a[2],b[3]=a[3],b[4]=a[4];
		int now=begin,s=-1;ans=0;
		while(true)
		{
			answer[++ans]=now-1;b[now]--;
			if(b[now+s])now+=s;
			else if(!b[now-s]){break;}
			else s=-s,now+=s;
		}
		int p=0;
		for(register int j=1;j<=4;j++)
		{
			if(b[j]){p=1;break;}
		}
		if(!p){flag=1;break;}
	}
	if(flag){printf("YES\n");for(register int i=1;i<=ans;i++)printf("%d ",answer[i]);printf("\n");return 0;}
	printf("NO\n");
	return 0;
}
/*
1 2 3 2
*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<stack>
#include<map>
#include<deque>
#include<set>
#define inf 1e9
#define eps 1e-6
#define N 100010
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline ll read()
{
	char ch=getchar();
	ll s=0,w=1;
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getchar();}
	return s*w;
}
int p[101],q[101];
int *a=p+20,*b=q+20;
int ans[N],sum;
int main()
{
	a[1]=read(),a[2]=read(),a[3]=read(),a[4]=read();
	sum=a[1]+a[2]+a[3]+a[4];
	for(register int i=1;i<=4;i++)
	{
		for(register int j=1;j<=4;j++)b[j]=a[j];
		int now=i,flag=0;
		for(register int j=1;j<=sum;j++)
		{
			if(!b[now]){flag=1;break;}
			b[now]--;ans[j]=now;
			if(b[now-1])now--;
			else now++;
		}
		if(!flag)
		{
			printf("YES\n");
			for(register int i=1;i<=sum;i++)printf("%d ",ans[i]-1);
			printf("\n");return 0;
		}
	}
	printf("NO\n");
	return 0;
}
/*
*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<stack>
#include<map>
#include<deque>
#include<set>
#define inf 1e9
#define eps 1e-6
#define N 100010
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline ll read()
{
	char ch=getchar();
	ll s=0,w=1;
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getchar();}
	return s*w;
}
int p[101];
int *a=p+20;
int answer[N],ans;
int main()
{
	a[1]=read(),a[2]=read(),a[3]=read(),a[4]=read();
	int pos=0;
	for(register int i=1;i<=4;i++)
	{
		if(a[i]>a[i-1]+a[i+1]+1){printf("NO\n");return 0;}
		if(a[i]==a[i-1]+a[i+1]+1)
		{
			if((a[i-3]||a[i-2]||a[i+2]||a[i+3])){printf("NO\n");return 0;}
			else {pos=i;}
		}
	}
	if(pos){a[pos]--;answer[++ans]=pos-1;}
	for(register int i=1;i<=3;i++)
	{
		if(i!=1)a[i+1]--,answer[++ans]=i;
		while(a[i])
		{
			answer[++ans]=i-1;a[i]--;
			if(a[i+1]){a[i+1]--;answer[++ans]=i;}
	    }
	}
//	cout<<"WTF:"<<a[4]<<endl;
	if(a[4]>1){printf("NO\n");}
	else
	{
		printf("YES\n");
		for(register int i=1;i<=ans;i++)printf("%d ",answer[i]);
		printf("\n");
	}
	return 0;
}
/*
1 2 3 2
*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<stack>
#include<map>
#include<deque>
#include<set>
#define inf 1e9
#define eps 1e-6
#define N 100010
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline ll read()
{
	char ch=getchar();
	ll s=0,w=1;
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getchar();}
	return s*w;
}
int p[101],q[101];
int *a=p+20,*b=q+20;
int ans[N],sum;
int main()
{
	a[1]=read(),a[2]=read(),a[3]=read(),a[4]=read();
	sum=a[1]+a[2]+a[3]+a[4];
	for(register int i=1;i<=4;i++)
	{
		for(register int j=1;j<=4;j++)b[j]=a[j];
		int now=i,flag=0;
		for(register int j=1;j<=sum;j++)
		{
			if(!b[now]){flag=1;break;}
			b[now]--;ans[j]=now;
			if(b[now-1])now--;
			else now++;
		}
		if(!flag)
		{
			printf("YES\n");
			for(register int i=1;i<=sum;i++)printf("%d ",ans[i]-1);
			printf("\n");return 0;
		}
	}
	printf("NO\n");
	return 0;
}
/*
1 2 3 2
*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<stack>
#include<map>
#include<deque>
#include<set>
#define inf 1e9
#define eps 1e-6
#define N 100010
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline ll read()
{
	char ch=getchar();
	ll s=0,w=1;
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getchar();}
	return s*w;
}
int p[101],q[101];
int *a=p+20,*b=q+20;
int answer[N],ans;
int flag=0;
int main()
{
	a[1]=read(),a[2]=read(),a[3]=read(),a[4]=read();
	for(register int begin=1;begin<=4;begin++)
	{
		b[1]=a[1],b[2]=a[2],b[3]=a[3],b[4]=a[4];
		int now=begin,s=1;ans=0;
		while(true)
		{
			answer[++ans]=now-1;b[now]--;
			if(b[now+s])now+=s;
			else if(!b[now-s]){break;}
			else s=-s,now+=s;
		}
		int p=0;
		for(register int j=1;j<=4;j++)
		{
			if(b[j]){p=1;break;}
		}
		if(!p){flag=1;break;}
	}
	if(flag){printf("YES\n");for(register int i=1;i<=ans;i++)printf("%d ",answer[i]);printf("\n");return 0;}
	for(register int begin=1;begin<=4;begin++)
	{
		b[1]=a[1],b[2]=a[2],b[3]=a[3],b[4]=a[4];
		int now=begin,s=-1;ans=0;
		while(true)
		{
			answer[++ans]=now-1;b[now]--;
			if(b[now+s])now+=s;
			else if(!b[now-s]){break;}
			else s=-s,now+=s;
		}
		int p=0;
		for(register int j=1;j<=4;j++)
		{
			if(b[j]){p=1;break;}
		}
		if(!p){flag=1;break;}
	}
	if(flag){printf("YES\n");for(register int i=1;i<=ans;i++)printf("%d ",answer[i]);printf("\n");return 0;}
	printf("NO\n");
	return 0;
}
/*
1 2 3 2
*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<stack>
#include<map>
#include<deque>
#include<set>
#define inf 1e9
#define eps 1e-6
#define N 100010
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline ll read()
{
	char ch=getchar();
	ll s=0,w=1;
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getchar();}
	return s*w;
}
int p[101];
int *a=p+20;
int answer[N],ans;
int main()
{
	a[1]=read(),a[2]=read(),a[3]=read(),a[4]=read();
	int pos=0;
	for(register int i=1;i<=4;i++)
	{
		if(a[i]>a[i-1]+a[i+1]+1){printf("NO\n");return 0;}
		if(a[i]==a[i-1]+a[i+1]+1)
		{
			if((a[i-3]||a[i-2]||a[i+2]||a[i+3])){printf("NO\n");return 0;}
			else {pos=i;}
		}
	}
	if(pos){a[pos]--;answer[++ans]=pos-1;}
	for(register int i=1;i<=3;i++)
	{
		a[i+1]--;answer[++ans]=i;
		while(a[i])
		{
			answer[++ans]=i-1;a[i]--;
			if(a[i+1]){a[i+1]--;answer[++ans]=i;}
	    }
	}
//	cout<<"WTF:"<<a[4]<<endl;
	if(a[4]>1){printf("NO\n");}
	else
	{
		printf("YES\n");
		for(register int i=1;i<=ans;i++)printf("%d ",answer[i]);
		printf("\n");
	}
	return 0;
}
/*
1 2 3 2
*/


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <memory.h>
#include <list>
#include <numeric>
#include <functional>
#include <complex>
#include <cassert>
#include <regex>
#include <random>
#include <fstream>

#define ll long long
#define ld long double
#define ull unsigned ll
#define mp(xx, yy) make_pair(xx, yy)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pii pair<int,int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define vvi vector<vector<int>>
#define vvll vector<vll>
#define forn(it,from,to) for(int (it)=from; (it)<to; (it)++)
//#define endl '\n'
ld pi = acos(-1.0);
const int mod = 1e9 + 7;
using namespace std;

void solve1(vi cnt) {
	int n = 0;
	forn(i, 0, 4) {
		n += cnt[i];
	}
	deque<int> ans;
	while (cnt[0] && cnt[1]) {
		ans.push_back(0);
		ans.push_back(1);
		cnt[0]--;
		cnt[1]--;
	}
	while (cnt[2] > cnt[3] && cnt[1] > 0) {
		ans.push_back(2);
		ans.push_back(1);
		cnt[2]--;
		cnt[1]--;
	}
	while (cnt[3] > 0 && cnt[2] > 0) {
		ans.push_back(2);
		ans.push_back(3);
		cnt[2]--;
		cnt[3]--;
	}
	if (cnt[1]) {
		ans.push_front(1);
		cnt[1]--;
	}
	if (cnt[2]) {
		ans.push_back(2);
		cnt[2]--;
	}
	if (ans.size() == n) {
		cout << "YES\n";
		forn(i, 0, n) {
			cout << ans[i] << ' ';
		}
		exit(0);
	}
}

void solve2(vi cnt) {
	vi ans;
	int mnPos = -1, mxPos = 4;
	forn(i, 0, 4) {
		if (cnt[i] > 0 && mnPos == -1) mnPos = i;
		if (cnt[i] > 0) mxPos = i;
	}
	int n = cnt[0] + cnt[1] + cnt[2] + cnt[3];
	forn(st, mnPos, mxPos + 1) {
		vi ans;
		vi _cnt = cnt;
		ans.push_back(st);
		_cnt[st]--;
		int fl = 1;
		while (1) {
			if (ans.back() == 3) {
				if (_cnt[2] == 0) {
					break;
				}
				else {
					ans.push_back(2);
					_cnt[2]--;
				}
			}
			else {
				if (ans.back() == 0) {
					if (_cnt[1] == 0) {
						break;
					}
					else {
						ans.push_back(1);
						_cnt[1]--;
					}
				}
				else {
					if (ans.back() == 1) {
						if (_cnt[0] == 0 && _cnt[2] == 0) break;
						if (_cnt[0] >= _cnt[2]) {
							ans.push_back(0);
							_cnt[0]--;
						}
						else {
							ans.push_back(2);
							_cnt[2]--;
						}
					}
					else {
						if (_cnt[1] == 0 && _cnt[3] == 0) break;
						if (_cnt[1] > _cnt[3]) {
							ans.push_back(1);
							_cnt[1]--;
						}
						else {
							ans.push_back(3);
							_cnt[3]--;
						}
					}
				}
			}
		}
		if (ans.size() == n) {
			cout << "YES\n";
			forn(i, 0, ans.size()) {
				cout << ans[i] << ' ';
			}
			exit(0);
		}
	}
}

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#else
	//freopen("garbling.in", "r", stdin);
	//freopen("garbling.out", "w", stdout);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	vi cnt(4);
	forn(i, 0, 4) cin >> cnt[i];
	solve1(cnt);
	solve2(cnt);
	cout << "NO";
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <memory.h>
#include <list>
#include <numeric>
#include <functional>
#include <complex>
#include <cassert>
#include <regex>
#include <random>
#include <fstream>

#define ll long long
#define ld long double
#define ull unsigned ll
#define mp(xx, yy) make_pair(xx, yy)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pii pair<int,int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define vvi vector<vector<int>>
#define vvll vector<vll>
#define forn(it,from,to) for(int (it)=from; (it)<to; (it)++)
//#define endl '\n'
ld pi = acos(-1.0);
const int mod = 1e9 + 7;
using namespace std;

ll gcd(ll a, ll b) {
	return b ? gcd(b, a % b) : a;
}

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#else
	//freopen("garbling.in", "r", stdin);
	//freopen("garbling.out", "w", stdout);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	vi cnt(4);
	int n = 0;
	forn(i, 0, 4) {
		cin >> cnt[i];
		n += cnt[i];
	}
	deque<int> ans;
	while (cnt[0] && cnt[1]) {
		ans.push_back(0);
		ans.push_back(1);
		cnt[0]--;
		cnt[1]--;
	}
	while (cnt[2] > cnt[3] && cnt[1] > 0) {
		ans.push_back(2);
		ans.push_back(1);
		cnt[2]--;
		cnt[1]--;
	}
	while (cnt[3] > 0 && cnt[2] > 0) {
		ans.push_back(2);
		ans.push_back(3);
		cnt[2]--;
		cnt[3]--;
	}
	if (cnt[1]) {
		ans.push_front(1);
		cnt[1]--;
	}
	if (cnt[2]) {
		ans.push_back(2);
		cnt[2]--;
	}
	if (ans.size() == n) {
		cout << "YES\n";
		forn(i, 0, n) {
			cout << ans[i] << ' ';
		}
	}
	else {
		cout << "NO";
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <memory.h>
#include <list>
#include <numeric>
#include <functional>
#include <complex>
#include <cassert>
#include <regex>
#include <random>
#include <fstream>

#define ll long long
#define ld long double
#define ull unsigned ll
#define mp(xx, yy) make_pair(xx, yy)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pii pair<int,int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define vvi vector<vector<int>>
#define vvll vector<vll>
#define forn(it,from,to) for(int (it)=from; (it)<to; (it)++)
//#define endl '\n'
ld pi = acos(-1.0);
const int mod = 1e9 + 7;
using namespace std;

ll gcd(ll a, ll b) {
	return b ? gcd(b, a % b) : a;
}

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#else
	//freopen("garbling.in", "r", stdin);
	//freopen("garbling.out", "w", stdout);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	vi cnt(4);
	forn(i, 0, 4) cin >> cnt[i];
	vi ans;
	int mnPos = -1, mxPos = 4;
	forn(i, 0, 4) {
		if (cnt[i] > 0 && mnPos == -1) mnPos = i;
		if (cnt[i] > 0) mxPos = i;
	}
	//forn(i, mnPos, mxPos + 1) {
	//	if (cnt[i] == 0) {
	//		cout << "NO";
	//		return 0;
	//	}
	//}
	int n = cnt[0] + cnt[1] + cnt[2] + cnt[3];
	forn(st, mnPos, mxPos + 1) {
		vi ans;
		vi _cnt = cnt;
		ans.push_back(st);
		_cnt[st]--;
		int fl = 1;
		while (1) {
			if (ans.back() == 3) {
				if (_cnt[2] == 0) {
					break;
				}
				else {
					ans.push_back(2);
					_cnt[2]--;
				}
			}
			else {
				if (ans.back() == 0) {
					if (_cnt[1] == 0) {
						break;
					}
					else {
						ans.push_back(1);
						_cnt[1]--;
					}
				}
				else {
					if (ans.back() == 1) {
						if (_cnt[0] == 0 && _cnt[2] == 0) break;
						if (_cnt[0] >= _cnt[2]) {
							ans.push_back(0);
							_cnt[0]--;
						}
						else {
							ans.push_back(2);
							_cnt[2]--;
						}
					}
					else {
						if (_cnt[1] == 0 && _cnt[3] == 0) break;
						if (_cnt[1] > _cnt[3]) {
							ans.push_back(1);
							_cnt[1]--;
						}
						else {
							ans.push_back(3);
							_cnt[3]--;
						}
					}
				}
			}
		}
		if (ans.size() == n) {
			cout << "YES\n";
			forn(i, 0, ans.size()) {
				cout << ans[i] << ' ';
			}
			return 0;
		}
	}
	cout << "NO";
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <memory.h>
#include <list>
#include <numeric>
#include <functional>
#include <complex>
#include <cassert>
#include <regex>
#include <random>
#include <fstream>

#define ll long long
#define ld long double
#define ull unsigned ll
#define mp(xx, yy) make_pair(xx, yy)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pii pair<int,int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define vvi vector<vector<int>>
#define vvll vector<vll>
#define forn(it,from,to) for(int (it)=from; (it)<to; (it)++)
//#define endl '\n'
ld pi = acos(-1.0);
const int mod = 1e9 + 7;
using namespace std;

ll gcd(ll a, ll b) {
	return b ? gcd(b, a % b) : a;
}

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#else
	//freopen("garbling.in", "r", stdin);
	//freopen("garbling.out", "w", stdout);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	vi cnt(4);
	forn(i, 0, 4) cin >> cnt[i];
	vi ans;
	int mnPos = -1, mxPos = 4;
	forn(i, 0, 4) {
		if (cnt[i] > 0 && mnPos == -1) mnPos = i;
		if (cnt[i] > 0) mxPos = i;
	}
	//forn(i, mnPos, mxPos + 1) {
	//	if (cnt[i] == 0) {
	//		cout << "NO";
	//		return 0;
	//	}
	//}
	int n = cnt[0] + cnt[1] + cnt[2] + cnt[3];
	forn(st, mnPos, mxPos + 1) {
		vi ans;
		vi _cnt = cnt;
		ans.push_back(st);
		_cnt[st]--;
		int fl = 1;
		while (1) {
			if (ans.back() == 3) {
				if (_cnt[2] == 0) {
					break;
				}
				else {
					ans.push_back(2);
					_cnt[2]--;
				}
			}
			else {
				if (ans.back() == 0) {
					if (_cnt[1] == 0) {
						break;
					}
					else {
						ans.push_back(1);
						_cnt[1]--;
					}
				}
				else {
					if (ans.back() == 1) {
						if (_cnt[0] == 0 && _cnt[2] == 0) break;
						if (_cnt[0] > _cnt[2]) {
							ans.push_back(0);
							_cnt[0]--;
						}
						else {
							ans.push_back(2);
							_cnt[2]--;
						}
					}
					else {
						if (_cnt[1] == 0 && _cnt[3] == 0) break;
						if (_cnt[1] > _cnt[3]) {
							ans.push_back(1);
							_cnt[1]--;
						}
						else {
							ans.push_back(3);
							_cnt[3]--;
						}
					}
				}
			}
		}
		if (ans.size() == n) {
			cout << "YES\n";
			forn(i, 0, ans.size()) {
				cout << ans[i] << ' ';
			}
			return 0;
		}
	}
	cout << "NO";
	return 0;
}


#include <bits/stdc++.h>

#define rep(i, a, b) for(int i = (a); i <= (b); ++ i)
#define per(i, a, b) for(int i = (a); i >= (b); -- i)
#define VI vector<int>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
const int P = 998244353;
const int N = 200010;

int A[N], n;
void dfs(VI v, int p) {
    if(p == n) {
        cout << "YES\n";
        rep(i, 1, n) {
            cout << A[i] << " ";
        }cout << "\n";
        exit(0);
    }
    if( A[p] - 1 >= 0 && v[A[p]-1] > 0) {
        -- v[A[p]-1];
        A[p+1] = A[p] - 1;
        dfs(v, p+1);
        ++ v[A[p]-1];
    }
    if( A[p] + 1 <= 3 && v[A[p]+1] > 0) {
        -- v[A[p]+1];
        A[p+1] = A[p] + 1;
        dfs(v, p+1);
        ++ v[A[p]+1];
    }
}

int main(){
    ios::sync_with_stdio(false); cin.tie(0);
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    n = a + b + c + d;
    A[1] = 0;
    dfs({a-1, b, c, d}, 1);
    A[1] = 1;
    dfs({a, b-1, c, d}, 1);
    cout << "NO\n";
    return 0;
}


#include<bits/stdc++.h>
#define mxn 1000010
#define db double
#define LL long long
#define ldb long double
#define pb push_back
#define ppb pop_back
#define pf push_front
#define pii pair<int,int>
#define mp make_pair
#define fr first
#define sc second
#define x1 faqx1
#define y1 faqy1
#define x2 faqx2
#define y2 faqy2
using namespace std;
const int inf=1e9,mod=1e9+7;
int n,A,B,C,D,sl,fh,ans[mxn];
int rd()
{
	sl=0;fh=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();}
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar();
	return sl*fh;
}
int main()
{
	A=rd();B=rd();C=rd();D=rd();n=A+B+C+D;
	if(abs(A+C-B-D)>1) return puts("NO"),0;
	int x;
	if(A+C>=B+D)
	{
		x=0;
		if(!A) x=2;
		for(int i=1;i<=n;i+=2)
		{
			ans[i]=x;
			A--;
			if(!A) x=2;
		}
		x=1;
		if(!B) x=3;
		for(int i=2;i<=n;i+=2)
		{
			ans[i]=x;
			B--;
			if(!B) x=3;
		}
	}
	else
	{
		x=0;
		if(!A) x=2;
		for(int i=2;i<=n;i+=2)
		{
			ans[i]=x;
			A--;
			if(!A) x=2;
		}
		x=1;
		if(!B) x=3;
		for(int i=1;i<=n;i+=2)
		{
			ans[i]=x;
			B--;
			if(!B) x=3;
		}
	}
	for(int i=1;i<n;++i)
		if(abs(ans[i]-ans[i+1])>1)
			return puts("NO"),0;
	puts("YES");
	for(int i=1;i<=n;++i) printf("%d ",ans[i]);puts("");
	return 0;
}


#include<bits/stdc++.h>
#define mxn 1000010
#define db double
#define LL long long
#define ldb long double
#define pb push_back
#define ppb pop_back
#define pf push_front
#define pii pair<int,int>
#define mp make_pair
#define fr first
#define sc second
#define x1 faqx1
#define y1 faqy1
#define x2 faqx2
#define y2 faqy2
using namespace std;
const int inf=1e9,mod=1e9+7;
int A,B,C,D,s1,s2,sl,fh,top,s[mxn],ans[mxn];
int rd()
{
	sl=0;fh=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();}
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar();
	return sl*fh;
}
void dfs(int ps,int lst,int a,int b,int c,int d)
{
	if(a<0||b<0||c<0||d<0) return ;
	s[ps]=lst;
	if(!a&&!b&&!c&&!d)
	{
		puts("YES");
		s[A+B+C+D+1]=-1;
		for(int i=1;i<=A+B+C+D;++i)
		{
			ans[++top]=s[i];
			if(s[i]==1&&s[i+1]==0&&s1)
				for(;s1;--s1)
					ans[++top]=0,ans[++top]=1;
			if(s[i]==0&&s[i+1]==1&&s1)
				for(;s1;--s1)
					ans[++top]=1,ans[++top]=0;
			if(s[i]==2&&s[i+1]==3&&s2)
				for(;s2;--s2)
					ans[++top]=3,ans[++top]=2;
			if(s[i]==3&&s[i+1]==2&&s2)
				for(;s2;--s2)
					ans[++top]=2,ans[++top]=3;
		}
		for(int i=1;i<=top;++i) printf("%d ",ans[i]);puts("");
		exit(0);
	}
	if(lst==-1)
	{
		dfs(ps+1,0,a-1,b,c,d);
		dfs(ps+1,1,a,b-1,c,d);
		dfs(ps+1,2,a,b,c-1,d);
		dfs(ps+1,3,a,b,c,d-1);
	}
	else if(lst==0) dfs(ps+1,1,a,b-1,c,d);
	else if(lst==1) dfs(ps+1,0,a-1,b,c,d),dfs(ps+1,2,a,b,c-1,d);
	else if(lst==2) dfs(ps+1,3,a,b,c,d-1),dfs(ps+1,1,a,b-1,c,d);
	else dfs(ps+1,2,a,b,c-1,d);
}
int main()
{
	A=rd();B=rd();C=rd();D=rd();
	if(A>2) {s1=A-2;B-=A-2;A=2;}
	if(D>2) {s2=D-2;C-=D-2;D=2;}
	dfs(0,-1,A,B,C,D);
	puts("NO");
	return 0;
}


#include<bits/stdc++.h>
#define mxn 1000010
#define db double
#define LL long long
#define ldb long double
#define pb push_back
#define ppb pop_back
#define pf push_front
#define pii pair<int,int>
#define mp make_pair
#define fr first
#define sc second
#define x1 faqx1
#define y1 faqy1
#define x2 faqx2
#define y2 faqy2
using namespace std;
const int inf=1e9,mod=1e9+7;
int A,B,C,D,s1,s2,sl,fh,top,s[mxn],ans[mxn];
int rd()
{
	sl=0;fh=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();}
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar();
	return sl*fh;
}
void dfs(int ps,int lst,int a,int b,int c,int d)
{
	if(a<0||b<0||c<0||d<0) return ;
	s[ps]=lst;
	if(!a&&!b&&!c&&!d)
	{
		puts("YES");
		s[A+B+C+D+1]=-1;
		for(int i=1;i<=A+B+C+D;++i)
		{
			ans[++top]=s[i];
			if(s[i]==1&&s[i+1]==0&&s1)
				for(;s1;--s1)
					ans[++top]=0,ans[++top]=1;
			if(s[i]==0&&s[i+1]==1&&s1)
				for(;s1;--s1)
					ans[++top]=1,ans[++top]=0;
			if(s[i]==2&&s[i+1]==3&&s2)
				for(;s2;--s2)
					ans[++top]=3,ans[++top]=2;
			if(s[i]==3&&s[i+1]==2&&s2)
				for(;s2;--s2)
					ans[++top]=2,ans[++top]=3;
		}
		for(int i=1;i<=top;++i) printf("%d ",ans[i]);puts("");
		exit(0);
	}
	if(lst==-1)
	{
		dfs(ps+1,0,a-1,b,c,d);
		dfs(ps+1,1,a,b-1,c,d);
		dfs(ps+1,2,a,b,c-1,d);
		dfs(ps+1,3,a,b,c,d-1);
	}
	else if(lst==0) dfs(ps+1,1,a,b-1,c,d);
	else if(lst==1) dfs(ps+1,0,a-1,b,c,d),dfs(ps+1,2,a,b,c-1,d);
	else if(lst==2) dfs(ps+1,1,a,b-1,c,d),dfs(ps+1,3,a,b,c,d-1);
	else dfs(ps+1,2,a,b,c-1,d);
}
int main()
{
	A=rd();B=rd();C=rd();D=rd();
	if(A) {s1=A-1;B-=A-1;A=1;}
	if(D) {s2=D-1;C-=D-1;D=1;}
	dfs(0,-1,A,B,C,D);
	puts("NO");
	return 0;
}


#include<bits/stdc++.h>
#define mxn 1000010
#define db double
#define LL long long
#define ldb long double
#define pb push_back
#define ppb pop_back
#define pf push_front
#define pii pair<int,int>
#define mp make_pair
#define fr first
#define sc second
#define x1 faqx1
#define y1 faqy1
#define x2 faqx2
#define y2 faqy2
using namespace std;
const int inf=1e9,mod=1e9+7;
int A,B,C,D,s1,s2,sl,fh,top,s[mxn],ans[mxn];
int rd()
{
	sl=0;fh=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();}
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar();
	return sl*fh;
}
void dfs(int ps,int lst,int a,int b,int c,int d)
{
	if(a<0||b<0||c<0||d<0) return ;
	s[ps]=lst;
	if(!a&&!b&&!c&&!d)
	{
		puts("YES");
		s[A+B+C+D+1]=-1;
		for(int i=1;i<=A+B+C+D;++i)
		{
			ans[++top]=s[i];
			if(s[i]==1&&s[i+1]==0&&s1)
				for(;s1;--s1)
					ans[++top]=0,ans[++top]=1;
			if(s[i]==0&&s[i+1]==1&&s1)
				for(;s1;--s1)
					ans[++top]=1,ans[++top]=0;
			if(s[i]==2&&s[i+1]==3&&s2)
				for(;s2;--s2)
					ans[++top]=3,ans[++top]=2;
			if(s[i]==3&&s[i+1]==2&&s2)
				for(;s2;--s2)
					ans[++top]=2,ans[++top]=3;
		}
		for(int i=1;i<=top;++i) printf("%d ",ans[i]);puts("");
		exit(0);
	}
	if(lst==-1)
	{
		dfs(ps+1,0,a-1,b,c,d);
		dfs(ps+1,1,a,b-1,c,d);
		dfs(ps+1,2,a,b,c-1,d);
		dfs(ps+1,3,a,b,c,d-1);
	}
	else if(lst==0) dfs(ps+1,1,a,b-1,c,d);
	else if(lst==1) dfs(ps+1,0,a-1,b,c,d),dfs(ps+1,2,a,b,c-1,d);
	else if(lst==2) dfs(ps+1,1,a,b-1,c,d),dfs(ps+1,3,a,b,c,d-1);
	else dfs(ps+1,2,a,b,c-1,d);
}
int main()
{
	A=rd();B=rd();C=rd();D=rd();
	if(A&&B) {s1=min(A-1,B-1);B-=s1;A-=s1;}
	if(D&&C) {s2=min(D-1,C-1);C-=s2;D-=s2;}
	dfs(0,-1,A,B,C,D);
	puts("NO");
	return 0;
}


#include<bits/stdc++.h>
#define mxn 1000010
#define db double
#define LL long long
#define ldb long double
#define pb push_back
#define ppb pop_back
#define pf push_front
#define pii pair<int,int>
#define mp make_pair
#define fr first
#define sc second
#define x1 faqx1
#define y1 faqy1
#define x2 faqx2
#define y2 faqy2
using namespace std;
const int inf=1e9,mod=1e9+7;
int A,B,C,D,s1,s2,sl,fh,top,s[mxn],ans[mxn];
int rd()
{
	sl=0;fh=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();}
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar();
	return sl*fh;
}
void dfs(int ps,int lst,int a,int b,int c,int d)
{
	if(a<0||b<0||c<0||d<0) return ;
	s[ps]=lst;
	if(!a&&!b&&!c&&!d)
	{
		puts("YES");
		s[A+B+C+D+1]=-1;
		for(int i=1;i<=A+B+C+D;++i)
		{
			ans[++top]=s[i];
			if(s[i]==1&&s[i+1]==0&&s1)
				for(;s1;--s1)
					ans[++top]=0,ans[++top]=1;
			if(s[i]==0&&s[i+1]==1&&s1)
				for(;s1;--s1)
					ans[++top]=1,ans[++top]=0;
			if(s[i]==2&&s[i+1]==3&&s2)
				for(;s2;--s2)
					ans[++top]=3,ans[++top]=2;
			if(s[i]==3&&s[i+1]==2&&s2)
				for(;s2;--s2)
					ans[++top]=2,ans[++top]=3;
		}
		for(int i=1;i<=top;++i) printf("%d ",ans[i]);puts("");
		exit(0);
	}
	if(lst==-1)
	{
		dfs(ps+1,0,a-1,b,c,d);
		dfs(ps+1,1,a,b-1,c,d);
		dfs(ps+1,2,a,b,c-1,d);
		dfs(ps+1,3,a,b,c,d-1);
	}
	else if(lst==0) dfs(ps+1,1,a,b-1,c,d);
	else if(lst==1) dfs(ps+1,0,a-1,b,c,d),dfs(ps+1,2,a,b,c-1,d);
	else if(lst==2) dfs(ps+1,1,a,b-1,c,d),dfs(ps+1,3,a,b,c,d-1);
	else dfs(ps+1,2,a,b,c-1,d);
}
int main()
{
	A=rd();B=rd();C=rd();D=rd();
	if(A) {s1=min(A-1,B-1);B-=s1;A-=s1;}
	if(D) {s2=min(D-1,C-1);C-=s2;D-=s2;}
	dfs(0,-1,A,B,C,D);
	puts("NO");
	return 0;
}


#include<bits/stdc++.h>
#define mxn 1000010
#define db double
#define LL long long
#define ldb long double
#define pb push_back
#define ppb pop_back
#define pf push_front
#define pii pair<int,int>
#define mp make_pair
#define fr first
#define sc second
#define x1 faqx1
#define y1 faqy1
#define x2 faqx2
#define y2 faqy2
using namespace std;
const int inf=1e9,mod=1e9+7;
int A,B,C,D,s1,s2,sl,fh,top,s[mxn],ans[mxn];
int rd()
{
	sl=0;fh=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();}
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar();
	return sl*fh;
}
void dfs(int ps,int lst,int a,int b,int c,int d)
{
	if(a<0||b<0||c<0||d<0) return ;
	s[ps]=lst;
	if(!a&&!b&&!c&&!d)
	{
		puts("YES");
		s[A+B+C+D+1]=-1;
		for(int i=1;i<=A+B+C+D;++i)
		{
			ans[++top]=s[i];
			if(s[i]==1&&s[i+1]==0&&s1)
				for(;s1;--s1)
					ans[++top]=0,ans[++top]=1;
			if(s[i]==0&&s[i+1]==1&&s1)
				for(;s1;--s1)
					ans[++top]=1,ans[++top]=0;
			if(s[i]==2&&s[i+1]==3&&s2)
				for(;s2;--s2)
					ans[++top]=3,ans[++top]=2;
			if(s[i]==3&&s[i+1]==2&&s2)
				for(;s2;--s2)
					ans[++top]=2,ans[++top]=3;
		}
		for(int i=1;i<=top;++i) printf("%d ",ans[i]);puts("");
		exit(0);
	}
	if(lst==-1)
	{
		dfs(ps+1,0,a-1,b,c,d);
		dfs(ps+1,1,a,b-1,c,d);
		dfs(ps+1,2,a,b,c-1,d);
		dfs(ps+1,3,a,b,c,d-1);
	}
	else if(lst==0) dfs(ps+1,1,a,b-1,c,d);
	else if(lst==1) dfs(ps+1,0,a-1,b,c,d),dfs(ps+1,2,a,b,c-1,d);
	else if(lst==2) dfs(ps+1,1,a,b-1,c,d),dfs(ps+1,3,a,b,c,d-1);
	else dfs(ps+1,2,a,b,c-1,d);
}
int main()
{
	A=rd();B=rd();C=rd();D=rd();
	if(A>3) {s1=A-3;B-=A-3;A=3;}
	if(D>3) {s2=D-3;C-=D-3;D=3;}
	dfs(0,-1,A,B,C,D);
	puts("NO");
	return 0;
}


#include<bits/stdc++.h>
#define mxn 1000010
#define db double
#define LL long long
#define ldb long double
#define pb push_back
#define ppb pop_back
#define pf push_front
#define pii pair<int,int>
#define mp make_pair
#define fr first
#define sc second
#define x1 faqx1
#define y1 faqy1
#define x2 faqx2
#define y2 faqy2
using namespace std;
const int inf=1e9,mod=1e9+7;
int A,B,C,D,s1,s2,sl,fh,top,s[mxn],ans[mxn];
int rd()
{
	sl=0;fh=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();}
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar();
	return sl*fh;
}
void dfs(int ps,int lst,int a,int b,int c,int d)
{
	if(a<0||b<0||c<0||d<0) return ;
	s[ps]=lst;
	if(!a&&!b&&!c&&!d)
	{
		puts("YES");
		s[A+B+C+D+1]=-1;
		for(int i=1;i<=A+B+C+D;++i)
		{
			ans[++top]=s[i];
			if(s[i]==1&&s[i+1]==0&&s1)
				for(;s1;--s1)
					ans[++top]=0,ans[++top]=1;
			if(s[i]==0&&s[i+1]==1&&s1)
				for(;s1;--s1)
					ans[++top]=1,ans[++top]=0;
			if(s[i]==2&&s[i+1]==3&&s2)
				for(;s2;--s2)
					ans[++top]=3,ans[++top]=2;
			if(s[i]==3&&s[i+1]==2&&s2)
				for(;s2;--s2)
					ans[++top]=2,ans[++top]=3;
		}
		for(int i=1;i<=top;++i) printf("%d ",ans[i]);puts("");
		exit(0);
	}
	if(lst==-1)
	{
		dfs(ps+1,0,a-1,b,c,d);
		dfs(ps+1,1,a,b-1,c,d);
		dfs(ps+1,2,a,b,c-1,d);
		dfs(ps+1,3,a,b,c,d-1);
	}
	else if(lst==0) dfs(ps+1,1,a,b-1,c,d);
	else if(lst==1) dfs(ps+1,0,a-1,b,c,d),dfs(ps+1,2,a,b,c-1,d);
	else if(lst==2) dfs(ps+1,1,a,b-1,c,d),dfs(ps+1,3,a,b,c,d-1);
	else dfs(ps+1,2,a,b,c-1,d);
}
int main()
{
	A=rd();B=rd();C=rd();D=rd();
	if(A&&B>A-1) {s1=A-1;B-=A-1;A=1;}
	if(D&&C>D-1) {s2=D-1;C-=D-1;D=1;}
	dfs(0,-1,A,B,C,D);
	puts("NO");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i{a}; i < b; i++)
#define FORD(i, b, a) for (int i{b}; i >= a; i--)
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) (a > b ? a : b)
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PII;
typedef vector<LL> VLL;
typedef vector<PII> VPP;
//#define pi 3.14159265358979323846
#define ub upper_bound
#define lb lower_bound
#define pb push_back
#define F first
#define S second
#define abs(a) ((a) > 0 ? (a) : -(a))
#define MOD m
#define LL long long
#define ULL unsigned LL
#define Md(a) ((a) >= 0 ? (a) % MOD : (MOD - (-(a))) % MOD)
#define INF 150005
#define MAXM 100008
#define C1 55
#define div(a, b) ((a) >= 0 ? (a) / (b) : -((-a) / b))

void solve(int tim)
{
    int d, m;
    scanf("%d%d", &d, &m);
    int hd = -1;
    int td = d;
    while(td){
        hd++;
        td >>= 1;
    }
    if(hd == 0){
        printf("1\n");
        return;
    }
    VI dp(32, 1);
    FOR(i, 0, 31){
        if(i == hd){
            dp[i] = d - (1 << i) + 1;
            //printf("DEBUG: %d,%d,%d,%d ", dp[i], i, d, 1 << i);
            break;
        }
        else
        {
            dp[i] = (1 << i + 1) - (1 << i) ;
        }
        //printf("DEBUG: %d,%d ", dp[i], i);
    }
    int ans = 1;
    FOR(i, 0, hd + 1)
        ans = (LL) ans * (dp[i] + 1) % m;
    printf("%d\n", Md(ans - 1));
}

int main()
{
   int test = 1;
   scanf("%d", &test);
   FOR(t, 1, test + 1)
   {
     // printf("Case #%d: ", t);
      solve(t);
   }
}


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

int t;
int d, m;
ll ans;

int main()
{
	cin >> t;
	while (t--)
	{
		cin >> d >> m;
		ans = 1;
		for (int i = 0; i < 30; i++)
		{
			if (d < (1 << i))
			{
				break;
			}
			ans = ans * (min(d, (1 << (i + 1)) - 1) - (1 << i) + 2) % m;
		}
		ans--;
		if (ans < 0)
		{
			ans += 1ll * m;
		}
		cout << ans << endl;
	}
	return 0;
}


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

int t;
int d, m;
ll ans;

int main()
{
	cin >> t;
	while (t--)
	{
		cin >> d >> m;
		ans = 1;
		for (int i = 0; i < 30; i++)
		{
			if (d < (1 << i))
			{
				break;
			}
			ans = ans * ((min(d, (1 << (i + 1)) - 1) - (1 << i) + 2) % m);
		}
		ans--;
		if (ans < 0)
		{
			ans += 1ll * m;
		}
		cout << ans << endl;
	}
	return 0;
}


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

int t;
int d, m;
ll ans;

int main()
{
	cin >> t;
	while (t--)
	{
		cin >> d >> m;
		ans = 1;
		for (int i = 0; i < 30; i++)
		{
			if (d < (1 << i))
			{
				break;
			}
			ans = ans * ((min(d, (1 << (i + 1)) - 1) - (1 << i) + 2) % m);
		}
		ans--;
		if (ans < 0)
		{
			ans += m;
		}
		cout << ans << endl;
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
#define int long long
int d,m,t,ans,mx,a[35],f[35][35];
int dfs(int x,int y)
{
//	cerr<<f[x][y];
	if(y>=mx&&x)return 0;
	if(f[x][y]!=-1)return f[x][y];
	int now=0;
	for(int i=y+1;i<=mx;i++)
	{
		now+=dfs(x-1,i)*a[i]%m;
		now%=m;
	}
	return f[x][y]=now;
}
signed main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>d>>m;ans=0;mx=0;
		memset(a,0,sizeof(a));
		memset(f,-1,sizeof(f));
		int now=d;
		while(now)
		{
			mx++;
			now>>=1;
		}
	//	cerr<<t;
		a[mx]=d-(1<<(mx-1))+1;
		for(int i=1,j=1;i<mx;i++,j+=j)
		{
			a[i]=j;
		}
		for(int i=1;i<=mx;i++)
			f[0][i]=1;
		for(int i=mx;i>=1;i--)
			ans+=dfs(i,0),ans%=m;
		cout<<ans<<endl;
	}
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--)
	{
		ll n,mod,ans=1,l=0,flag=0;
		cin>>n>>mod;n++;
		for(ll i=1;i<=1e10;i*=2)
		{
			if(flag)break;
			if(i>n)flag=1,ans=ans*(n-i/2+1)%mod;
			else ans=ans*(i/2+1)%mod;
		}
		cout<<(ans-1+mod)%mod<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--)
	{
		ll n,mod,ans=1,l=0,flag=0;
		cin>>n>>mod;n++;
		for(ll i=1;i<=1e10;i*=2)
		{
			if(flag)break;
			if(i>n)flag=1,ans=ans*(n-i/2+1);
			else ans=ans*(i/2+1);
		}
		cout<<(ans-1+mod)%mod<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--)
	{
		ll n,mod,ans=1,l=0,flag=0;
		cin>>n>>mod;n++;
		for(int i=1;i<=1e10;i*=2)
		{
			if(flag)break;
			if(i>n)flag=1,ans=ans*(n-i/2+1);
			else ans=ans*(i/2+1);
		}
		cout<<(ans-1+mod)%mod<<endl;
	}
	return 0;
}


//============================================================================
// Name        : a.cpp
// Author      : Ernestico
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <bits/stdc++.h>
#define endl '\n'

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair <int,int> pii;

const int maxn = 200005;
const db pi = acos(-1);
const db eps = 1e-9;
const int mod =  998244353;

int main() {
#ifdef ernestico
	freopen ("a.in", "r", stdin );
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int test; cin >> test;
	while ( test-- ) {
		int m, d; cin >> d >> m;
		vector < vector <int> > dp ( 40, vector<int> (40) );

		int lg = __lg ( d );
		for ( int i = 0; i < lg; i ++ )
			dp[1][i] = ( 1 << i ) % m;
		dp[1][lg] = ( d - (1<<lg) + 1 ) % m;

		for ( int i = 1; i <= lg; i ++ )
			dp[1][i] = ( dp[1][i] + dp[1][i-1] ) % mod;

		int sol = dp[1][lg];
		for ( int i = 2; i <= lg+1; i ++ ) {
			for ( int j = 1; j < lg; j ++ )
				dp[i][j] =  1ll * dp[i-1][j-1] * ( 1<<(j) ) % m;
			dp[i][lg] = 1ll * dp[i-1][lg-1] * ( d - (1<<lg) + 1) % m;

			for ( int j = 1; j <= lg; j ++ )
				dp[i][j] = ( dp[i][j-1] + dp[i][j] ) % m;
			sol = ( sol + dp[i][lg] ) % m;
		}

		cout << sol << endl;
	}

	return 0;
}


//============================================================================
// Name        : a.cpp
// Author      : Ernestico
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <bits/stdc++.h>
#define endl '\n'

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair <int,int> pii;

const int maxn = 200005;
const db pi = acos(-1);
const db eps = 1e-9;
const int mod =  998244353;

int main() {
#ifdef ernestico
	freopen ("a.in", "r", stdin );
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int test; cin >> test;
	while ( test-- ) {
		int m, d; cin >> d >> m;
		vector < vector <int> > dp ( 40, vector<int> (40) );

		int lg = 31 - __builtin_clz ( d );
		for ( int i = 0; i < lg; i ++ )
			dp[1][i] = ( 1 << i ) % m;
		dp[1][lg] = ( d - (1<<lg) + 1 ) % m;

		for ( int i = 1; i <= lg; i ++ )
			dp[1][i] = ( dp[1][i] + dp[1][i-1] ) % m;

		for ( int i = 2; i <= lg+1; i ++ ) {
			for ( int j = 1; j < lg; j ++ )
				dp[i][j] =  1ll * dp[i-1][j-1] * ( 1<<j ) % m;
			dp[i][lg] = 1ll * dp[i-1][lg-1] * ( d - (1<<lg) + 1) % m;

			for ( int j = 1; j <= lg; j ++ )
				dp[i][j] = ( dp[i][j-1] + dp[i][j] ) % m;
		}

		int sol = 0;
		for ( int i = 1; i <=lg+1; i ++ )
			sol = ( sol + dp[i][lg] ) % m;

		cout << sol << endl;
	}

	return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define INF 2147483647
#define mod 1000000007
#define lowbit(n) n&-n
#define mem(x) memset(x,0,sizeof(x))
#define fo(i,a,b) for(int i=a,_e=b;i<=_e;++i)
#define fd(i,a,b) for(int i=b,_e=a;i>=_e;--i)
#define ff(i,a,b) for(int i=a,_e=b;i<_e;++i)
#define Cout(a,l,r) fo(i,l,r) cout<<a[i]<<' ';cout<<endl
using namespace std;
const int maxn=200010;
typedef long long ll;
typedef long double ld;
int n;
ll m;
template<class T> inline void read(T &x){
	int f=0;x=0;char ch=getchar();
	for(;!isdigit(ch);ch=getchar())f|=(ch=='-');
	for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';
	if(f)x=-x;
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);

	int T;cin>>T;
	while(T--){
		ll d;
		cin>>d>>m;
		if(d==1){
			cout<<1%m<<endl;
			continue;
		}
		ll cnt=0;
		for(int i=1;i<=(d);i<<=1) cnt++;
		cnt--;
		//cout<<cnt<<endl;
		ll x=d-(1<<cnt)+1;//cout<<x<<endl;
		ll ans=1;
		fo(i,2,cnt){
			ll c=(1<<(i-1))%m;
			ll t=(c*(ans+1))%m;
			(ans+=t)%=m;
		}
		ll t=(x*(ans+1ll))%m;
		(ans+=t)%=m;
		cout<<(ans+m)%m<<endl;

	}

	return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define INF 2147483647
#define mod 1000000007
#define lowbit(n) n&-n
#define mem(x) memset(x,0,sizeof(x))
#define fo(i,a,b) for(int i=a,_e=b;i<=_e;++i)
#define fd(i,a,b) for(int i=b,_e=a;i>=_e;--i)
#define ff(i,a,b) for(int i=a,_e=b;i<_e;++i)
#define Cout(a,l,r) fo(i,l,r) cout<<a[i]<<' ';cout<<endl
using namespace std;
const int maxn=200010;
typedef long long ll;
typedef long double ld;
int n,m;
template<class T> inline void read(T &x){
	int f=0;x=0;char ch=getchar();
	for(;!isdigit(ch);ch=getchar())f|=(ch=='-');
	for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';
	if(f)x=-x;
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);

	int T;cin>>T;
	while(T--){
		int d;
		cin>>d>>m;
		if(d==1){
			cout<<1%m<<endl;
			continue;
		}
		int cnt=0;
		for(int i=1;i<=(d);i<<=1) cnt++;
		cnt--;
		//cout<<cnt<<endl;
		int x=d-(1<<cnt)+1;//cout<<x<<endl;
		int ans=1;
		fo(i,2,cnt){
			int c=(1<<(i-1))%m;
			int t=(c*(ans+1))%m;
			(ans+=t)%=m;
		}
		int t=(x*(ans+1))%m;
		(ans+=t)%=m;
		cout<<ans<<endl;

	}

	return 0;
}


/*#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+100;
int vis[maxn],a[maxn],vis2[maxn];
vector <int> v;
int main()
{
    int T,i,k,n,x,ans,flag,up,cntup,mx,mx2;
    freopen("text.in","r",stdin);
    scanf("%d",&T);
    while (T--) {
        scanf("%d",&n);
        flag=1;
        for (i=1;i<=n+10;i++) {
            vis[i]=0;
        }
        mx=0;
        for (i=1;i<=n;i++) {
            scanf("%d",&a[i]);
            vis[a[i]]++;
            mx=max(mx,a[i]);
            if (a[i]>=n) {
                flag=0;
            }
        }
      //  printf("%d\n",mx);
        //flag=1;
       // for (i=1;i<=n;i++) {
            //vis[i]=0;
       //     printf("%d ",vis[i]);
       // }
        //printf("\n");
        for (i=2;i<=n;i++) {
            if (vis[i]>vis[i-1]||vis[i]>=3) {flag=0; break ;}
        }
        if (flag==0) {
            printf("0\n");
            continue ;
        }
        mx2=0;
        for (i=1;i<=n;i++) {
            if (vis[i]==2) {
                mx2=max(mx2,i);
            }
        }
        for (i=1;i<=n+10;i++) {
            vis2[i]=0;
        }
        up=0; ans=0; v.clear(); cntup=0;
        for (i=1;i<=n;i++) {
            up=max(up,a[i]);
            vis2[a[i]]+=1; cntup=max(cntup,vis2[a[i]]);
            if (up==i&&cntup==1) {
                if (mx+up==n) {
                    ans++;
                    v.push_back(i);
                }
                if (mx2!=mx&&mx2+up==n) {
                    ans++;
                    v.push_back(i);
                }
            }
        }
        printf("%d\n",ans);
        if (ans) {
            for (int i=0;i<v.size();i++) {
                printf("%d %d\n",v[i],n-v[i]);
            }
        }
    }
    return 0;
}
*/

#include <bits/stdc++.h>
using namespace std;
int dp[35][35];
int f(int x)
{
    int ret=0;
    while (x) {
        ret++;
        x/=2;
    }
    return ret ;
}
int main()
{
   // freopen("text.in","r",stdin);
    int T,n,m,i,j,k,val,cnt,d,ans;
    scanf("%d",&T);
    while (T--) {
        scanf("%d%d",&d,&m);
        memset(dp,0,sizeof(dp));
        for (int i=1;i<=31;i++) {
            dp[1][i]=(1<<(i-1))%m;
        }
        for (i=2;i<=31;i++) {
            for (j=1;j<=31;j++) {
                //dp[i][j]=(1<<(j-1))%m;
                for (k=1;k<j;k++) {
                    dp[i][j]+=1LL*dp[i-1][k]*(1<<(j-1))%m;
                    dp[i][j]%=m;
                }
                //printf("%d %d %d\n",i,j,dp[i][j]);
            }
        }
        ans=0;
        cnt=f(d); val=d-(1<<(cnt-1))+1;
        for (i=2;i<=cnt;i++) {
            for (j=1;j<cnt;j++) {
                ans+=dp[i][j];
                ans%=m;
            }
            for (j=1;j<cnt;j++) {
                ans+=1LL*dp[i-1][j]*val%m;
                ans%=m;
            }
        }
        printf("%d\n",(ans+d)%m);
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define ll long long
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        if(n==1&&m!=1)
            cout<<1<<"\n";
        else if(n==1&&m==1)
        	cout<<0<<"\n";
        else
        {
            int t=log2(n)-1;
            ll ans=1;
            ll k=t;
            while(k>=0)
            {
                ans*=((1<<k)+1)%m;
                k--;
            }
            ans*=(n+2-(1<<(t+1)))%m;
            printf("%lld\n",(ans-1+m)%m);
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define ll long long
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
       	ll ans=1,t=log2(n);
       	for(int i=0;i<t;++i)
       		ans=ans*((1<<i)+1)%m;
       	ans=ans*(n-(1<<t)+2)%m;
       	ans=(ans-1+m)%m;
       	printf("%lld\n",ans);
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define ll long long
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        if(n==1&&m!=1)
            cout<<1<<"\n";
        else if(n==1&&m==1)
        	cout<<0<<"\n";
        else
        {
            int t=log2(n)-1;
            ll ans=1;
            ll k=t;
            while(k>=0)
            {
                ans*=((ll)(1<<k)%m+1%m)%m;
                k--;
            }
            ans*=(n+2-(1<<(t+1)))%m;
            printf("%lld\n",(ans-1+m)%m);
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define ll long long
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        if(n==1)
            cout<<1<<"\n";
        else
        {
            int t=log2(n)-1;
            ll ans=1;
            ll k=t;
            while(k>=0)
            {
                ans*=((1<<k)+1)%m;
                k--;
            }
            ans*=(n+2-(1<<(t+1)))%m;
            printf("%lld\n",(ans-1+m)%m);
        }
    }
    return 0;
}


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

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;

#define mp make_pair
#define f first
#define s second
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define trav(a, x) for (auto &a : x)

const int MOD = 1e9 + 7; // 998244353;
const int MX = 1 << 20;
const ll INF = 1e18;
const ld PI = acos((ld)-1);
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

template <class T>
bool ckmin(T &a, const T &b)
{
    return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T &a, const T &b)
{
    return a < b ? a = b, 1 : 0;
}
int pct(int x) { return __builtin_popcount(x); }

namespace input
{
    template <class T>
    void re(complex<T> &x);
    template <class T1, class T2>
    void re(pair<T1, T2> &p);
    template <class T>
    void re(vector<T> &a);
    template <class T, size_t SZ>
    void re(array<T, SZ> &a);

    template <class T>
    void re(T &x) { cin >> x; }
    void re(double &x)
    {
        string t;
        re(t);
        x = stod(t);
    }
    void re(ld &x)
    {
        string t;
        re(t);
        x = stold(t);
    }
    template <class T, class... Ts>
    void re(T &t, Ts &... ts)
    {
        re(t);
        re(ts...);
    }

    template <class T>
    void re(complex<T> &x)
    {
        T a, b;
        re(a, b);
        x = {a, b};
    }
    template <class T1, class T2>
    void re(pair<T1, T2> &p) { re(p.f, p.s); }
    template <class T>
    void re(vector<T> &a) { F0R(i, sz(a))
            re(a[i]); }
    template <class T, size_t SZ>
    void re(array<T, SZ> &a) { F0R(i, SZ)
            re(a[i]); }
} // namespace input

using namespace input;

namespace output
{
    void pr(int x) { cout << x; }
    void pr(long x) { cout << x; }
    void pr(ll x) { cout << x; }
    void pr(unsigned x) { cout << x; }
    void pr(unsigned long x) { cout << x; }
    void pr(unsigned long long x) { cout << x; }
    void pr(float x) { cout << x; }
    void pr(double x) { cout << x; }
    void pr(ld x) { cout << x; }
    void pr(char x) { cout << x; }
    void pr(const char *x) { cout << x; }
    void pr(const string &x) { cout << x; }
    void pr(bool x) { pr(x ? "true" : "false"); }
    template <class T>
    void pr(const complex<T> &x) { cout << x; }

    template <class T1, class T2>
    void pr(const pair<T1, T2> &x);
    template <class T>
    void pr(const T &x);

    template <class T, class... Ts>
    void pr(const T &t, const Ts &... ts)
    {
        pr(t);
        pr(ts...);
    }
    template <class T1, class T2>
    void pr(const pair<T1, T2> &x)
    {
        pr("{", x.f, ", ", x.s, "}");
    }
    template <class T>
    void pr(const T &x)
    {
        pr("{"); // const iterator needed for vector<bool>
        bool fst = 1;
        for (const auto &a : x)
            pr(!fst ? ", " : "", a), fst = 0;
        pr("}");
    }

    void ps() { pr("\n"); } // print w/ spaces
    template <class T, class... Ts>
    void ps(const T &t, const Ts &... ts)
    {
        pr(t);
        if (sizeof...(ts))
            pr(" ");
        ps(ts...);
    }

    void pc() { pr("]\n"); } // debug w/ commas
    template <class T, class... Ts>
    void pc(const T &t, const Ts &... ts)
    {
        pr(t);
        if (sizeof...(ts))
            pr(", ");
        pc(ts...);
    }
#define dbg(x...) pr("[", #x, "] = ["), pc(x);
} // namespace output

using namespace output;

namespace io
{
    void setIn(string s) { freopen(s.c_str(), "r", stdin); }
    void setOut(string s) { freopen(s.c_str(), "w", stdout); }
    void setIO(string s = "")
    {
        ios_base::sync_with_stdio(0);
        cin.tie(0); // fast I/O
        // cin.exceptions(cin.failbit);
        // throws exception when do smth illegal
        // ex. try to read letter into int
        if (sz(s))
        {
            setIn(s + ".in"), setOut(s + ".out");
        } // for USACO
    }
} // namespace io

using namespace io;

#define Lint long long

Lint mod(Lint a, Lint b, Lint c) {
    Lint ans = 1;
    while(b > 0) {
        if(b & 1) ans = ans * a % c; //位运算b&1与b%2相同
        a = a * a % c;
        b >>= 1;                     //位运算：b/=2;
    }
    return ans;
}

bool check(int l,int r, ll sum) {
    if((l+r)*(r-l+1)/2 == sum) {
        return true;
    }
    return false;
}

ll dp[50][50][50];

void solve() {
    memset(dp,0,sizeof(dp));
    ll d,m;
    re(d,m);
    vi a;
    ll tmp = 1;
    FOR(i,0,40) {
        if (tmp >= d) {
            a.push_back(d);
            break;
        }
        a.push_back(tmp);
        d-=tmp;
        tmp <<= 1;
    }
    dbg(a);

    ll ans = 0;
    FOR(k,1,a.size()+1) {
        FOR(l, 0, a.size()) {
            FOR(r, l + k -1, a.size()) {
                if(k == 1) {
                    FOR(i,l,r+1) {
                        dp[k][l][r] += a[i];
                        dp[k][l][r] %= m;
                     //   dbg(k,l,r,dp[k][l][r] );
                    }
                    continue;
                }
                FOR(t, l, r) {//t left value
                    dp[k][l][r] += (dp[1][t][t] * dp[k-1][t+1][r]) %m;
                    dp[k][l][r] %=m;
                   // dbg(k,l,r,t,dp[k][l][r] );
                }
            }
        }
        ans += dp[k][0][a.size()-1];
        ans %=m;
    }

    ps(ans);
}

int main()
{
    setIO();
    int t=1;
    re(t);
    while(t--) {
        solve();

    }

    return 0;
}
/*
5 3
3 1 2
 */


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

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;

#define mp make_pair
#define f first
#define s second
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define trav(a, x) for (auto &a : x)

const int MOD = 1e9 + 7; // 998244353;
const int MX = 1 << 20;
const ll INF = 1e18;
const ld PI = acos((ld)-1);
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

template <class T>
bool ckmin(T &a, const T &b)
{
    return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T &a, const T &b)
{
    return a < b ? a = b, 1 : 0;
}
int pct(int x) { return __builtin_popcount(x); }

namespace input
{
    template <class T>
    void re(complex<T> &x);
    template <class T1, class T2>
    void re(pair<T1, T2> &p);
    template <class T>
    void re(vector<T> &a);
    template <class T, size_t SZ>
    void re(array<T, SZ> &a);

    template <class T>
    void re(T &x) { cin >> x; }
    void re(double &x)
    {
        string t;
        re(t);
        x = stod(t);
    }
    void re(ld &x)
    {
        string t;
        re(t);
        x = stold(t);
    }
    template <class T, class... Ts>
    void re(T &t, Ts &... ts)
    {
        re(t);
        re(ts...);
    }

    template <class T>
    void re(complex<T> &x)
    {
        T a, b;
        re(a, b);
        x = {a, b};
    }
    template <class T1, class T2>
    void re(pair<T1, T2> &p) { re(p.f, p.s); }
    template <class T>
    void re(vector<T> &a) { F0R(i, sz(a))
            re(a[i]); }
    template <class T, size_t SZ>
    void re(array<T, SZ> &a) { F0R(i, SZ)
            re(a[i]); }
} // namespace input

using namespace input;

namespace output
{
    void pr(int x) { cout << x; }
    void pr(long x) { cout << x; }
    void pr(ll x) { cout << x; }
    void pr(unsigned x) { cout << x; }
    void pr(unsigned long x) { cout << x; }
    void pr(unsigned long long x) { cout << x; }
    void pr(float x) { cout << x; }
    void pr(double x) { cout << x; }
    void pr(ld x) { cout << x; }
    void pr(char x) { cout << x; }
    void pr(const char *x) { cout << x; }
    void pr(const string &x) { cout << x; }
    void pr(bool x) { pr(x ? "true" : "false"); }
    template <class T>
    void pr(const complex<T> &x) { cout << x; }

    template <class T1, class T2>
    void pr(const pair<T1, T2> &x);
    template <class T>
    void pr(const T &x);

    template <class T, class... Ts>
    void pr(const T &t, const Ts &... ts)
    {
        pr(t);
        pr(ts...);
    }
    template <class T1, class T2>
    void pr(const pair<T1, T2> &x)
    {
        pr("{", x.f, ", ", x.s, "}");
    }
    template <class T>
    void pr(const T &x)
    {
        pr("{"); // const iterator needed for vector<bool>
        bool fst = 1;
        for (const auto &a : x)
            pr(!fst ? ", " : "", a), fst = 0;
        pr("}");
    }

    void ps() { pr("\n"); } // print w/ spaces
    template <class T, class... Ts>
    void ps(const T &t, const Ts &... ts)
    {
        pr(t);
        if (sizeof...(ts))
            pr(" ");
        ps(ts...);
    }

    void pc() { pr("]\n"); } // debug w/ commas
    template <class T, class... Ts>
    void pc(const T &t, const Ts &... ts)
    {
        pr(t);
        if (sizeof...(ts))
            pr(", ");
        pc(ts...);
    }
#define dbg(x...) pr("[", #x, "] = ["), pc(x);
} // namespace output

using namespace output;

namespace io
{
    void setIn(string s) { freopen(s.c_str(), "r", stdin); }
    void setOut(string s) { freopen(s.c_str(), "w", stdout); }
    void setIO(string s = "")
    {
        ios_base::sync_with_stdio(0);
        cin.tie(0); // fast I/O
        // cin.exceptions(cin.failbit);
        // throws exception when do smth illegal
        // ex. try to read letter into int
        if (sz(s))
        {
            setIn(s + ".in"), setOut(s + ".out");
        } // for USACO
    }
} // namespace io

using namespace io;

#define Lint long long

Lint mod(Lint a, Lint b, Lint c) {
    Lint ans = 1;
    while(b > 0) {
        if(b & 1) ans = ans * a % c; //位运算b&1与b%2相同
        a = a * a % c;
        b >>= 1;                     //位运算：b/=2;
    }
    return ans;
}

bool check(int l,int r, ll sum) {
    if((l+r)*(r-l+1)/2 == sum) {
        return true;
    }
    return false;
}

ll dp[50][50][50];

void solve() {
    memset(dp,0,sizeof(dp));
    ll d,m;
    re(d,m);
    vi a;
    ll tmp = 1;
    FOR(i,0,40) {
        if (tmp >= d) {
            a.push_back(d);
            break;
        }
        a.push_back(tmp);
        d-=tmp;
        tmp <<= 1;
    }
  //  dbg(a);

    ll ans = 0;
    FOR(k,1,a.size()+1) {
        FOR(l, 0, a.size()) {
            FOR(r, l + k -1, a.size()) {
                if(k == 1) {
                    FOR(i,l,r+1) {
                        dp[k][l][r] += a[i];
                        dp[k][l][r] %= m;
                     //   dbg(k,l,r,dp[k][l][r] );
                    }
                    continue;
                }
                FOR(t, l, r) {//t left value
                    dp[k][l][r] += (dp[1][t][t] * dp[k-1][t+1][r]) %m;
                    dp[k][l][r] %=m;
                   // dbg(k,l,r,t,dp[k][l][r] );
                }
            }
        }
        ans += dp[k][0][a.size()-1];
        ans %=m;
    }

    ps(ans);
}

int main()
{
    setIO();
    int t=1;
    re(t);
    while(t--) {
        solve();

    }

    return 0;
}
/*
5 3
3 1 2
 */


#include <bits/stdc++.h>
using namespace std;
long long a[200005];

int ans[200005];
int res[200005];
int num[100];
int main()
{
int t;
cin>>t;
while(t--)
{
    long long d,m;
    cin>>d>>m;
    long long x=d;
     long  long tag=0;
     for(int i=0;i<100;i++)
        num[i]=0;
    for(int i=0;i<100;i++)
    {
        if(x%2)
            num[i]=1;
        x=x/2;
        if(x==0)
        {
            tag=i;
            break;

        }
    }
    long long ans=1;
    long long zl=2;
    for(long long i=2;i<=tag;i++)
    {
        ans=(ans%m+((1LL<<i)-(1LL<<(i-1)))*zl%m)%m;
        zl=(ans+1)%m;
      //  cout<<ans<<endl;
    }
    ans=(ans%m+(zl*(d-(1<<(tag))+1))%m)%m;
    ans=ans%m;
    cout<<ans<<endl;
}
}


#include <bits/stdc++.h>
using namespace std;
long long a[200005];

int ans[200005];
int res[200005];
int num[100];
int main()
{
int t;
cin>>t;
while(t--)
{
    long long d,m;
    cin>>d>>m;
    long long x=d;
     long  long tag=0;
     for(int i=0;i<100;i++)
        num[i]=0;
    for(int i=0;i<100;i++)
    {
        if(x%2)
            num[i]=1;
        x=x/2;
        if(x==0)
        {
            tag=i;
            break;

        }
    }
    long long ans=1;
    long long zl=2;
    for(long long i=2;i<=tag;i++)
    {
        ans=(ans%m+((1LL<<i)-(1LL<<(i-1)))*zl%m)%m;
        zl=(ans+1)%m;
      //  cout<<ans<<endl;
    }
    ans=(ans%m+(zl*(d-(1<<(tag))+1))%m)%m;
    ans=ans%m;
    if(d==1)
        cout<<1%m<<endl;
    else
    cout<<ans<<endl;
}
}


#include <bits/stdc++.h>
#define lson rt<<1
#define rson rt<<1|1
#define pi acos(-1)
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int N=1e5+5;
ll sum[100];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		ll d,m,len=0;
		scanf("%lld%lld",&d,&m);
		while((1<<len)<=d)len++;
		len--;
		sum[0]=1;
	    for(ll i=1;i<=len;i++)
	    {
		    sum[i]=1;
		    for(ll j=i-1;j>=0;j--)
		    {
		    	sum[i]+=(ll(pow(2,j))%m*sum[j])%m;
		    	sum[i]=sum[i]%m;
			}
	    }
		ll ans;
		ans=(ll(d-pow(2,len)+2)%m*sum[len]-1+m)%m;
		printf("%lld\n",ans);
	}
}


#include <bits/stdc++.h>
#define lson rt<<1
#define rson rt<<1|1
#define pi acos(-1)
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int N=1e5+5;
ll sum[100];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		ll d,m,len=0;
		scanf("%lld%lld",&d,&m);
		while((1<<len)<=d)len++;
		len--;
		sum[0]=1;
	    for(ll i=1;i<=len;i++)
	    {
		    sum[i]=1;
		    for(ll j=i-1;j>=0;j--)
		    {
		    	sum[i]+=(ll(pow(2,j))%m*sum[j])%m;
		    	sum[i]=sum[i]%m;
			}
	    }
		ll ans=0;
		ans+=(ll(d-pow(2,len)+2)%m*sum[len]-1)%m;
		printf("%lld\n",ans);
	}
}


#include<bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define pb push_back
#define inf 132913423039
typedef long long ll;
const ll mod=1e9+7;
const ll N=1e5+5;
const int max_=1e5+5;
const double eps=1e-7;
using namespace std;
ll gcd(ll a,ll b) {return b==0?a:gcd(b,a%b);}
ll lcm(ll a,ll b) { return a*b/gcd(a,b);    }
ll qp(ll a,ll b, ll p){ll ans = 1;while(b){if(b&1){ans = (ans*a)%p;--b;}a = (a*a)%p;b >>= 1;}return ans%p;}
ll Inv(ll x)          { return qp(x,mod-2,mod);}
ll C(ll n,ll m){if (m>n) return 0;ll ans = 1;for (int i = 1; i <= m; ++i) ans=ans*Inv(i)%mod*(n-i+1)%mod;return ans%mod;}
ll f[32];//打表每个区间个数
int main()
{
    f[0]=1;
    for(int i=1;i<=31;i++) f[i]=f[i-1]*2;
    ios;
    ll t;
    cin>>t;
    while(t--)
    {
        ll d,m,i,cnt=0,ans=1;
        cin>>d>>m;
        for(i=0;d>=f[i];i++);//找到l所在区间并且计算数量
        cnt=d-f[i-1]+1;
        for(ll j=0;j<=i-2;j++) ans=ans*(f[j]+1)%m;
        ans=(ans)*(cnt+1)%m;
        cout<<(ans-1+m)%m<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define pb push_back
#define inf 132913423039
typedef long long ll;
const ll mod=1e9+7;
const ll N=1e5+5;
const int max_=1e5+5;
const double eps=1e-7;
using namespace std;
ll gcd(ll a,ll b) {return b==0?a:gcd(b,a%b);}
ll lcm(ll a,ll b) { return a*b/gcd(a,b);    }
ll qp(ll a,ll b, ll p){ll ans = 1;while(b){if(b&1){ans = (ans*a)%p;--b;}a = (a*a)%p;b >>= 1;}return ans%p;}
ll Inv(ll x)          { return qp(x,mod-2,mod);}
ll C(ll n,ll m){if (m>n) return 0;ll ans = 1;for (int i = 1; i <= m; ++i) ans=ans*Inv(i)%mod*(n-i+1)%mod;return ans%mod;}
ll f[32];//打表每个区间个数
int main()
{
    f[0]=1;
    for(int i=1;i<=31;i++) f[i]=f[i-1]*2;
    ios;
    ll t;
    cin>>t;
    while(t--)
    {
        ll d,m,i,cnt=0,ans=1;
        cin>>d>>m;
        for(i=0;d>=f[i];i++);//找到l所在区间并且计算数量
        cnt=d-f[i-1]+1;
        for(ll j=0;j<=i-2;j++) ans=ans*(f[j]+1)%m;
        ans=ans*(cnt+1)%m;
      if(m!=1)  cout<<ans-1<<endl;
      else      cout<<0<<endl;
    }
    return 0;
}


/*------------------------------CODE BY SWAPNIL DHOLA------------------------------*/
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<long long int,long long int> pll;
#define lp(i,a,b) for(long long int i=a;i<=b;i++)
#define rlp(i,a,b) for(long long int i=a;i>=b;i--)
#define print(a) for(auto x:a){cout<<x<<" ";}cout<<endl
#define printpll(a) for(auto x:a){cout<<x.first<<" "<<x.second<<endl;}
#define input(a) for(auto &x:a){cin>>x;}
#define inputpll(a) for(auto &x:a){cin>>x.ff;cin>>x.ss;}
#define FAST ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL)
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define pf push_front
#define sz size()
#define all(v) v.begin(),v.end()
#define mod 1000000007
#define linf LLONG_MAX
#define inf INT_MAX
#define endl '\n'
#define ub upper_bound
#define lb lower_bound
#define precise(x) fixed<<setprecision(x)
const ll N=2e5+5;
bool comp(pll a,pll b)
{
    if(a.ff==b.ff)
    {
        return a.ss<b.ss;
    }
    return a.ff>b.ff;
}
void solve()
{
    ll n,m,sum=0,i,maxpos=0;
    cin>>n>>m;
    ll a[m];
    input(a);
    vector<pll> b(m);
    for(i=0;i<m;i++)
    {
        b[i].ff=a[i];
        b[i].ss=i+1;
        sum+=a[i];
        maxpos=max(maxpos,n-a[i]+1);
    }

    if(maxpos<m)
    {
        cout<<-1<<endl;
        return;
    }
    if(sum<n)
    {
        cout<<-1<<endl;
        return;
    }
    vector<ll> ans(n+1,0);
    //sort(all(b),comp);
    ll l=1,r=b[0].ff;
    ans[b[0].ss]=1;
    for(i=1;i<m;i++)
    {
        ll l1=l;
        l=r+1;
        ll rem=m-i;
        l=min(l,maxpos-rem+1);
        if(l>n-b[i].ff+1)
        {
            cout<<-1<<endl;
            return;
        }
        ans[b[i].ss]=l;
        r=l+b[i].ff-1;
    }
    if(r<n)
    {
        cout<<-1;
        return;
    }
    for(i=1;i<=m;i++)
    {
        cout<<ans[i]<<" ";
    }
}
signed main()
{
    FAST;
    ll t=1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
}


/*------------------------------CODE BY SWAPNIL DHOLA------------------------------*/
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<long long int,long long int> pll;
#define lp(i,a,b) for(long long int i=a;i<=b;i++)
#define rlp(i,a,b) for(long long int i=a;i>=b;i--)
#define print(a) for(auto x:a){cout<<x<<" ";}cout<<endl
#define printpll(a) for(auto x:a){cout<<x.first<<" "<<x.second<<endl;}
#define input(a) for(auto &x:a){cin>>x;}
#define inputpll(a) for(auto &x:a){cin>>x.ff;cin>>x.ss;}
#define FAST ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL)
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define pf push_front
#define sz size()
#define all(v) v.begin(),v.end()
#define mod 1000000007
#define linf LLONG_MAX
#define inf INT_MAX
#define endl '\n'
#define ub upper_bound
#define lb lower_bound
#define precise(x) fixed<<setprecision(x)
const ll N=2e5+5;
void solve()
{
    ll d,m,i;
    cin>>d>>m;
    ll ans=1;
    for(i=0;i<30;i++)
    {
        if(d<(ll)(1<<i))
        {
            break;
        }
        ans=(ans*(min((ll)(1<<(i+1))-1,d)-(1<<i)+2))%m;
    }
    ans--;
    if(ans<0)
    {
        ans+=m;
    }
    cout<<ans<<endl;
}
signed main()
{
    FAST;
    ll t=1;
    cin>>t;
    while(t--)
    {
        solve();
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//qaq
const int maxn =2e5+100;
int a[maxn];
int main()
{
    ios::sync_with_stdio(0);
    int t;cin >> t;
    while(t--)
    {
        ll d, m, ans=1, now=1;
        cin >> d >> m;
        d++;
        while(now<=d/2)
        {
            ans = ans * (now+1)%m;
            now *= 2;
        }
        ans *= (d-now+1)%m;
        if(ans) cout << (ans-1)%m << '\n';
        else cout << m-1 << "\n";
    }
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int t;
ll d,m;
vector<ll>v;
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>d>>m;
		ll t1=1;
		v.clear();
		while(1)
		{
			if(d<=(t1<<1)-1)
			{
				v.push_back(d-t1+1);
				break;
			}
			v.push_back(t1);
			t1=t1<<1;
		}
		ll ans=v[0],tmp=v[0];
		for(int i=1;i<v.size();i++)
		{
			tmp=ans*v[i]+v[i];
			tmp%=m;
			ans+=tmp;
			ans%=m;
		}
		ans%=m;
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int t,d,m;
vector<int>v;
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>d>>m;
		int t1=1;
		v.clear();
		while(1)
		{
			if(d<=(t1<<1)-1)
			{
				v.push_back(d-t1+1);
				break;
			}
			v.push_back(t1);
			t1=t1<<1;
		}
		ll ans=v[0],tmp=v[0];
		for(int i=1;i<v.size();i++)
		{
			tmp=ans*v[i]+v[i];
			tmp%=m;
			ans+=tmp;
			ans%=m;
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include <iostream>
#include <cstdio>

typedef long long LL;

LL dp[33][33];

int main() {
	int  t;
	scanf("%d", &t);
	while (t--) {
		int d, m;
		scanf("%d%d", &d, &m);
		int max_bit = 0;
		while ((1LL << max_bit) <= d) ++max_bit;
		--max_bit;
		LL ans = 1;
		dp[0][0] = 1;
		for (int i = 1; i < max_bit; ++i) {
			dp[0][i] = (1 << i);
			ans += dp[0][i];
			ans %= m;
		}
		if (max_bit > 0) {
			dp[0][max_bit] = (d - (1 << max_bit) + 1) % m;
			ans += dp[0][max_bit];
			ans %= m;
		}
		for (int i = 1; i <= max_bit; ++i) {
			for (int j = i; j <= max_bit; ++j) {
				if (j < max_bit) {
					for (int k = 0; k < j; ++k) {
						dp[i][j] += dp[i - 1][k] * (1 << j) % m;
						dp[i][j] %= m;
					}
				}
				else {
					for (int k = 0; k < j; ++k)
					dp[i][j] += dp[i - 1][k] * (d - (1 << max_bit) + 1) % m;
				}
				ans += dp[i][j];
				ans %= m;
			}

		}
		for (int i = 0; i <= max_bit; ++i)
			for (int j = 0; j <= max_bit; ++j)
				dp[i][j] = 0;
		std::cout << ans % m << std::endl;
	}
	return 0;
}


#include <iostream>
#include <cstdio>

typedef long long LL;

LL dp[20][20];

int main() {
	int  t;
	scanf("%d", &t);
	while (t--) {
		int d, m;
		scanf("%d%d", &d, &m);
		int max_bit = 0;
		while ((1LL << max_bit) <= d) ++max_bit;
		--max_bit;
		LL ans = 1;
		dp[0][0] = 1;
		for (int i = 1; i < max_bit; ++i) {
			dp[0][i] = (1 << i);
			ans += dp[0][i];
			ans %= m;
		}
		if (max_bit > 0) dp[0][max_bit] = (d - (1 << max_bit) + 1) % m;
		ans += dp[0][max_bit];
		ans %= m;
		for (int i = 1; i <= max_bit; ++i) {
			for (int j = i; j <= max_bit; ++j) {
				if (j < max_bit) {
					for (int k = 0; k < j; ++k) {
						dp[i][j] += dp[i - 1][k] * (1 << j) % m;
						dp[i][j] %= m;
					}
				}
				else {
					for (int k = 0; k < j; ++k)
					dp[i][j] += dp[i - 1][k] * (d - (1 << max_bit) + 1) % m;
				}
				ans += dp[i][j];
				ans %= m;
			}

		}
		for (int i = 0; i <= max_bit; ++i)
			for (int j = 0; j <= max_bit; ++j)
				dp[i][j] = 0;
		std::cout << ans << std::endl;
	}
	return 0;
}


#include <iostream>
#include <cstdio>

typedef long long LL;

LL dp[20][20];

int main() {
	int  t;
	scanf("%d", &t);
	while (t--) {
		int d, m;
		scanf("%d%d", &d, &m);
		int max_bit = 0;
		while ((1LL << max_bit) <= d) ++max_bit;
		--max_bit;
		LL ans = 1;
		dp[0][0] = 1;
		for (int i = 1; i < max_bit; ++i) {
			dp[0][i] = (1 << i);
			ans += dp[0][i];
			ans %= m;
		}
		if (max_bit > 0) {
			dp[0][max_bit] = (d - (1 << max_bit) + 1) % m;
			ans += dp[0][max_bit];
			ans %= m;
		}
		for (int i = 1; i <= max_bit; ++i) {
			for (int j = i; j <= max_bit; ++j) {
				if (j < max_bit) {
					for (int k = 0; k < j; ++k) {
						dp[i][j] += dp[i - 1][k] * (1 << j) % m;
						dp[i][j] %= m;
					}
				}
				else {
					for (int k = 0; k < j; ++k)
					dp[i][j] += dp[i - 1][k] * (d - (1 << max_bit) + 1) % m;
				}
				ans += dp[i][j];
				ans %= m;
			}

		}
		for (int i = 0; i <= max_bit; ++i)
			for (int j = 0; j <= max_bit; ++j)
				dp[i][j] = 0;
		std::cout << ans % m << std::endl;
	}
	return 0;
}


//please ac
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<set>
#include<queue>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
using namespace std;
typedef long long ll;
const int maxn=60;
const int mod=998244353;
int d,m,st[maxn];
ll dp[maxn][maxn];
int main()
{
    //freopen("tte.txt","r",stdin);
    int t;scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&d,&m);
        memset(st,0,sizeof(st));
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=31;i++)
        {
            if((1<<i)>d)
            {
                st[i]=d-(1<<(i-1))+1+m;
                st[i]%=m;
                break;
            }
            else
            {
                st[i]=1<<(i-1);
                st[i]%=m;
            }
        }
        dp[0][0]=1;
        ll ans=0;
        for(int i=1;i<=31;i++)
        {
            for(int j=1;j<=30;j++)
            {
                for(int k=0;k<j;k++)
                {
                    dp[i][j]+=dp[i-1][k]*st[j]%m;
                    dp[i][j]%=m;
                }
                ans=(ans+dp[i][j])%m;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


//please ac
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<set>
#include<queue>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
using namespace std;
typedef long long ll;
const int maxn=60;
const int mod=998244353;
int d,m,st[maxn];
ll dp[maxn][maxn];
int main()
{
    //freopen("tte.txt","r",stdin);
    int t;scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&d,&m);
        memset(st,0,sizeof(st));
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=29;i++)
        {
            if((1<<i)>d)
            {
                st[i]=d-(1<<(i-1))+1+m;
                st[i]%=m;
                break;
            }
            else
            {
                st[i]=1<<(i-1);
                st[i]%=m;
            }
        }
        dp[0][0]=1;
        ll ans=0;
        for(int i=1;i<=30;i++)
        {
            for(int j=1;j<=30;j++)
            {
                for(int k=0;k<j;k++)
                {
                    dp[i][j]+=dp[i-1][k]*st[j]%m;
                    dp[i][j]%=m;
                }
                ans=(ans+dp[i][j])%m;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


//please ac
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<set>
#include<queue>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
using namespace std;
typedef long long ll;
const int maxn=60;
const int mod=998244353;
int d,m,st[maxn];
ll dp[maxn][maxn];
int main()
{
    //freopen("tte.txt","r",stdin);
    int t;scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&d,&m);
        memset(st,0,sizeof(st));
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=29;i++)
        {
            if((1<<i)>d)
            {
                st[i]=d-(1<<(i-1))+1+m;
                st[i]%=m;
                break;
            }
            else
            {
                st[i]=1<<(i-1);
                st[i]%=m;
            }
        }
        dp[0][0]=1;
        ll ans=0;
        for(int i=1;i<=30;i++)
        {
            for(int j=1;j<=30;j++)
            {
                for(int k=0;k<j;k++)
                {
                    dp[i][j]+=dp[i-1][k]*st[j]%m;
                    dp[i][j]%=mod;
                }
                ans=(ans+dp[i][j])%m;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define PI pair<int,int>
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
using namespace std;
const int LIM = 2e5+3;
ll d = 0, mod = 0;
ll dp[30][30];
ll solve(int bit, int len)
{
	if(log2(d) == bit)
		return d-(1<<bit)+1;
	ll &ret = dp[bit][len];
	if(ret != -1)return ret;
	ret = 0;
	for(int i = bit+1; i<=log2(d);i++)
	{
		ret += 1ll * ((1<<i)-(1<<(i-1))) * solve(i, len+1);
		ret %= mod;
	}
	ret += d-(1<<bit)+1;
	ret %= mod;
	return ret;
}
int main(){
	int tc;cin >> tc;
	while(tc--)
	{
		memset(dp, -1, sizeof dp);
		cin >> d >> mod;
		ll Ans = 0;
		cout << solve(0, 1)%mod << endl;
	}
}


#include <bits/stdc++.h>
#define ll long long
#define PI pair<int,int>
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
using namespace std;
const int LIM = 2e5+3;
ll d = 0, mod = 0;
ll dp[30][30];
ll solve(int bit, int len)
{
	if((int)log2(d) == bit)
		return d-(1<<bit)+1;
	ll &ret = dp[bit][len];
	if(ret != -1)return ret;
	ret = 0;
	for(int i = bit+1; i<=log2(d);i++)
	{
		ret += (1ll * ((1<<i)-(1<<(i-1))) * solve(i, len+1))%mod;
		ret %= mod;
	}
	ret += d-(1<<bit)+1;
	ret %= mod;
	return ret;
}
int main(){
	int tc;cin >> tc;
	while(tc--)
	{
		memset(dp, -1, sizeof dp);
		cin >> d >> mod;
		//cout << log2(d) << endl;
		ll Ans = 0;
		cout << solve(0, 1)%mod << endl;
	}
}


#include <bits/stdc++.h>
#define ll long long
#define PI pair<int,int>
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
using namespace std;
const int LIM = 2e5+3;
ll d = 0, mod = 0;
ll dp[30][30];
ll solve(int bit, int len)
{
	if(log2(d) == bit)
		return d-(1<<bit)+1;
	ll &ret = dp[bit][len];
	if(ret != -1)return ret;
	ret = 0;
	for(int i = bit+1; i<=log2(d);i++)
	{
		ret += 1ll * ((1<<i)-(1<<(i-1))) * solve(i, len+1);
		ret %= mod;
	}
	ret += d-(1<<bit)+1;
	ret %= mod;
	//cout << bit << " " << len << " " << ret << endl;
	return ret;
}
int main(){
	int tc;cin >> tc;
	while(tc--)
	{
		memset(dp, -1, sizeof dp);
		cin >> d >> mod;
		ll Ans = 0;
		cout << solve(0, 1) << endl;
	}
}


//D
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
int main(){
	int t;
	cin >> t;
	while(t--){
		ll d,p,ans = 1;
		cin >> d >> p;
		for(int i = 0;i <= 30;i++){
			if(d < (1ll<<i))break;
			ans = ans*(min((1ll<<(i+1)),d+1) - (1ll<<i)+1)%p;
		}
		cout << (ans-1)%p << endl;
	}
	return 0;
}


//D
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
int main(){
	int t;
	cin >> t;
	while(t--){
		ll d,p,ans = 1;
		cin >> d >> p;
		for(int i = 0;i <= 30;i++){
			if(d < (1ll<<i))break;
			ans = ans*(min((1ll<<(i+1)),d+1) - (1ll<<i)+1)%p;
		}
		cout << (ans-1+p)%p << endl;
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int t;
long long d, m;

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    cin >> t;

    while (t--)
    {
        cin >> d >> m;
        long long ans = 1;

        long long p = floor(log2(d)) + 1;
        for (long long i = 0; i < p - 1; i++)
        {
            ans *= (1ll << i) + 1ll;
            ans %= m;
        }

        ans *= d - (1ll << (p - 1)) + 2;
        ans += m - 1;
        ans %= m;

        cout << ans << "\n";
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int t;
long long d, m;

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    cin >> t;

    while (t--)
    {
        cin >> d >> m;
        long long ans = 1;

        long long p = floor(log2(d)) + 1;
        for (long long i = 0; i < p - 1; i++)
        {
            ans *= (1ll << i) + 1ll;
            ans %= m;
        }

        ans *= d - (1ll << (p - 1)) + 2;
        ans--;
        ans %= m;

        cout << ans << "\n";
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int t;
long long d, m;

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    cin >> t;

    while (t--)
    {
        cin >> d >> m;
        long long ans = 1;

        long long p = floor(log2(d)) + 1;
        for (long long i = 0; i < p - 1; i++)
        {
            ans *= ((1ll << i) + 1ll) % m;
            ans %= m;
        }

        ans *= d - (1ll << (p - 1)) + 2;
        ans += m - 1;
        ans %= m;

        cout << ans << "\n";
    }

    return 0;
}


#include <stdio.h>
#include <map>
using namespace std;
typedef long long ll;
ll n, m, T;
ll cal(ll n)
{
	ll tmp = 1, now = 1;
	for(int i = 1; ; i++)
	{
		if(tmp + now > n) return tmp;
	tmp *= 2;
	now+=tmp;
	}
}
ll solve(ll n)
{
	if(n==1) return 1;

	ll tmp = solve(cal(n));
	return (tmp + (n - (n / 2)) * (tmp + 1) % m) % m;
}
int main()
{

	scanf("%lld", &T);
	while(T--)
	{

		scanf("%lld %lld", &n, &m);
		printf("%lld\n", solve(n));
	}
}


#include <stdio.h>
#include <map>
using namespace std;
typedef long long ll;
ll n, m, T;
ll cal(ll n)
{
	ll tmp = 2, now = 1;
	for(int i = 1; ; i++)
	{
		if(tmp + now >= n) return now;
		now+=tmp;
		tmp *= 2;
	}
}
ll solve(ll n)
{
	//printf("%lld<<\n", n);
	if(n==1) return 1;
	ll tmp = solve(cal(n));
	return (tmp + (tmp + 1) * (n - cal(n)) % m) % m;
}
int main()
{
	scanf("%lld", &T);
	while(T--)
	{
		scanf("%lld %lld", &n, &m);
		printf("%lld\n", solve(n) % m);
	}
}


#include <stdio.h>
#include <map>
using namespace std;
typedef long long ll;
ll n, m, T;
ll cal(ll n)
{
	ll tmp = 2, now = 1;
	for(int i = 1; ; i++)
	{
		if(tmp + now >= n) return now;
		now+=tmp;
		tmp *= 2;
	}
}
ll solve(ll n)
{
	//printf("%lld<<\n", n);
	if(n==1) return 1;
	ll tmp = solve(cal(n));
	return (tmp + (tmp + 1) * (n - cal(n))) % m;
}
int main()
{
	scanf("%lld", &T);
	while(T--)
	{
		scanf("%lld %lld", &n, &m);
		printf("%lld\n", solve(n));
	}
}


#include <stdio.h>
#include <map>
using namespace std;
typedef long long ll;
ll n, m, T;
ll cal(ll n)
{
	ll tmp = 2, now = 1;
	for(int i = 1; ; i++)
	{
		if(tmp + now >= n) return now;
		now+=tmp;
		tmp *= 2;
	}
}
ll solve(ll n)
{
	//printf("%lld<<\n", n);
	if(n==1) return 1;
	ll tmp = solve(cal(n));
	return (tmp + (tmp + 1) * (n - cal(n)) % m) % m;
}
int main()
{
	scanf("%lld", &T);
	while(T--)
	{
		scanf("%lld %lld", &n, &m);
		printf("%lld\n", solve(n));
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAX_N=1e4+20;
const int INF=0x3f3f3f3f;
const LL MOD=998244353;
int T;
LL d,m;
LL a[60];
LL dp[60][60];

void fun(){
    a[1]=1;
    for(int i=2;i<=40;i++){
        a[i]=1LL<<(i-1);
        if((1LL<<i)>d){
            a[i]-=((1LL<<i)-d-1);
            break;
        }
    }

}
void solve(){
    fun();

    for(int i=0;i<40;i++)   dp[i][0]=1;
    for(int i=1;i<40;i++){
        for(int j=1;j<40;j++){
            dp[i][j]=(dp[i][j]+dp[i-1][j])%m;
            dp[i][j]=(dp[i][j]+dp[i-1][j-1]*a[i])%m;
          //  cout<<i<<","<<j<<","<<dp[i][j]<<","<<a[i]<<endl;
        }
    }
    LL ans=0;
    for(int i=1;i<40;i++)   ans=(ans+dp[36][i])%m;
    printf("%I64d\n",ans);

}
int main()
{
    //freopen("1.in","r",stdin);
    scanf("%d",&T);
    while(T--){
        memset(a,0,sizeof(a));
        memset(dp,0,sizeof(dp));
        scanf("%I64d%I64d",&d,&m);
        solve();
    }
}


#include<bits/stdc++.h>
#define fs first
#define se second
#define pb push_back
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef double db;

const int maxn=3e6+5;
const ll inf=0x3f3f3f3f;
const ll mod=1e9+7;

ll dp[100];

int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		int n,m;
		scanf("%d%d",&n,&m);
		int t=0;
		while(1){
			int tmp=1<<t;
			if(n<tmp) break;
			dp[t+1]=(dp[t]+dp[t]*tmp%m+tmp)%m;
			t++;
			n-=tmp;
		}
		if(n==0) printf("%lld\n",dp[t]);
		else{
			printf("%lld\n",(dp[t]+dp[t]*n%m+n)%m);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define fs first
#define se second
#define pb push_back
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef double db;

const int maxn=3e6+5;
const ll inf=0x3f3f3f3f;
const ll mod=1e9+7;

ll dp[100];

int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		int n,m;
		scanf("%d%d",&n,&m);
		int t=0;
		while(1){
			int tmp=1<<t;
			if(n<tmp) break;
			dp[t+1]=(dp[t]+dp[t]*tmp%m+tmp)%m;
			t++;
			n-=tmp;
		}
		printf("%lld\n",(dp[t]+dp[t]*n%m+n)%m);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int t;
    cin >> t;
    while(t--){
        ll n, m;
        cin >> n >> m;
        vector<int> s;
        for(ll i = 0; (1<<i) <= n; i++){
            if(n>=(1<<(i+1)))
                s.push_back((1<<i));
            else s.push_back(n-(1<<i)+1);
        }
        ll sum = 1;
        for(int i = 0; i < s.size(); i++){
            sum *= (s[i]+1ll);
            sum %= m;
            if(sum==0) sum = m;
        }
        sum--;
        cout << sum << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int t;
    cin >> t;
    while(t--){
        ll n, m;
        cin >> n >> m;
        vector<int> s;
        for(ll i = 0; (1<<i) <= n; i++){
            if(n>=(1<<(i+1)))
                s.push_back((1<<i));
            else s.push_back(n-(1<<i)+1);
        }
        ll sum = 1;
        for(int i = 0; i < s.size(); i++){
            sum *= (s[i]+1ll);
            sum %= m;
        }
        if(sum)
        sum--;
        cout << sum << endl;
    }
}


import java.io.*;
import java.util.*;
public class Main {
    static class Scan {
        private byte[] buf=new byte[1024];
        private int index;
        private InputStream in;
        private int total;
        public Scan()
        {
            in=System.in;
        }
        public int scan()throws IOException
        {
            if(total<0)
            throw new InputMismatchException();
            if(index>=total)
            {
                index=0;
                total=in.read(buf);
                if(total<=0)
                return -1;
            }
            return buf[index++];
        }
        public int scanInt()throws IOException
        {
            int integer=0;
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            int neg=1;
            if(n=='-')
            {
                neg=-1;
                n=scan();
            }
            while(!isWhiteSpace(n))
            {
                if(n>='0'&&n<='9')
                {
                    integer*=10;
                    integer+=n-'0';
                    n=scan();
                }
                else throw new InputMismatchException();
            }
            return neg*integer;
        }
        public double scanDouble()throws IOException
        {
            double doub=0;
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            int neg=1;
            if(n=='-')
            {
                neg=-1;
                n=scan();
            }
            while(!isWhiteSpace(n)&&n!='.')
            {
                if(n>='0'&&n<='9')
                {
                    doub*=10;
                    doub+=n-'0';
                    n=scan();
                }
                else throw new InputMismatchException();
            }
            if(n=='.')
            {
                n=scan();
                double temp=1;
                while(!isWhiteSpace(n))
                {
                    if(n>='0'&&n<='9')
                    {
                        temp/=10;
                        doub+=(n-'0')*temp;
                        n=scan();
                    }
                    else throw new InputMismatchException();
                }
            }
            return doub*neg;
        }
        public String scanString()throws IOException
        {
            StringBuilder sb=new StringBuilder();
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            while(!isWhiteSpace(n))
            {
                sb.append((char)n);
                n=scan();
            }
            return sb.toString();
        }
        private boolean isWhiteSpace(int n)
        {
            if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
            return true;
            return false;
        }
    }

    public static void main(String args[]) throws IOException {
        Scan input=new Scan();
        int test=input.scanInt();
        for(int t=1;t<=test;t++) {
            int n=input.scanInt();
            int arr[]=new int[n];
            int arr1[]=new int[n];
            int arr2[]=new int[n];
            for(int i=0;i<n;i++) {
                arr[i]=input.scanInt();
                arr1[i]=arr[i];
                arr2[i]=arr[i];
            }
            Set<Integer> hash_set=new HashSet<>();
            int indx=-1;
            int count=0;
            StringBuilder ans=new StringBuilder("");
            for(int i=0;i<n;i++){
                if(hash_set.contains(arr[i])) {
                    indx=i;
                    break;
                }
                hash_set.add(arr[i]);
            }
            if(indx!=-1 && check(arr1,indx)) {
                count++;
                ans.append(indx+" "+(arr.length-indx)+"\n");
            }
            int indx2=-1;
            hash_set=new HashSet<>();
            for(int i=arr.length-1;i>=0;i--){
                if(hash_set.contains(arr[i])) {
                    indx2=i;
                    break;
                }
                hash_set.add(arr[i]);
            }
            if(indx2!=-1 && indx2!=indx && check(arr2,indx2+1)) {
                count++;
                ans.append((indx2+1)+" "+(arr.length-indx2-1));
            }
            System.out.println(count+"\n"+ans);
        }
    }
    public static boolean check(int arr[],int indx) {
        Arrays.sort(arr,0,indx);
        Arrays.sort(arr,indx,arr.length);
        for(int i=0,j=1;i<indx;i++,j++) {
            if(arr[i]!=j) {
                return false;
            }
        }
        for(int i=indx,j=1;i<arr.length;i++,j++) {
            if(arr[i]!=j) {
                return false;
            }
        }
        return true;
    }
}