#include <iostream>
#include <map>
#include <cstring>
using namespace std;
int main()
{
map<int,int>m;
m[0]=12;
for(int i=1;i<=23;i++){
if(i<=12){
m[i]=12+i;
continue;
}
m[i]=i-12;
}
int t;
cin>>t;
char time[6];
while(t--){
scanf("%s",time);
int h=0;
if(time[1]!=':'){
h=(time[0]-48)*10+(time[1]-48);
if(time[3]=='0'&&time[4]=='0'){
printf("%d\n",m[h]);
}else puts("0");
}else{
h=time[0]-48;
if(time[2]=='0'&&time[3]=='0'){
printf("%d\n",m[h]);
}else puts("0");
}
}
return 0;
}
#include <map>
#include <cstring>
using namespace std;
int main()
{
map<int,int>m;
m[0]=12;
for(int i=1;i<=23;i++){
if(i<=12){
m[i]=12+i;
continue;
}
m[i]=i-12;
}
int t;
cin>>t;
char time[6];
while(t--){
scanf("%s",time);
int h=0;
if(time[1]!=':'){
h=(time[0]-48)*10+(time[1]-48);
if(time[3]=='0'&&time[4]=='0'){
printf("%d\n",m[h]);
}else puts("0");
}else{
h=time[0]-48;
if(time[2]=='0'&&time[3]=='0'){
printf("%d\n",m[h]);
}else puts("0");
}
}
return 0;
}