#include <stdio.h>
int main() {
int A, B, i, j, k, l, n, pw[4];
while (scanf("%d %d %d %d", &pw[0], &pw[1], &pw[2], &pw[3]) != EOF) {
scanf("%d", &n);
int g[n][4];
for (i = 0; i < n; i++) {
scanf("%d %d %d %d", &g[i][0], &g[i][1], &g[i][2], &g[i][3]);
}
for (i = 0; i < n; i++) {
A = 0;
B = 0;
int check[4] = {0, 0, 0, 0};
int check2[4] = {0, 0, 0, 0};
for (j = 0; j < 4; j++) {
if (g[i][j] == pw[j]) {
A += 1;
check[j] = 1;
check2[j] = 1;
}
}
for (k = 0; k < 4; k++) {
if (check[k] == 0) {
for (l = 0; l < 4; l++) {
if ((check2[l] == 0)) {
if (g[i][k] == pw[l]) {
check2[l] = 1;
B += 1;
}
}
}
}
}
printf("%dA%dB\n", A, B);
}
}
return 0;
}