#include<bits/stdc++.h>
 
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef double llf;
typedef long double lf;
typedef std::pair<int,int> pii;
 
#define xx first
#define yy second
 
template<typename T> inline T max(T a,T b){return a>b?a:b;}
template<typename T> inline T min(T a,T b){return a<b?a:b;}
template<typename T> inline T abs(T a){return a>0?a:-a;}
template<typename T> inline bool repr(T &a,T b){return a<b?a=b,1:0;}
template<typename T> inline bool repl(T &a,T b){return a>b?a=b,1:0;}
template<typename T> inline T gcd(T a,T b){T t;if(a<b){while(a){t=a;a=b%a;b=t;}return b;}else{while(b){t=b;b=a%b;a=t;}return a;}}
template<typename T> inline T sqr(T x){return x*x;}
#define mp(a,b) std::make_pair(a,b)
#define pb push_back
#define I inline
#define mset(a,b) memset(a,b,sizeof(a))
#define mcpy(a,b) memcpy(a,b,sizeof(a))
 
#define fo0(i,n) for(int i=0,i##end=n;i<i##end;i++)
#define fo1(i,n) for(int i=1,i##end=n;i<=i##end;i++)
#define fo(i,a,b) for(int i=a,i##end=b;i<=i##end;i++)
#define fd0(i,n) for(int i=(n)-1;~i;i--)
#define fd1(i,n) for(int i=n;i;i--)
#define fd(i,a,b) for(int i=a,i##end=b;i>=i##end;i--)
#define foe(i,x)for(__typeof(x.end())i=x.begin();i!=x.end();++i)
 
struct Cg{I char operator()(){return getchar();}};
struct Cp{I void operator()(char x){putchar(x);}};
#define OP operator
#define RT return *this;
#define RX x=0;char t=P();while((t<'0'||t>'9')&&t!='-')t=P();bool f=0;\
if(t=='-')t=P(),f=1;x=t-'0';for(t=P();t>='0'&&t<='9';t=P())x=x*10+t-'0'
#define RL if(t=='.'){lf u=0.1;for(t=P();t>='0'&&t<='9';t=P(),u*=0.1)x+=u*(t-'0');}if(f)x=-x
#define RU x=0;char t=P();while(t<'0'||t>'9')t=P();x=t-'0';for(t=P();t>='0'&&t<='9';t=P())x=x*10+t-'0'
#define TR *this,x;return x;
I bool IS(char x){return x==10||x==13||x==' ';}template<typename T>struct Fr{T P;I Fr&OP,(int &x)
{RX;if(f)x=-x;RT}I OP int(){int x;TR}I Fr&OP,(ll &x){RX;if(f)x=-x;RT}I OP ll(){ll x;TR}I Fr&OP,(char &x)
{for(x=P();IS(x);x=P());RT}I OP char(){char x;TR}I Fr&OP,(char *x){char t=P();for(;IS(t);t=P());if(~t){for(;!IS
(t)&&~t;t=P())*x++=t;}*x++=0;RT}I Fr&OP,(lf &x){RX;RL;RT}I OP lf(){lf x;TR}I Fr&OP,(llf &x){RX;RL;RT}I OP llf()
{llf x;TR}I Fr&OP,(uint &x){RU;RT}I OP uint(){uint x;TR}I Fr&OP,(ull &x){RU;RT}I OP ull(){ull x;TR}};Fr<Cg>in;
#define WI(S) if(x){if(x<0)P('-'),x=-x;char s[S],c=0;while(x)s[c++]=x%10+'0',x/=10;while(c--)P(s[c]);}else P('0')
#define WL if(y){lf t=0.5;for(int i=y;i--;)t*=0.1;if(x>=0)x+=t;else x-=t,P('-');*this,(ll)(abs(x));P('.');if(x<0)\
x=-x;while(y--){x*=10;x-=floor(x*0.1)*10;P(((int)x)%10+'0');}}else if(x>=0)*this,(ll)(x+0.5);else *this,(ll)(x-0.5);
#define WU(S) if(x){char s[S],c=0;while(x)s[c++]=x%10+'0',x/=10;while(c--)P(s[c]);}else P('0')
template<typename T>struct Fw{T P;I Fw&OP,(int x){WI(10);RT}I Fw&OP()(int x){WI(10);RT}I Fw&OP,(uint x){WU(10);RT}
I Fw&OP()(uint x){WU(10);RT}I Fw&OP,(ll x){WI(19);RT}I Fw&OP()(ll x){WI(19);RT}I Fw&OP,(ull x){WU(20);RT}I Fw&OP()
(ull x){WU(20);RT}I Fw&OP,(char x){P(x);RT}I Fw&OP()(char x){P(x);RT}I Fw&OP,(const char *x){while(*x)P(*x++);RT}
I Fw&OP()(const char *x){while(*x)P(*x++);RT}I Fw&OP()(lf x,int y){WL;RT}I Fw&OP()(llf x,int y){WL;RT}};Fw<Cp>out;
 
#define pow pow_
 
int a,m;
 
inline ll mod(ll x,ll y)
{
	if(x<y)return x;
	return x%y+y;
}
 
inline ll mul(ll a,ll b,ll c)
{
	/*if(b<=c/a)return a*b;
	a%=c,b%=c;
	ll t=(a*b-c*(ll)((llf)a*b/c+0.5))%c;
	if(t<0)return t+c*2;return t+c;*/
	ll r=0;
	for(;b;b>>=1,a=mod(a*2,c))
		if(b&1)r=mod(r+a,c);
	return r;
}
 
inline ll pow(ll a,ll b,ll c)
{
	ll r=1;
	for(;b;b>>=1,a=mul(a,a,c))
		if(b&1)r=mul(r,a,c);
	return r;
}
 
std::map<ll,ll>ph;
 
inline ll solve(ll P)
{
	//a^a^a...%P
	if(P==1)return 1;
	//out,P,'\n';
	ll t=solve(ph[P]);
	return pow(a,t,P);
}
 
inline void solve()
{
	in,a,m;if(a==0){out,m,'\n';return;}
	int phi=1,tm=m;
	for(int i=2;i*i<=m;i++)if(m%i==0)
	{
		phi*=i-1;
		m/=i;
		while(m%i==0)m/=i,phi*=i;
	}
	if(m>1)phi*=m-1;
	m=tm;
	ll P=(ll)m*phi;
	std::map<int,int>a,b;
	for(int i=2;i*i<=m;i++)if(m%i==0)
	{
		do a[i]++,m/=i;while(m%i==0);
	}
	if(m>1)a[m]++;
	for(int i=2;i*i<=phi;i++)if(phi%i==0)
	{
		do a[i]++,phi/=i;while(phi%i==0);
	}
	if(phi>1)a[phi]++;
	while(1)
	{
		a.erase(1);
		b.clear();
		ll t=1;
		foe(i,a)
		{
			fo0(j,i->yy)t*=i->xx;
			int v=i->xx-1;
			for(int j=2;j*j<=v;j++)if(v%j==0)
			{
				do b[j]++,v/=j;while(v%j==0);
			}
			if(v>1)b[v]++;
			fo0(j,i->yy-1)b[i->xx]++;
		}
		if(t==1)break;
		ll t2=1;
		foe(i,b)fo0(j,i->yy)t2*=i->xx;
		ph[t]=t2;
		a.swap(b);
	}
	ll ans=solve(P);
	//out,pow(::a,ans,tm),' ',ans%tm,'\n';
	assert(pow(::a,ans,tm)%tm==ans%tm);
	out,ans,'\n';
}
 
int main()
{
	for(int T=in;T--;solve());
}

Measured memory_bytes: 256