#include<bits/stdc++.h>
using namespace std;
char s[10][10];
vector<vector<int>> vastaukset = {};
bool samat(vector<int> a, vector<int> b){
for(int i = 0; i < a.size(); i++){
if(a[i] != b[i]) return false;
}
return true;
}
/*bool tarkista(vector<int> a, vector<int> b){
int paallinen;
for(int i = 0; i < b.size(); i++){
if(b[i] == a[0]){
paallinen = i;
}
}
vector<int> b2 = {};
if(paallinen == 0){
b2 = b;
}if(paallinen == 1){
b2 = {b[1], b[5], b[2], b[3], b[0], b[4]};
}if(paallinen == 2){
b2 = {b[2], b[1], b[5], b[0], b[4], b[3]};
}if(paallinen == 3){
b2 = {b[3], b[1], b[0], b[5], b[4], b[2]};
}if(paallinen == 4){
b2 = {b[4], b[0], b[2], b[3], b[5], b[1]};
}if(paallinen == 5){
b2 = {b[5], b[1], b[2], b[3], b[4], b[0]};
}
if(samat(a,b2)) return 1;
vector<int> b3 = {b2[0], b2[3], b2[1], b2[4], b2[2], b2[5]};
if(samat(a,b3)) return 1;
vector<int> b4 = {b3[0], b3[3], b3[1], b3[4], b3[2], b3[5]};
if(samat(a,b4)) return 1;
vector<int> b5 = {b4[0], b4[3], b4[1], b4[4], b4[2], b4[5]};
if(samat(a, b5)) return 1;
return 0;
}*/
// int paalla = s[2][1]-'0';
// int edessa = s[1][1]-'0';
// int vasemmalla = s[0][0]-'0';
// int oikealla = s[0][2]-'0';
// int takana = s[3][1]-'0';
// int alhaalla = s[0][1]-'0';
vector<int> rotatex(vector<int> a){
vector<int> a2 = {a[0], a[3], a[1], a[4], a[2], a[5]};
return a2;
}
vector<int> rotatey(vector<int> a){
vector<int> v2 = {a[1], a[5], a[2], a[3], a[0], a[4]};
return v2;
}
bool tarkista(vector<int> a, vector<int> b){
for(int j = 0; j < 10; j++){
for(int i = 0; i < 10; i++){
if(samat(a, b)) return 1;
vector<int> a = rotatex(a);
}
vector<int> a = rotatey(a);
}
return 0;
}
int main(){
int n;
cin >> n;
for(int i = 0; i < n; i++){
for(int j = 0; j < 5; j++){
for(int k = 0; k < 5; k++){
cin >> s[j][k];
}
}
int paalla = s[2][1]-'0';
int edessa = s[1][1]-'0';
int vasemmalla = s[0][0]-'0';
int oikealla = s[0][2]-'0';
int takana = s[3][1]-'0';
int alhaalla = s[0][1]-'0';
for(int j = 0; j < 5; j++){
for(int k = 0; k < 5; k++){
// cout << s[j][k];
}
// cout << "\n";
}
// cout << "Paalla: " << paalla << "\n";
//cout << "Edessa: " << edessa << "\n";
//cout << "Vasemmalla: " << vasemmalla << "\n";
//cout << "Oikealla: " << oikealla << "\n";
//cout << "Takana: " << takana << "\n";
//cout << "Alhaalla: " << alhaalla << "\n";
vastaukset.push_back({paalla, edessa, vasemmalla, oikealla, takana, alhaalla});
//cout << "\n";
}
for(int i = 0; i < vastaukset.size(); i++){
bool bo = true;
for(int j = 0; j < vastaukset.size(); j++){
if(i != j && /*tarkista(vastaukset[i], vastaukset[j])*/){
cout << j << " ";
bo = false;
}
}
if(bo){
cout << "-";
}
cout << "\n";
}
}