# include<stdio.h>
int a[250];
int gcd(int num,int temp)
{ if(temp==0)
return num;
else return gcd(temp,num%temp);}
int main(void)
{ int num,i,temp,t=0,n=0,cases,chk=1;
scanf("%d",&cases);
while(cases--)
{char c,w;
t=0;
n=0;
chk=1;
scanf("%d",&num);
w=getchar();
while((c=getchar())!='\n')
a[n++]=c-48;
temp=a[t];
t++;
while(t<n)
{ if(num==0)
{ for(i=0;i<n;++i)
printf("%d",a[i]);
printf("\n");
chk=0;
break;
} else if(temp<num)
{ temp=temp*10;
temp+=a[t];
t++;}
else if(temp==num)
{temp=a[t];
t++;}
else if(temp>num)
{temp=temp%num;}}
if(chk==1)
{if(temp>num)
temp=temp%num;
else if(temp==num)
temp=0;
t=gcd(num,temp);
printf("%d\n",t);
}}
return(0);}
IyBpbmNsdWRlPHN0ZGlvLmg+CmludCBhWzI1MF07CmludCBnY2QoaW50IG51bSxpbnQgdGVtcCkKeyAgIGlmKHRlbXA9PTApCiAgICByZXR1cm4gbnVtOwogICAgZWxzZSByZXR1cm4gZ2NkKHRlbXAsbnVtJXRlbXApO30KaW50IG1haW4odm9pZCkKeyAgIGludCBudW0saSx0ZW1wLHQ9MCxuPTAsY2FzZXMsY2hrPTE7CiAgICBzY2FuZigiJWQiLCZjYXNlcyk7CiAgICB3aGlsZShjYXNlcy0tKQogICAge2NoYXIgYyx3OwogICAgIHQ9MDsKICAgICBuPTA7CiAgICBjaGs9MTsKICAgIHNjYW5mKCIlZCIsJm51bSk7CiAgICB3PWdldGNoYXIoKTsKICAgIHdoaWxlKChjPWdldGNoYXIoKSkhPSdcbicpCiAgICBhW24rK109Yy00ODsKICAgIHRlbXA9YVt0XTsKICAgIHQrKzsKICAgIHdoaWxlKHQ8bikKICAgIHsgICBpZihudW09PTApCiAgICB7ICAgZm9yKGk9MDtpPG47KytpKQogICAgICAgIHByaW50ZigiJWQiLGFbaV0pOwogICAgICAgIHByaW50ZigiXG4iKTsKICAgICAgICBjaGs9MDsKICAgICAgICBicmVhazsKICAgIH0gICBlbHNlIGlmKHRlbXA8bnVtKQogICAgICAgIHsgICB0ZW1wPXRlbXAqMTA7CiAgICAgICAgICAgIHRlbXArPWFbdF07CiAgICAgICAgICAgIHQrKzt9CiAgICAgICAgZWxzZSBpZih0ZW1wPT1udW0pCiAgICAgICAgICAgICB7dGVtcD1hW3RdOwogICAgICAgICAgICAgICAgIHQrKzt9CiAgICAgICAgZWxzZSBpZih0ZW1wPm51bSkKICAgICAgICB7dGVtcD10ZW1wJW51bTt9fQogICAgaWYoY2hrPT0xKQogICAge2lmKHRlbXA+bnVtKQogICAgdGVtcD10ZW1wJW51bTsKICAgIGVsc2UgaWYodGVtcD09bnVtKQogICAgdGVtcD0wOwogICAgdD1nY2QobnVtLHRlbXApOwogICAgcHJpbnRmKCIlZFxuIix0KTsKICAgIH19CiAgICByZXR1cm4oMCk7fQ==