#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
int N;
int i,k,len,tot;
char s[100];
scanf("%d",&N);
while(N--)
{
tot = 0;k=0;
scanf("%s",s);
len = strlen(s);
for(i = len-1;i>1;i--)
{
if(s[i] >= '1' && s[i] <= '9')
tot += ((s[i] - '1' + 1)*pow(16,k++));
else if(s[i] >= 'a' && s[i] <= 'f')
tot += ((s[i] - 'a' + 10)*pow(16,k++));
else if(s[i] >= 'A' && s[i] <= 'F')
tot += ((s[i] - 'A' + 10)*pow(16,k++));
}
printf("%d\n",tot);
}
return 0;
}