#include <iostream> #include <algorithm> #include <cstring> using namespace std; int change(char n){ switch(n){ case 'A':return 0; case 'B':return 1; case 'C':return 2; case 'D':return 3; case 'E':return 4; case 'F':return 5; case 'G':return 6; case 'H':return 7; case 'I':return 8; case 'J':return 9; case 'K':return 10; case 'L':return 11; case 'M':return 12; case 'N':return 13; case 'O':return 14; case 'P':return 15; case 'Q':return 16; case 'R':return 17; case 'S':return 18; default :return -1; } } double power(double x, int n) { double val = 1.0; while (n--) val *= x; return val; } int main() { int num; cin>>num; while(num--){ char a,b,c; cin>>a>>b>>c; int t; t=change(a)*power(19,2)+change(b)*power(19,1)+change(c)*power(19,0); cout<<6857-t<<endl; } return 0; }