//Qua cau
// Tank like this
#include <iostream>
using namespace std;
int TC, N, mang[20][7], maxx, count, s;
int dy[3]={-1, 0, 1};
void backtrack(int sum, int x, int y){
if(x==1){
if(maxx<sum) maxx=sum;
s=1;
return;
}
for(int i=0; i<3; i++){
int yN=y+dy[i];
if(yN>0 && yN<=5){
if(mang[x-1][yN]!=2){
backtrack(sum+mang[x-1][yN], x-1, yN);
}
else if(mang[x-1][yN]==2&& count==0){
count++;
backtrack(sum, x-1, yN);
count--;
}
}
}
}
int main(){
//freopen("in.txt", "r", stdin);
cin>> TC;
for(int t=1; t<=TC; t++){
cin>> N;
for(int i=0; i<=N+1; i++){
for(int j=0; j<=6; j++){
if(i>=1 && i<=N && j>=1 && j<=5){
cin>> mang[i][j];
}
else mang[i][j]=2;
}
}
maxx=count=s=0;
backtrack(0,N+1, 3);
cout<< "#"<< t<< " ";
if(s==0) cout<< -1<< endl;
else cout<< maxx<< endl;
}
return 0;
}