#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
#define int long long
#define inf 0x7fffffff
int Test;
char cnt[7][7];
char en[7][7]{
{'0','0','0','0','0','0'},
{'0','1','1','1','1','1'},
{'0','0','1','1','1','1'},
{'0','0','0','*','1','1'},
{'0','0','0','0','0','1'},
{'0','0','0','0','0','0'}
};
int temp[8][2]{ {2,1},{2,-1},{-2,1},{-2,-1},{1,2},{1,-2},{-1,2},{1,2} };
bool check() {
for (int i = 1; i <= 5; i++)
for (int j = 1; j <= 5; j++)
if (cnt[i][j] != en[i][j]) return false;
return true;
}
int Step() {
int ret = 0;
for (int i = 1; i <= 5; i++)
for (int j = 1; j <= 5; j++)
if (cnt[i][j] != en[i][j] && cnt[i][j] != '*') ret++;
return ret;
}
bool IDA(int depth, int max_depth, int x, int y) {
if (depth + Step() > max_depth) return false;
if (check()) return true;
for (int i = 0; i < 8; i++) {
int tx = x + temp[i][0];
int ty = y + temp[i][1];
if (tx > 5 || tx < 1 || ty>5 || ty < 1) continue;
swap(cnt[x][y], cnt[tx][ty]);
if (IDA(depth + 1, max_depth, tx, ty)) return true;
swap(cnt[x][y], cnt[tx][ty]);
}
return false;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> Test;
while (Test--) {
int tempx, tempy;
for (int i = 1; i <= 5; i++)
for (int j = 1; j <= 5; j++) {
cin >> cnt[i][j];
if (cnt[i][j] == '*') tempx = i, tempy = j;
}
int depth = 0;
bool flag = false;
while (depth <= 15) {
if (IDA(0, depth, tempx, tempy)) {
cout << depth << endl;
flag = true;
break;
}
depth++;
}
if (flag == false) cout << -1 << endl;
}
return 0;
}