#include <bits/stdc++.h>
typedef long long ll;
#define M 1000000007
using namespace std;
struct Dice {
char top = 1, left = 1, bottom = 1, right = 1, back = 1, val = 1;
};
string field[5];
void print(Dice d) {
cout << " " << d.top << endl;
cout << d.left << " " << d.val << " " << d.right << endl;
cout << " " << d.bottom << endl;
cout << " " << d.back << endl;
}
bool eq(Dice d1, Dice d2) {
if (d1.back != d2.back) return false;
vector<char> d1or;
vector<char> d2or;
d1or.push_back(d1.top);
d1or.push_back(d1.left);
d1or.push_back(d1.bottom);
d1or.push_back(d1.right);
d2or.push_back(d2.top);
d2or.push_back(d2.left);
d2or.push_back(d2.bottom);
d2or.push_back(d2.right);
for (int rot = 0; rot < 4; ++rot) {
for (int i = 0; i <= 4; ++i) {
if (i == 4) return true;
if (d1or[i] != d2or[(i + rot) % 4]) break;
}
}
return false;
}
Dice rotaten(Dice d, char target) {
Dice ret;
ret = d;
if (d.val == target) return ret;
else if (d.top == target) {
ret.top = d.back;
ret.back = d.bottom;
ret.bottom = d.val;
ret.val = target;
} else if (d.back == target) {
int t = d.top;
ret.top = d.bottom;
ret.bottom = t;
ret.back = d.val;
ret.val = target;
} else if (d.bottom == target) {
ret.bottom = d.back;
ret.back = d.top;
ret.top = d.val;
ret.val = target;
} else if (d.left == target) {
ret.left = d.back;
ret.back = d.right;
ret.right = d.val;
ret.val = target;
} else {
ret.right = d.back;
ret.back = d.left;
ret.left = d.val;
ret.val = target;
}
return ret;
}
Dice ho4row() {
int row = 0;
int scol = 5;
for (int i = 0; i < 5; ++i) {
int cnt = 0;
for (int j = 0; j < 5; ++j) {
if (field[i][j] != '.') {
cnt++;
scol = min(scol, j);
}
}
if (cnt == 4) {
row = i;
break;
}
}
Dice d;
vector<char> r;
for (int i = scol; i < scol + 4; i++) {
r.push_back(field[row][i]);
}
d.val = r[0];
d.right = r[1];
d.back = r[2];
d.left = r[3];
for (int i = 0; i < 5; ++i) {
if (field[row-1][i] != '.') {
d.top = field[row-1][i];
}
if (field[row+1][i] != '.') {
d.bottom = field[row+1][i];
}
}
return d;
}
Dice ho3row() {
int row = 0;
int scol = 5;
for (int i = 0; i < 5; ++i) {
int cnt = 0;
scol = 5;
for (int j = 0; j < 5; ++j) {
if (field[i][j] != '.') {
cnt++;
scol = min(scol, j);
}
}
if (cnt == 3) {
row = i;
break;
}
}
Dice d;
vector<char> r;
for (int i = scol; i < scol + 3; i++) {
r.push_back(field[row][i]);
}
d.val = r[0];
d.right = r[1];
d.back = r[2];
for (int i = scol; i < scol + 3; ++i) {
if (field[row-1][i] != '.') {
d.top = field[row-1][i];
r.push_back(field[row-1][i]);
}
if (field[row+1][i] != '.') {
d.bottom = field[row+1][i];
r.push_back(field[row+1][i]);
}
}
sort(r.begin(), r.end());
/*for (char c : r) {
cout << c << endl;
}
*/
char c = '1';
int i = 0;
while (r[i] == c) {
i++;
c++;
}
d.left = c;
return d;
}
Dice ve4row() {
int col = 0;
int srow = 5;
for (int i = 0; i < 5; ++i) {
int cnt = 0;
srow = 5;
for (int j = 0; j < 5; ++j) {
if (field[j][i] != '.') {
cnt++;
srow = min(srow, j);
}
}
if (cnt == 4) {
col = i;
break;
}
}
Dice d;
vector<char> r;
for (int i = srow; i < srow + 4; i++) {
r.push_back(field[i][col]);
}
d.val = r[0];
d.right = r[1];
d.back = r[2];
d.left = r[3];
for (int i = 0; i < 5; ++i) {
if (field[i][col-1] != '.') {
d.bottom = field[i][col-1];
}
if (field[i][col+1] != '.') {
d.top = field[i][col+1];
}
}
return d;
}
Dice ve3row() {
int col = 0;
int srow = 5;
for (int i = 0; i < 5; ++i) {
int cnt = 0;
srow = 5;
for (int j = 0; j < 5; ++j) {
if (field[j][i] != '.') {
cnt++;
srow = min(srow, j);
}
}
if (cnt == 3) {
col = i;
break;
}
}
Dice d;
vector<char> r;
for (int i = srow; i < srow + 3; i++) {
r.push_back(field[i][col]);
}
d.val = r[0];
d.right = r[1];
d.back = r[2];
for (int i = srow; i < srow + 3; ++i) {
if (field[i][col-1] != '.') {
d.bottom = field[i][col-1];
r.push_back(field[i][col-1]);
}
if (field[i][col+1] != '.') {
d.top = field[i][col+1];
r.push_back(field[i][col+1]);
}
}
sort(r.begin(), r.end());
char c = '1';
int i = 0;
while (r[i] == c) {
i++;
c++;
}
d.left = c;
vector<char> vals = {x.val, x.top, x.bottom, x.left, x.right, x.back};
sort(vals.begin(), vals.end());
char look = '1';
for (char i = 0; i <= 5; ++i) {
if (vals[i] != look) {
exit(-1);
}
look++;
}
return d;
}
Dice special() {
// Handle case where layout is
/*
XX...
.XX..
..XX.
.....
.....
*/
// or
/*
...XX
..XX.
.XX..
.....
.....
*/
bool typeA = true;
int x = 0, y = 0;
bool found = false;
for (int i = 0; i < 5; ++i) {
if (found) break;
for (int j = 0; j < 5; ++j) {
if (field[i][j] != '.') {
y = i;
x = j;
found = true;
break;
}
}
}
if (x == 3 || field[y+1][x+1] == '.') {
typeA = false;
}
Dice d;
if (typeA) {
d.val = field[y+1][x+1];
d.top = field[y][x+1];
d.left = field[y][x];
d.right = field[y+1][x+2];
d.bottom = field[y+2][x+2];
d.back = field[y+2][x+3];
} else {
d.val = field[y+1][x];
d.top = field[y][x];
d.left = field[y+1][x-1];
d.right = field[y][x+1];
d.bottom = field[y+2][x-1];
d.back = field[y+2][x-2];
}
return d;
}
Dice parse() {
for (int i = 0; i < 5; ++i) {
cin >> field[i];
}
// Max horizontal row
int hmx = 0;
for (int i = 0; i < 5; ++i) {
int streak = 0;
for (int j = 0; j < 5; ++j) {
if (field[i][j] != '.') streak++;
else {
hmx = max(hmx, streak);
streak = 0;
}
}
hmx = max(hmx, streak);
}
int vmx = 0;
for (int i = 0; i < 5; ++i) {
int streak = 0;
for (int j = 0; j < 5; ++j) {
if (field[j][i] != '.') streak++;
else {
vmx = max(vmx, streak);
streak = 0;
}
}
vmx = max(vmx, streak);
}
if (hmx == 4) {
return ho4row();
} else if (vmx == 4) {
return ve4row();
} else if (hmx == 3) {
return ho3row();
} else if (vmx == 3) {
return ve3row();
}
return special();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<Dice> d;
for (int i = 0; i < n; ++i) {
d.push_back(rotaten(parse(), '1'));
//print(d[i]);
}
/*for (int i = 0; i < n; ++i) {
print(d[i]);
cout << endl;
}*/
for (int i = 0; i < n; ++i) {
vector<int> e;
for (int j = 0; j < n; ++j) {
if (j == i) continue;
if (eq(d[i], d[j])) {
e.push_back(j+1);
}
}
if (e.size() == 0) {
cout << "-" << endl;
} else {
for (int val : e) {
cout << val << " ";
}
cout << endl;
}
}
return 0;
}