//O(N)
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
	freopen("award.in","r",stdin);
	freopen("award.out","w",stdout);
	int N,tests,i,o,v,k,l,j;
	long s1;
	char s[20];
	scanf("%d",&tests);
	while(tests)
	{

			scanf("%d%s",&v,s);
			o=v%10+2;s1=0;
			l=strlen(s);
			for(j=0;j<l; j++)
				if(s[j]!='0')
				 {if(s[j]=='A')
				  k=10;else k=s[j]-'0';
				  s1=s1+k*pow(o,l-(j+1));
				 }

	 printf("%ld\n",s1);
		tests--;
	}
	return 0;
}
