import java.util.*;
public class uva10813 {
public static void main(String [] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
for(int x=0;x<n;x++) {
int arr[][] = new int[5][5];
for(int y=0;y<5;y++) {
for(int z=0;z<5;z++) {
if(y==2 && z==2)
arr[y][z]=0;
else
arr[y][z]=scn.nextInt();
}
}
boolean bool = true;
int lessFive = 0;
while(lessFive<75) {
int a = scn.nextInt();
lessFive++;
if(bool==true) {
int count = 0;
boolean boolCheck = true;
for(int y=0;y<5;y++) {
for(int z=0;z<5;z++) {
if(a == arr[y][z])
arr[y][z] = 0;
}
}
if(boolCheck == true) {
for(int y=0;y<5;y++) {
count = 0;
for(int z=0;z<5;z++) {
if(arr[y][z] == 0)
count++;
}
if(count == 5) {
System.out.println("BINGO after "+lessFive +" numbers announced");
bool = false;
boolCheck = false;
y=5;
}
}
}
if(boolCheck == true) {
for(int y=0;y<5;y++) {
count = 0;
for(int z=0;z<5;z++) {
if(arr[z][y] == 0)
count++;
}
if(count == 5) {
System.out.println("BINGO after "+lessFive +" numbers announced");
bool = false;
boolCheck = false;
y=5;
}
}
}
if(boolCheck == true) {
count = 0;
for(int y=0;y<5;y++) {
if(arr[y][y] == 0)
count++;
}
if(count == 5) {
System.out.println("BINGO after "+lessFive +" numbers announced");
bool = false;
boolCheck = false;
}
}
if(boolCheck == true) {
count = 0;
for(int y=4;y>-1;y--) {
if(arr[4-y][y] == 0)
count++;
}
if(count == 5) {
System.out.println("BINGO after "+lessFive +" numbers announced");
bool = false;
boolCheck = false;
}
}
}
}
}
}
}