/**********************************************************************************/
/*  Problem: a293 "A 百年國慶" from 2011 NPSC 國中組初賽                 */
/*  Language: CPP (613 Bytes)                                                     */
/*  Result: AC(0.1s, 388KB) judge by this@ZeroJudge                               */
/*  Author: lfs92002 at 2011-12-06 12:35:30                                       */
/**********************************************************************************/


#include<iostream>
using namespace std;
int main()
{
	int b[5][5],a[5][5];
	int T,_x,x,y,sum;

	cin>>T;
	for(y=0;y<5;y++)
		for(x=0;x<5;x++)
			cin>>a[y][x];

	for(_x=0;_x<T-1;_x++)
	{
		for(y=0;y<5;y++)
			for(x=0;x<5;x++)
				cin>>b[y][x];//c.4
		sum=0;
		for(y=0;y<5;y++)
			for(x=0;x<5;x++)
			{
				if(a[y][x]==8&&b[y][x]==8)
					sum+=5;
				else if(a[y][x]==1&&b[y][x]==1)
					sum+=1;
				else if(a[y][x]==8&&b[y][x]==1)
					sum+=2;
				else
					sum+=7;


			}
			cout<<sum<<endl;
			for(y=0;y<5;y++)
				for(x=0;x<5;x++)
					a[y][x]=b[y][x];
	}
	

	return 0;
}
