#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
int main() {
int t;
cin>>t;
while(t--){
char x;
int y;
cin>>x>>y;
if(x >= 'a' && x <= 'z'){
//小写
int tmp = x - 'a' + 1;
y -=tmp;
}else if(x>='A' && x <= 'Z'){
int tmp = x - 'A' + 1;
y+=tmp;
}
cout<<y<<endl;
}
return 0;
}