Problem: Description

KULASIS is the name of a system developed and operated by the Higher Education Research and Development Promotion Organization for the purpose of providing information on common subjects throughout the university on the Web, supporting students and faculty members, and improving services. KULASIS started with the development of the online syllabus in 2003, and has gradually expanded the system to include Web bulletin boards, course registration, grades (scoring registration, scoring confirmation from students).
Students can use functions such as confirmation of academic affairs information (class cancellations, class changes, reports), course registration, and scoring confirmation from both inside and outside the university from a personal computer or mobile phone. The number of logins exceeds 10,000 on many days, and it has become widespread as an academic affairs information system at Kyoto University, and is indispensable for taking common courses throughout the university. We are developing this KULASIS so that it can be applied not only to common subjects throughout the university, but also to undergraduate specialized courses and graduate schools.
http://www.z.k.kyoto-u.ac.jp/introduction_kulasis.html

Q, a student at Kyoto University, was logged in to KULASIS to form a late syllabus. When I was wondering which subject I was going to enter, KULASIS suddenly gave off a dazzling light and moved to another page.
The page to which the transition was made was as shown in the figure below. The subject name and evaluation (impossible, acceptable, good, excellent) are written in the 5x5 square, and 16 ● buttons are arranged on the grid.
It was Q that I did not know what happened, but apparently when I press the ● button on the grid, the evaluation of the subjects in the upper right, lower right, upper left, and lower left is not possible → Yes, Yes → Good, Good → Excellent, Excellent → Impossible.


<image>


You can press the ● button as many times as you like.
I'm not sure if KULASIS has been rewritten by someone or if I'm dreaming, but if I can confirm my grades with this, Q wanted to get as good a grade as possible.
Confident that he was collecting a lot of credits, Q decided to maximize his fighting power in preparation for his assignment to the laboratory six months later than the number of credits itself.
Combat power is the total value of the evaluation of each subject converted into impossible → 0 points, acceptable → 60 points, good → 70 points, excellent → 80 points. It is thought (in the Q department) to show how superior it is.

Now that the subjects displayed on the KULASIS screen and their evaluations are given, please describe the program that outputs the maximum value of the combat power that can be obtained.



Input

The number of test cases is given on the first line of input. The number of test cases is guaranteed to be 100 or less.
From the second line onward, 5x5 numbers are lined up, and the evaluation of the subject displayed on the KULASIS screen is given, and 1,2,3,4 correspond to impossible, acceptable, good, and excellent, respectively.
If it is 0, no subject is registered in that frame.
The test cases are separated by a blank line.

Output

Output the maximum value of combat power that can be obtained for each test case.

Example

Input

5

1 1 0 3 3
1 1 0 3 3
0 0 0 0 0
2 2 0 4 4
2 2 0 4 4

1 1 1 0 0
1 1 1 1 0
1 0 1 1 0
0 0 1 1 1
1 1 1 1 1

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

4 3 4 3 4
3 4 3 4 3
4 3 4 3 4
3 4 3 4 3
4 3 4 3 4

2 2 2 2 2
0 0 0 2 0
2 2 0 2 0
2 2 0 2 0
0 0 0 2 0


Output

1280
1420
0
1920
1020

Solution (in CPP):

Solution:
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>
#include<numeric>
#include<cassert>
#include<complex>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define rep(i,s,n) for(int i=(s); i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))
#define print(x) printf("%d\n",x)
#define EPS (1e-4)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0,1,0,-1};
const int _dy[] = {-1,0,1,0};

int getInt(){
  int ret = 0,c;
  c = getchar();
  while(!isdigit(c)) c = getchar();
  while(isdigit(c)){
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int dp[4][256];
int board[5][5];

int ten[4] = { 0,60,70,80 };

#define H(h,x) ((h>>(x+x)) & 3)

int solve(int now, int h){
  if(dp[now][h] != -1) return dp[now][h];
  int ret = 0;

  int tmp[5] = {0};
  if(board[now][0] != 0) tmp[0] = 1 + ((board[now][0] - 1) + H(h,0)) % 4;
  if(board[now][1] != 0) tmp[1] = 1 + ((board[now][1] - 1) + H(h,0) + H(h,1)) % 4;
  if(board[now][2] != 0) tmp[2] = 1 + ((board[now][2] - 1) + H(h,1) + H(h,2)) % 4;
  if(board[now][3] != 0) tmp[3] = 1 + ((board[now][3] - 1) + H(h,2) + H(h,3)) % 4;
  if(board[now][4] != 0) tmp[4] = 1 + ((board[now][4] - 1) + H(h,3)) % 4;

  REP(x,256){
    int rtmp = 0;
    if(tmp[0] != 0) rtmp += ten[((tmp[0] - 1) + H(x,0)) % 4];
    if(tmp[1] != 0) rtmp += ten[((tmp[1] - 1) + H(x,0) + H(x,1)) % 4];
    if(tmp[2] != 0) rtmp += ten[((tmp[2] - 1) + H(x,1) + H(x,2)) % 4];
    if(tmp[3] != 0) rtmp += ten[((tmp[3] - 1) + H(x,2) + H(x,3)) % 4];
    if(tmp[4] != 0) rtmp += ten[((tmp[4] - 1) + H(x,3)) % 4];

    if(now == 3){
      if(board[4][0] != 0)
        rtmp += ten[((board[4][0] - 1) + H(x,0)) % 4];
      if(board[4][1] != 0)
        rtmp += ten[((board[4][1] - 1) + H(x,0) + H(x,1)) % 4];
      if(board[4][2] != 0)
        rtmp += ten[((board[4][2] - 1) + H(x,1) + H(x,2)) % 4];
      if(board[4][3] != 0)
        rtmp += ten[((board[4][3] - 1) + H(x,2) + H(x,3)) % 4];
      if(board[4][4] != 0)
        rtmp += ten[((board[4][4] - 1) + H(x,3)) % 4];
      ret = max(ret, rtmp);
    }else{
      ret = max(ret, rtmp + solve(now+1,x));
    }
  }
  return dp[now][h] = ret;
}

int main(){
  int n = getInt();
  while(n --> 0){
    memset(dp, -1, sizeof(dp));
    REP(i,5) REP(j,5) board[i][j] = getInt();
    print(solve(0,0));
  }
  return 0;
}