Problem:
フロッピーキューブ
フロッピーキューブをプログラミングで解いてみましょう。フロッピーキューブは図のように表面に色のついた９個の立方体から構成されている立体パズルで、キューブの列を回転させることによって、６つの各面の色をそろえます。
フロッピーキューブに対しては下図のような４種類の操作を行うことができ、一回の操作で、端にある３つの隣接したキューブを１８０度回転することができます。わかりやすいように、図では、上面に＋(赤色)、下面に＊(緑色)、右前面に□(黄色)、左前面に●(青色)、右奥面に○(水色)、左奥面に■紫色) の記号が付いている状態を初期状態としています。
フロッピーキューブの初期状態が与えられるので、パズルを解くために必要な最小の操作回数を求めるプログラムを作成してください。
入力
入力は以下の形式で与えられる。
N
puzzle
1
puzzle
2
:
puzzle
N
１行目の
N
(1 ≤
N
≤ 30) は操作回数を計算したいパズルの数である。続くN行に各フロッピーキューブの初期状態
puzzle
i
が与えられる。
puzzle
i
は以下の形式で与えられる。
p
1
p
2
p
3
p
4
p
5
p
6
p
7
p
8
p
9
p
10
p
11
p
12
p
13
p
14
p
15
p
16
p
17
p
18
p
19
p
20
p
21
p
22
p
23
p
24
p
25
p
26
p
27
p
28
p
29
p
30
各フロッピーキューブの情報は 30 個の整数
p
i
(1 ≤
p
i
≤ 6) からなる。
p
i
は、下図のようにフロッピーキューブの各面に番号
i
を振ったときの、そのキューブの面の色を表す。
パズルは、多くとも８回の操作で解くことができると仮定してよい。
出力
パズルごとに、最小の操作回数を１行に出力する。
入出力例
入力例
4
1 1 1 1 1 1 1 1 1 2 2 2 4 4 4 6 6 6 5 5 5 3 3 3 3 3 3 3 3 3
3 3 3 1 1 1 1 1 1 2 2 2 4 4 6 4 6 6 5 5 5 3 3 3 3 3 3 1 1 1
3 3 3 1 1 3 1 1 1 2 2 5 6 4 4 4 6 6 2 5 5 3 3 3 1 3 3 1 1 1
1 3 1 3 1 3 3 1 3 2 2 2 6 4 4 6 6 4 5 5 5 1 3 1 1 3 1 3 1 3
出力例
0
1
2
7

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
// #include<atcoder/all>
// using mint = atcoder::modint998244353;
using ld = long double;
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(int)(n);++i)
template<typename T>bool chmin(T&a,T b){return b<a?(a=b,1):0;}
template<typename T>bool chmax(T&a,T b){return b>a?(a=b,1):0;}

void dfs(vector<int> A, int depth, int &ans){
    bool b=1;
    rep(i,30-1){
        if(i==8||i==11||i==14||i==17||i==20) continue;
        if(A[i]!=A[i+1]) b=0;
    }
    if(b){
        chmin(ans, depth);
        return ;
    }
    if(depth==8) return ;

    {
        vector<int> B=A;
        rep(i,3) swap(B[i], B[i+27]);
        swap(B[14],B[15]);
        swap(B[18],B[20]);
        dfs(B, depth+1, ans);
    }
    {
        vector<int> B=A;
        for(int i=2;i<=8;i+=3) swap(B[i], B[i+19]);
        swap(B[11], B[18]);
        swap(B[12], B[14]);
        dfs(B, depth+1, ans);
    }
    {
        vector<int> B=A;
        for(int i=6;i<=8;i++) swap(B[i], B[i+15]);
        swap(B[12],B[17]);
        swap(B[9],B[11]);
        dfs(B, depth+1, ans);
    }
    {
        vector<int> B=A;
        for(int i=0;i<=6;i+=3) swap(B[i],B[i+23]);
        swap(B[9],B[20]);
        swap(B[15],B[17]);
        dfs(B, depth+1, ans);
    }
    return;
}


int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int T;
    cin>>T;
    while(T--){
        int N=30;
        vector<int> A(N);
        rep(i,N)cin>>A[i];
        int ans=1e9;
        dfs(A, 0, ans);
        cout<<ans<<"\n";
    }
}
accuracy: 1
time_ms: 90
memory_kb: 3584
final_rank: 12

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,N) for(ll i=0;i<N;i++)
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

bool check(vector<int>&S){
    rep(i,30){
        if(i<9){
            if(S[0]!=S[i])return false;
        }
        else if(i<12){
            if(S[9]!=S[i])return false;
        }
        else if(i<15){
            if(S[12]!=S[i])return false;
        }
        else if(i<18){
            if(S[15]!=S[i])return false;
        }
        else if(i<21){
            if(S[18]!=S[i])return false;
        }
        else {
            if(S[21]!=S[i])return false;
        }
    }
    return true;
}

void solve(){
    vector<int>S(30);
    rep(i,30)cin>>S[i];
    if(check(S)){
        cout<<0<<endl;
        return;
    }
    queue<pair<int,vector<int>>>Q;
    Q.push({0,S});
    while(!Q.empty()){
        vector<int>s = Q.front().se;
        int d = Q.front().fi;
        Q.pop();
        vector<int>n = s;
        n[2]=s[21],n[5]=s[24],n[8]=s[27],n[18]=s[11],n[11]=s[18],n[14]=s[12],n[12]=s[14];
        n[21]=s[2],n[24]=s[5],n[8]=s[27];
        if(check(n)){
            cout<<d+1<<endl;
            Q={};
            return;
        }
        Q.push({d+1,n});
        n=s;
        n[0]=s[23],n[3]=s[26],n[6]=s[29],n[15]=s[17],n[9]=s[20];
        n[23]=s[0],n[26]=s[3],n[29]=s[6],n[17]=s[15],n[20]=s[9];
        if(check(n)){
            cout<<d+1<<endl;
            Q={};
            return;
        }
        Q.push({d+1,n});
        n=s;
        n[0]=s[27],n[1]=s[28],n[2]=s[29],n[15]=s[14],n[20]=s[18];
        n[27]=s[0],n[28]=s[1],n[29]=s[2],n[14]=s[15],n[18]=s[20];
        if(check(n)){
            cout<<d+1<<endl;
            Q={};
            return;
        }
        Q.push({d+1,n});
        n=s;
        n[6]=s[21],n[7]=s[22],n[8]=s[23],n[9]=s[11],n[17]=s[12];
        n[21]=s[6],n[22]=s[7],n[23]=s[8],n[11]=s[9],n[12]=s[17];
        if(check(n)){
            cout<<d+1<<endl;
            Q={};
            return;
        }
        Q.push({d+1,n});
    }
}

int main(){
    int N;cin>>N;
    rep(i,N)solve();
}
accuracy: 0.125
time_ms: 90
memory_kb: 56376
final_rank: 20

code_snippet: #include<bits/stdc++.h>
using namespace std;
void func(int);
int check(void);
int a[31],mn;
int main(void){
	int n,i,j,nn,flg;
	cin>>n;
	for(i=0;i<n;i++){
		for(j=1;j<=30;j++) cin>>a[j];
		mn=8;
		func(0);
		cout<<mn<<endl;
	}
	return 0;
}
void func(int nn){
	int i,j,flg;
	int pat[4][10]={{1,24,4,27,7,30,10,21,16,18},{1,28,2,29,3,30,15,16,19,21},{3,22,6,25,9,28,12,19,13,15},{7,22,8,23,9,24,13,18,10,12}};
	int b[31];
	if(nn<mn){
		flg=check();
		if(flg==1){
			mn=nn;
		}
		else{
			for(i=0;i<4;i++){
				for(j=0;j<10;j+=2){
					swap(a[pat[i][j]],a[pat[i][j+1]]);
				}
				func(nn+1);
				for(j=0;j<10;j+=2){
					swap(a[pat[i][j]],a[pat[i][j+1]]);
				}
			}
		}
	}
}
int check(void){
	int i,j,flg;
	int f;
	flg=1;
	for(i=2;i<=9;i++){
		if(a[1]!=a[i]){
			flg=0;
			break;
		}
	}
	for(i=11;i<=12;i++){
		if(a[10]!=a[i]){
			flg=0;
			break;
		}
	}
	for(i=14;i<=15;i++){
		if(a[13]!=a[i]){
			flg=0;
			break;
		}
	}
	for(i=17;i<=18;i++){
		if(a[16]!=a[i]){
			flg=0;
			break;
		}
	}
	for(i=20;i<=21;i++){
		if(a[19]!=a[i]){
			flg=0;
			break;
		}
	}
	for(i=23;i<=30;i++){
		if(a[22]!=a[i]){
			flg=0;
			break;
		}
	}
	return flg;
}
accuracy: 1
time_ms: 10
memory_kb: 3116
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

int p[31];

void rotate_front() {
  swap(p[18], p[13]);
  swap(p[10], p[12]);
  swap(p[7], p[22]);
  swap(p[8], p[23]);
  swap(p[9], p[24]);
}

void rotate_back() {
  swap(p[16], p[15]);
  swap(p[21], p[19]);
  swap(p[1], p[28]);
  swap(p[2], p[29]);
  swap(p[3], p[30]);
}

void rotate_right() {
  swap(p[19], p[12]);
  swap(p[15], p[13]);
  swap(p[3], p[22]);
  swap(p[6], p[25]);
  swap(p[9], p[28]);
}

void rotate_left() {
  swap(p[21], p[10]);
  swap(p[16], p[18]);
  swap(p[1], p[24]);
  swap(p[4], p[27]);
  swap(p[7], p[30]);
}

bool is_solved() {
  for(int i = 1; i <= 28; i+=3) {
    if(p[i] != p[i+1] || p[i] != p[i+2]) {
      return false;
    }
  }
  return p[3] == p[4] && p[6] == p[7] && p[24] == p[25] && p[27] == p[28];
}

void solve() {
  for(int i = 1; i <= 30; i++) {
    cin >> p[i];
  }
  int ans = 8;
  auto dfs = [&](auto dfs, int cnt) -> void {
    if(cnt == 8 || is_solved()) {
      ans = min(ans, cnt);
      return;
    }
    rotate_front(); dfs(dfs, cnt+1); rotate_front();
    rotate_back(); dfs(dfs, cnt+1); rotate_back();
    rotate_right(); dfs(dfs, cnt+1); rotate_right();
    rotate_left(); dfs(dfs, cnt+1); rotate_left();
  };
  dfs(dfs, 0);
  cout << ans << endl;
}

int main() {
  int N;
  cin >> N;
  while(N--) { solve(); }
}
accuracy: 1
time_ms: 10
memory_kb: 3440
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;
int p[31],mn;
void func(int);
int main(void)
{
    int n,q;
    cin>>n;
    for(int i=0;i<n;i++){
        mn=9;
        for(int j=0;j<30;j++){cin>>q; p[j]=q;}
        func(0);
        cout<<mn<<endl;
    }
    return 0;
}
void func(int c)
{
    int nx[4][10]={{1,24,4,27,7,30,10,21,16,18},{7,22,8,23,9,24,13,18,10,12},
                    {9,28,6,25,3,22,12,19,13,15},{3,30,2,29,1,28,15,16,19,21}};
    int f=1;
    for(int i=1;i<9;i++) if(p[i]!=p[0]) f=0;
    for(int i=10;i<12;i++) if(p[i]!=p[9]) f=0;
    for(int i=13;i<15;i++) if(p[i]!=p[12]) f=0;
    for(int i=16;i<18;i++) if(p[i]!=p[15]) f=0;
    for(int i=19;i<21;i++) if(p[i]!=p[18]) f=0;
    for(int i=22;i<30;i++) if(p[i]!=p[21]) f=0;
    if(f){mn=min(mn,c);}
    else if(c<mn){
        for(int i=0;i<4;i++){
            for(int j=0;j<10;j+=2) swap(p[nx[i][j]-1],p[nx[i][j+1]-1]);
            func(c+1);
            for(int j=0;j<10;j+=2) swap(p[nx[i][j]-1],p[nx[i][j+1]-1]);
        }
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3088
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;
int match(vector<int> a){
    for(int i=0;i<9;i+=1){
        if(a.at(i)!=1){
            return 0;
        }}
        for(int i=0;i<9;i+=1){
        if(a.at(i+9)!=2*(i/3+1)){
            return 0;
        }
        }
        for(int i=0;i<3;i+=1){
            if(a.at(i+18)!=5){
                return 0;
            }
        }
          for(int i=0;i<9;i+=1){
        if(a.at(i+21)!=3){
            return 0;
        }}
        return 1;
}
int swap(int &a,int &b){
    int u=0;
    u=a;
    a=b;
    b=u;
    return 0;
}
int main(){
    int a;
    int ans=8;
cin>>a;
vector<int> inform(30);
vector<int> inform2(30);
for(int ttt=0;ttt<a;ttt+=1){
    ans=8;
    for(int i=0;i<30;i+=1){
        cin>>inform.at(i);
        inform2.at(i)=inform.at(i);
    }
    for(int i=0;i<65536;i+=1){
        int k=0;
        int j=1,j2=-1;
        for(k=0;match(inform2)==0&&k<=8;k+=1,j*=4){
            if(j2!=(i/j)%4){
            switch((i/j)%4){
                case 0:
                swap(inform2.at(0),inform2.at(27));
                swap(inform2.at(1),inform2.at(28));
                swap(inform2.at(2),inform2.at(29));
                swap(inform2.at(14),inform2.at(15));
                swap(inform2.at(18),inform2.at(20));
                break;
                case 1:
                swap(inform2.at(0),inform2.at(23));
                swap(inform2.at(3),inform2.at(26));
                swap(inform2.at(6),inform2.at(29));
                swap(inform2.at(9),inform2.at(20));
                swap(inform2.at(15),inform2.at(17));
                break;
                case 2:
                swap(inform2.at(6),inform2.at(21));
                swap(inform2.at(7),inform2.at(22));
                swap(inform2.at(8),inform2.at(23));
                swap(inform2.at(17),inform2.at(12));
                swap(inform2.at(9),inform2.at(11));
                break;
                case 3:
                swap(inform2.at(2),inform2.at(21));
                swap(inform2.at(5),inform2.at(24));
                swap(inform2.at(8),inform2.at(27));
                swap(inform2.at(18),inform2.at(11));
                swap(inform2.at(12),inform2.at(14));
            }
            }else{
                k=8;
                break;
            }
            j2=i/j%4;
        }
            for(int i=0;i<30;i+=1){
        inform2.at(i)=inform.at(i);
    }
        ans=min(ans,k);
    }
    cout<<ans<<endl;
}
}
accuracy: 1
time_ms: 370
memory_kb: 3104
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
vector<vector<int>> op = {{0, 27, 1, 28, 2, 29, 14, 15, 18, 20}, {6, 21, 7, 22, 8, 23, 12, 17, 9, 11}, {0, 23, 3, 26, 6, 29, 9, 20, 15, 17}, {2, 21, 5, 24, 8, 27, 11, 18, 12, 14}};
vector<int> f = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5};
int main(){
  int N;
  cin >> N;
  for (int i = 0; i < N; i++){
    vector<int> p(30);
    for (int j = 0; j < 30; j++){
      cin >> p[j];
    }
    int ans = -1;
    for (int j = 0; j <= 8; j++){
      bool ok = false;
      for (int k = 0; k < (1 << (j * 2)); k++){
        vector<int> p2 = p;
        for (int l = 0; l < j; l++){
          int x = (k >> (l * 2)) & 3;
          for (int m = 0; m < 5; m++){
            swap(p2[op[x][m * 2]], p2[op[x][m * 2 + 1]]);
          }
        }
        vector<int> c(6, -1);
        bool ok2 = true;
        for (int l = 0; l < 30; l++){
          if (c[f[l]] == -1){
            c[f[l]] = p2[l];
          } else if (c[f[l]] != p2[l]){
            ok2 = false;
          }
        }
        if (ok2){
          ok = true;
        }
      }
      if (ok){
        ans = j;
        break;
      }
    }
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 20
memory_kb: 3352
final_rank: 8

code_snippet: #include <iostream>
using namespace std;
#define INF 1000010

void swap(int i, int j, int* p){
    int tmp;
    tmp = p[i];
    p[i] = p[j];
    p[j] = tmp;
}

void copy(int* array1, int* array2, int N){
    for(int i=0;i<N;i++){
        array2[i] = array1[i];
    }
}

bool equal(int* array1, int* array2, int N){
    for(int i=0;i<N;i++){
        if(array1[i]!=array2[i]){
            return false;
        }
    }
    return true;
}

void op1(int* puzzle){
    swap(7,22,puzzle);
    swap(8,23,puzzle);
    swap(9,24,puzzle);
    swap(13,18,puzzle);
    swap(10,12,puzzle);
}

void op2(int* puzzle){
    swap(1,28,puzzle);
    swap(2,29,puzzle);
    swap(3,30,puzzle);
    swap(15,16,puzzle);
    swap(19,21,puzzle);
}

void op3(int* puzzle){
    swap(1,24,puzzle);
    swap(4,27,puzzle);
    swap(7,30,puzzle);
    swap(10,21,puzzle);
    swap(16,18,puzzle);
}

void op4(int* puzzle){
    swap(3,22,puzzle);
    swap(6,25,puzzle);
    swap(9,28,puzzle);
    swap(12,19,puzzle);
    swap(13,15,puzzle);
}

int solve(int *puzzle, int depth, int N, int op){
    if(depth>8){
        return INF;
    }
    int goal[] = {0,1,1,1,1,1,1,1,1,1,2,2,2,4,4,4,6,6,6,5,5,5,3,3,3,3,3,3,3,3,3};
    if(equal(goal, puzzle, N)){
        return 0;
    }
    int retval = INF+100;
    int* puzzle_ = new int[N];
    if(op!=1){
        copy(puzzle, puzzle_,N);
        op1(puzzle_);
        retval = min(retval, 1+solve(puzzle_, depth+1, N, 1));
    }

    if(op!=2){
        copy(puzzle, puzzle_,N);
        op2(puzzle_);
        retval = min(retval, 1+solve(puzzle_, depth+1, N, 2));
    }

    if(op!=3){
        copy(puzzle, puzzle_,N);
        op3(puzzle_);
        retval = min(retval, 1+solve(puzzle_, depth+1, N, 3));
    }

    if(op!=4){
        copy(puzzle, puzzle_,N);
        op4(puzzle_);
        retval = min(retval, 1+solve(puzzle_, depth+1, N, 4));
    }



    return retval;
}


int main(void){
    int N;
    cin>>N;
    for(int i=0;i<N;i++){
        int* puzzle = new int[31];
        puzzle[0]=0;
        for(int j=1;j<=30;j++){
            cin>>puzzle[j];
        }
        cout<<solve(puzzle,0,31,-1)<<endl;
    }
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 57556
final_rank: 17

code_snippet: #include <iostream>
using namespace std;
#define INF 1000010

void swap(int i, int j, int* p){
    int tmp;
    tmp = p[i];
    p[i] = p[j];
    p[j] = tmp;
}

void copy(int* array1, int* array2, int N){
    for(int i=0;i<N;i++){
        array2[i] = array1[i];
    }
}

bool equal(int* array1, int* array2, int N){
    for(int i=0;i<N;i++){
        if(array1[i]!=array2[i]){
            return false;
        }
    }
    return true;
}

void op1(int* puzzle){
    swap(7,22,puzzle);
    swap(8,23,puzzle);
    swap(9,24,puzzle);
    swap(13,18,puzzle);
    swap(10,12,puzzle);
}

void op2(int* puzzle){
    swap(1,28,puzzle);
    swap(2,29,puzzle);
    swap(3,30,puzzle);
    swap(15,16,puzzle);
    swap(19,21,puzzle);
}

void op3(int* puzzle){
    swap(1,24,puzzle);
    swap(4,27,puzzle);
    swap(7,30,puzzle);
    swap(10,21,puzzle);
    swap(16,18,puzzle);
}

void op4(int* puzzle){
    swap(3,22,puzzle);
    swap(6,25,puzzle);
    swap(9,28,puzzle);
    swap(12,19,puzzle);
    swap(13,15,puzzle);
}

int solve(int *puzzle, int depth, int N, int op){
    if(depth>=8){
        return INF;
    }
    int goal[] = {0,1,1,1,1,1,1,1,1,1,2,2,2,4,4,4,6,6,6,5,5,5,3,3,3,3,3,3,3,3,3};
    if(equal(goal, puzzle, N)){
        return 0;
    }
    int retval = INF+100;
    int* puzzle_ = new int[N];
    if(op!=1){
        copy(puzzle, puzzle_,N);
        op1(puzzle_);
        retval = min(retval, 1+solve(puzzle_, depth+1, N, 1));
    }

    if(op!=2){
        copy(puzzle, puzzle_,N);
        op2(puzzle_);
        retval = min(retval, 1+solve(puzzle_, depth+1, N, 2));
    }

    if(op!=3){
        copy(puzzle, puzzle_,N);
        op3(puzzle_);
        retval = min(retval, 1+solve(puzzle_, depth+1, N, 3));
    }

    if(op!=4){
        copy(puzzle, puzzle_,N);
        op4(puzzle_);
        retval = min(retval, 1+solve(puzzle_, depth+1, N, 4));
    }



    return retval;
}


int main(void){
    int N;
    cin>>N;
    for(int i=0;i<N;i++){
        int* puzzle = new int[31];
        puzzle[0]=0;
        for(int j=1;j<=30;j++){
            cin>>puzzle[j];
        }
        cout<<solve(puzzle,0,31,-1)<<endl;
    }
    return 0;
}
accuracy: 0.875
time_ms: 10
memory_kb: 21388
final_rank: 18

code_snippet: #include<bits/stdc++.h>
using namespace std;
 
int main(){
  int N;
  cin >> N;
  for(int i=0;i<N;i++){
    vector<int> p(30);
    for(int j=0;j<30;j++) cin >> p[j];
    queue<pair<vector<int>,int>> que;
    que.push(make_pair(p,0));
    int ans = 8;
    while(!que.empty()){
      vector<int> now = que.front().first;
      int time = que.front().second;
      que.pop();
      if(time >= ans) continue;
      bool flag = true;
      for(int j=1;j<9;j++) if(now[0] != now[j]) flag = false;
      for(int j=10;j<12;j++) if(now[9] != now[j]) flag = false;
      for(int j=13;j<15;j++) if(now[12] != now[j]) flag = false;
      for(int j=16;j<18;j++) if(now[15] != now[j]) flag = false;
      for(int j=19;j<21;j++) if(now[18] != now[j]) flag = false;
      for(int j=22;j<30;j++) if(now[21] != now[j]) flag = false;
      if(flag){
        ans = min(ans,time);
        continue;
      }
      vector<int> NEW = now;
      swap(NEW[6],NEW[21]);
      swap(NEW[7],NEW[22]);
      swap(NEW[8],NEW[23]);
      swap(NEW[12],NEW[17]);
      swap(NEW[9],NEW[11]);
      que.push(make_pair(NEW,time+1));
      NEW = now;
      swap(NEW[0],NEW[27]);
      swap(NEW[1],NEW[28]);
      swap(NEW[2],NEW[29]);
      swap(NEW[14],NEW[15]);
      swap(NEW[18],NEW[20]);
      que.push(make_pair(NEW,time+1));
      NEW  = now;
      swap(NEW[0],NEW[23]);
      swap(NEW[3],NEW[26]);
      swap(NEW[6],NEW[29]);
      swap(NEW[9],NEW[20]);
      swap(NEW[15],NEW[17]);
      que.push(make_pair(NEW,time+1));
      NEW = now;
      swap(NEW[2],NEW[21]);
      swap(NEW[5],NEW[24]);
      swap(NEW[8],NEW[27]);
      swap(NEW[11],NEW[18]);
      swap(NEW[12],NEW[14]);
      que.push(make_pair(NEW,time+1));
    }
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 20
memory_kb: 13828
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
int n,p[31],mn;
void func(int);
int main(void)
{
	int i,j;
	scanf("%d",&n);
	for(i=0;i<n;i++){
		for(j=1;j<31;j++) scanf("%d",&p[j]);
		mn=8;
		func(0);
		printf("%d\n",mn);
	}
	return 0;
}
void func(int nn)
{
	int pp[4][10]={{1,24,4,27,7,30,10,21,16,18},{1,28,2,29,3,30,15,16,19,21},{3,22,6,25,9,28,12,19,13,15},{7,22,8,23,9,24,13,18,10,12}};
	int flg,i,j;
	if(mn>nn){
		flg=1;
		for(i=1;i<31;i++){
			if(i<10) {
				if(p[1]!=p[i]) flg=0;
			}
			else if(i<13) {
				if(p[10]!=p[i]) flg=0;
			}
			else if(i<16) {
				if(p[13]!=p[i]) flg=0;
			}
			else if(i<19) {
				if(p[16]!=p[i]) flg=0;
			}
			else if(i<22) {
				if(p[19]!=p[i]) flg=0;
			}
			else {
				if(p[22]!=p[i]) flg=0;
			}
		}
		if(flg==1) mn=nn;
		else{
			for(i=0;i<4;i++){
				for(j=0;j<10;j+=2){
					swap(p[pp[i][j]],p[pp[i][j+1]]);
				}
				func(nn+1);
				for(j=0;j<10;j+=2){
					swap(p[pp[i][j]],p[pp[i][j+1]]);
				}
			}
		}
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3236
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
int n,p[31],mn;
void func(int);
int main(void)
{
	int i,j;
	scanf("%d",&n);
	for(i=0;i<n;i++){
		for(j=1;j<=30;j++) scanf("%d",&p[j]);
		mn=8;
		func(0);
		printf("%d\n",mn);
	}
	return 0;
}
void func(int nn)
{
	int pp[4][10]={{1,24,4,27,7,30,10,21,16,18},{1,28,2,29,3,30,15,16,19,21},{3,22,6,25,9,28,12,19,13,15},{7,22,8,23,9,24,13,18,10,12}};
	int flg,i,j;
	if(nn<mn){
		flg=1;
		for(i=2;i<10;i++){
			if(p[1]!=p[i]) flg=0;
		}
		for(i=11;i<13;i++){
			if(p[10]!=p[i]) flg=0;
		}
		for(i=14;i<16;i++){
			if(p[13]!=p[i]) flg=0;
		}
		for(i=17;i<19;i++){
			if(p[16]!=p[i]) flg=0;
		}
		for(i=20;i<22;i++){
			if(p[19]!=p[i]) flg=0;
		}
		for(i=23;i<31;i++){
			if(p[22]!=p[i]) flg=0;
		}
		if(flg==1) mn=nn;
		else{
			for(i=0;i<4;i++){
				for(j=0;j<10;j+=2){
					swap(p[pp[i][j]],p[pp[i][j+1]]);
				}
				func(nn+1);
				for(j=0;j<10;j+=2){
					swap(p[pp[i][j]],p[pp[i][j+1]]);
				}
			}
		}
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3216
final_rank: 2

code_snippet: #include <bits/stdc++.h>
 
#define lol long long
#define gcd(x, y) __gcd(x, y)
#define mt make_tuple
#define mp make_pair
#define fi first
#define se second
#define fixed(x) fixed << setprecision(x)
#define all(x) x.begin(),x.end()
using namespace std;
using pii = pair<int, int>;
template <class A, class B>
inline bool chmax(A& a, const B& b) {
  return b > a && (a = b, true);
}
template <class A, class B>
inline bool chmin(A& a, const B& b) {
  return b < a && (a = b, true);
}
template <class A>
inline A abs(A& a) {
  return (a < 0 ? -a : a);
}
bool inLine(int x, int y, int mx, int my) {
  return (x >= 0 && y >= 0 && x < mx && y < my);
}
template<class T> using min_heap=priority_queue<T,vector<T>,greater<T>>;
template<class T> using uset=unordered_set<T>;
template<class A,class B> using umap=unordered_map<A,B>;
const lol inf = (1LL << 62);
const int MOD = (1e9) + 7;
const int mod = 998244353;
const int dx[] = {1,  0,  -1, 0, 1, 1, -1, -1, -2, -2, -2, -2,
                  -2, -1, -1, 1, 1, 0, 0,  2,  2,  2,  2,  2};
const int dy[] = {0, 1,  0, -1, 1, -1, 1, -1, -2, -1, 0, 1,
                  2, -2, 2, -2, 2, -2, 2, -2, -1, 0,  1, 2};
int co(vector<int> p,int cost){
  int tp=0;
  for(int i=2;i<=9;i++){
    tp+=(p[1]!=p[i]);
  }
  for(int i=22;i<=30;i++){
    tp+=(p[22]!=p[i]);
  }
  for(int i=10;i<=12;i++){
    tp+=(p[10]!=p[i]);
  }
  for(int i=13;i<=15;i++){
    tp+=(p[13]!=p[i]);
  }
  for(int i=16;i<=18;i++){
    tp+=(p[16]!=p[i]);
  }
  for(int i=19;i<=21;i++){
    tp+=(p[19]!=p[i]);
  }
  if(tp==0){
    return (cost);
  }
  if(cost>=9){
    return (mod);
  }
  int ans=mod;
  auto v=p;
  swap(v[1],v[24]);
  swap(v[4],v[27]);
  swap(v[7],v[30]);
  swap(v[21],v[10]);
  swap(v[16],v[18]);
  chmin(ans,co(v,cost+1));
   
  v=p;
  swap(v[3],v[22]);
  swap(v[6],v[25]);
  swap(v[9],v[28]);
  swap(v[12],v[19]);
  swap(v[13],v[15]);
  chmin(ans,co(v,cost+1));
 
  v=p;
  swap(v[1],v[28]);
  swap(v[2],v[29]);
  swap(v[3],v[30]);
  swap(v[15],v[16]);
  swap(v[19],v[21]);
  chmin(ans,co(v,cost+1));
 
  v=p;
  swap(v[7],v[22]);
  swap(v[8],v[23]);
  swap(v[9],v[24]);
  swap(v[10],v[12]);
  swap(v[13],v[18]);
  chmin(ans,co(v,cost+1));
  return(ans);
   
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >>n;
  for(int i=0;i<n;i++){
    vector<int> p(31);
    for(int j=0;j<30;j++){
      cin >>p[j+1];
    }
    cout <<co(p,0)<<'\n';
  }
  return (0);
}
accuracy: 1
time_ms: 330
memory_kb: 3392
final_rank: 15

code_snippet: /*
このコード、と～おれ!
Be accepted!
∧＿∧　
（｡･ω･｡)つ━☆・*。
⊂　　 ノ 　　　・゜+.
　しーＪ　　　°。+ *´¨)
 　　　　　　　　　.· ´¸.·*´¨) ¸.·*¨)
		  　　　　　　　　　　(¸.·´ (¸.·'* ☆
*/

#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <vector>
#include <numeric>
#include <iostream>
#include <random>
#include <map>
#include <unordered_map>
#include <queue>
#include <regex>
#include <functional>
#include <complex>
#include <list>
#include <cassert>
#include <iomanip>
#include <set>
#include <stack>
#include <bitset>
#include <array>
#include <chrono>

//#pragma GCC target("arch=skylake-avx512")
#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse4")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define repeat(i, n, m) for(int i = n; i < (m); ++i)
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define printynl(a) printf(a ? "yes\n" : "no\n")
#define printyn(a) printf(a ? "Yes\n" : "No\n")
#define printYN(a) printf(a ? "YES\n" : "NO\n")
#define printim(a) printf(a ? "possible\n" : "imposible\n")
#define printdb(a) printf("%.50lf\n", a)
#define printLdb(a) printf("%.50Lf\n", a)
#define printdbd(a) printf("%.16lf\n", a)
#define prints(s) printf("%s\n", s.c_str())
#define all(x) (x).begin(), (x).end()
#define deg_to_rad(deg) (((deg)/360.0L)*2.0L*PI)
#define rad_to_deg(rad) (((rad)/2.0L/PI)*360.0L)
#define Please return
#define AC 0
#define manhattan_dist(a, b, c, d) (abs(a - c) + abs(b - d))

using ll = long long;
using ull = unsigned long long;

constexpr int INF = 1073741823;
constexpr int MINF = -1073741823;
constexpr ll LINF = ll(4661686018427387903);
constexpr ll MOD = 1e9 + 7;
constexpr ll mod = 998244353;
constexpr long double eps = 1e-6;
const long double PI = acosl(-1.0L);

using namespace std;

void scans(string& str) {
	char c;
	str = "";
	scanf("%c", &c);
	if (c == '\n')scanf("%c", &c);
	while (c != '\n' && c != -1 && c != ' ') {
		str += c;
		scanf("%c", &c);
	}
}

void scanc(char& str) {
	char c;
	scanf("%c", &c);
	if (c == -1)return;
	while (c == '\n') {
		scanf("%c", &c);
	}
	str = c;
}

double acot(double x) {
	return PI / 2 - atan(x);
}

ll LSB(ll n) { return (n & (-n)); }

template<typename T>
inline T chmin(T& a, const T& b) {
	if (a > b)a = b;
	return a;
}

template<typename T>
inline T chmax(T& a, const T& b) {
	if (a < b)a = b;
	return a;
}

//cpp_int
#if __has_include(<boost/multiprecision/cpp_int.hpp>)
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using namespace boost::multiprecision;
#else
using cpp_int = ll;
#endif

//atcoder library
#if __has_include(<atcoder/all>)
#include <atcoder/all>
//using namespace atcoder;
#endif

/*
	random_device seed_gen;
	mt19937 engine(seed_gen());
	uniform_int_distribution dist(1, 100);
*/


/*----------------------------------------------------------------------------------*/

bool check(int p[30]) {
	bool ret = true;
	int q = p[0];
	rep(i, 9) {
		if (p[i] != q)ret = false;
	}
	if (not(p[9] == p[10] and p[10] == p[11]))ret = false;
	if (not(p[12] == p[13] and p[13] == p[14]))ret = false;
	if (not(p[15] == p[16] and p[16] == p[17]))ret = false;
	if (not(p[18] == p[19] and p[19] == p[20]))ret = false;
	q = p[21];
	rep(i, 9) {
		if (p[21 + i] != q)ret = false;
	}
	return ret;
}

int solve(int p[30], int cnt) {
	if (cnt == 8)return cnt;
	int ret = INF;
	if (check(p))ret = cnt;
	++cnt;
	swap(p[6], p[21]);
	swap(p[7], p[22]);
	swap(p[8], p[23]);
	swap(p[12], p[17]);
	swap(p[9], p[11]);
	chmin(ret, solve(p, cnt));
	swap(p[6], p[21]);
	swap(p[7], p[22]);
	swap(p[8], p[23]);
	swap(p[12], p[17]);
	swap(p[9], p[11]);

	swap(p[0], p[27]);
	swap(p[1], p[28]);
	swap(p[2], p[29]);
	swap(p[14], p[15]);
	swap(p[20], p[18]);
	chmin(ret, solve(p, cnt));
	swap(p[0], p[27]);
	swap(p[1], p[28]);
	swap(p[2], p[29]);
	swap(p[14], p[15]);
	swap(p[20], p[18]);

	swap(p[0], p[23]);
	swap(p[3], p[26]);
	swap(p[6], p[29]);
	swap(p[20], p[9]);
	swap(p[15], p[17]);
	chmin(ret, solve(p, cnt));
	swap(p[0], p[23]);
	swap(p[3], p[26]);
	swap(p[6], p[29]);
	swap(p[20], p[9]);
	swap(p[15], p[17]);

	swap(p[2], p[21]);
	swap(p[5], p[24]);
	swap(p[8], p[27]);
	swap(p[18], p[11]);
	swap(p[14], p[12]);
	chmin(ret, solve(p, cnt));
	swap(p[2], p[21]);
	swap(p[5], p[24]);
	swap(p[8], p[27]);
	swap(p[18], p[11]);
	swap(p[14], p[12]);

	return ret;
}

int main() {

	int n;
	scanf("%d", &n);
	while (n--) {
		int p[30];
		rep(i, 30)scanf("%d", &p[i]);
		printf("%d\n", solve(p, 0));
	}

	Please AC;
}
accuracy: 1
time_ms: 10
memory_kb: 3548
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll,ll>;
using pi = pair<int,int>;
#define all(x) x.begin(),x.end()
#define rep(i,j,n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const int INF = ((1<<30)-1);
const ll LINF = (1LL<<60);
const double PI = 3.141592653589793238;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//(a+b-1)/b

int solve(vi p,int last = -1,int cnt = 0){
	if(cnt > 8)return 10;
	bool ok = 1;
	rep(i,1,9)if(p[i] != p[i-1]){
		ok = 0;
	}
	rep(i,22,30)if(p[i] != p[i-1])ok = 0;
	if(!(p[15] == p[16] && p[16] == p[17]))ok = 0;
	if(!(p[18] == p[19] && p[19] == p[20]))ok = 0;
	if(!(p[9] == p[10] && p[10] == p[11]))ok = 0;
	if(!(p[12] == p[13] && p[13] == p[14]))ok = 0;
	if(ok)return 0;
	int ans = 10;
	if(last != 1){
		auto p1 = p;
		swap(p1[12],p1[17]);
		swap(p1[9],p1[11]);
		rep(i,6,9)swap(p1[i],p1[i+15]);
		chmin(ans,solve(p1,1,cnt+1)+1);
	}
	if(last != 2){
		auto p1 = p;
		swap(p1[14],p1[15]);
		swap(p1[20],p1[18]);
		rep(i,0,3)swap(p1[i],p1[i+27]);
		chmin(ans,solve(p1,2,cnt+1)+1);
	}
	if(last != 3){
		auto p1 = p;
		swap(p1[9],p1[20]);
		swap(p1[15],p1[17]);
		swap(p1[0],p1[23]);
		swap(p1[3],p1[26]);
		swap(p1[6],p1[29]);
		chmin(ans,solve(p1,3,cnt+1)+1);

	}
	if(last != 3){
		auto p1 = p;
		swap(p1[11],p1[18]);
		swap(p1[12],p1[14]);
		swap(p1[2],p1[21]);
		swap(p1[5],p1[24]);
		swap(p1[8],p1[27]);
		chmin(ans,solve(p1,4,cnt+1)+1);
	}
	return ans;
}

signed main(){
	//cout << fixed << setprecision(10);
	int n; cin >> n;
	rep(i,0,n){
		vi p2(30);
		rep(j,0,30)cin >> p2[j];
		cout << solve(p2) << endl;
	}


	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3372
final_rank: 10

code_snippet: /*
このコード、と～おれ!
Be accepted!
∧＿∧　
（｡･ω･｡)つ━☆・*。
⊂　　 ノ 　　　・゜+.
　しーＪ　　　°。+ *´¨)
 　　　　　　　　　.· ´¸.·*´¨) ¸.·*¨)
		  　　　　　　　　　　(¸.·´ (¸.·'* ☆
*/

#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <vector>
#include <numeric>
#include <iostream>
#include <random>
#include <map>
#include <unordered_map>
#include <queue>
#include <regex>
#include <functional>
#include <complex>
#include <list>
#include <cassert>
#include <iomanip>
#include <set>
#include <stack>
#include <bitset>
#include <array>
#include <chrono>

//#pragma GCC target("arch=skylake-avx512")
#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse4")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define repeat(i, n, m) for(int i = n; i < (m); ++i)
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define printynl(a) printf(a ? "yes\n" : "no\n")
#define printyn(a) printf(a ? "Yes\n" : "No\n")
#define printYN(a) printf(a ? "YES\n" : "NO\n")
#define printim(a) printf(a ? "possible\n" : "imposible\n")
#define printdb(a) printf("%.50lf\n", a)
#define printLdb(a) printf("%.50Lf\n", a)
#define printdbd(a) printf("%.16lf\n", a)
#define prints(s) printf("%s\n", s.c_str())
#define all(x) (x).begin(), (x).end()
#define deg_to_rad(deg) (((deg)/360.0L)*2.0L*PI)
#define rad_to_deg(rad) (((rad)/2.0L/PI)*360.0L)
#define Please return
#define AC 0
#define manhattan_dist(a, b, c, d) (abs(a - c) + abs(b - d))

using ll = long long;
using ull = unsigned long long;

constexpr int INF = 1073741823;
constexpr int MINF = -1073741823;
constexpr ll LINF = ll(4661686018427387903);
constexpr ll MOD = 1e9 + 7;
constexpr ll mod = 998244353;
constexpr long double eps = 1e-6;
const long double PI = acosl(-1.0L);

using namespace std;

void scans(string& str) {
	char c;
	str = "";
	scanf("%c", &c);
	if (c == '\n')scanf("%c", &c);
	while (c != '\n' && c != -1 && c != ' ') {
		str += c;
		scanf("%c", &c);
	}
}

void scanc(char& str) {
	char c;
	scanf("%c", &c);
	if (c == -1)return;
	while (c == '\n') {
		scanf("%c", &c);
	}
	str = c;
}

double acot(double x) {
	return PI / 2 - atan(x);
}

ll LSB(ll n) { return (n & (-n)); }

template<typename T>
inline T chmin(T& a, const T& b) {
	if (a > b)a = b;
	return a;
}

template<typename T>
inline T chmax(T& a, const T& b) {
	if (a < b)a = b;
	return a;
}

//cpp_int
#if __has_include(<boost/multiprecision/cpp_int.hpp>)
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using namespace boost::multiprecision;
#else
using cpp_int = ll;
#endif

//atcoder library
#if __has_include(<atcoder/all>)
#include <atcoder/all>
//using namespace atcoder;
#endif

/*
	random_device seed_gen;
	mt19937 engine(seed_gen());
	uniform_int_distribution dist(1, 100);
*/


/*----------------------------------------------------------------------------------*/

bool check(int p[30]) {
	bool ret = true;
	int q = p[0];
	rep(i, 9) {
		if (p[i] != q)ret = false;
	}
	if (not(p[9] == p[10] and p[10] == p[11]))ret = false;
	if (not(p[12] == p[13] and p[13] == p[14]))ret = false;
	if (not(p[15] == p[16] and p[16] == p[17]))ret = false;
	if (not(p[18] == p[19] and p[19] == p[20]))ret = false;
	q = p[21];
	rep(i, 9) {
		if (p[21 + i] != q)ret = false;
	}
	return ret;
}

int solve(int p[30], int cnt) {
	if (cnt == 8)return cnt;
	int ret = INF;
	if (check(p))ret = cnt;
	++cnt;
	swap(p[6], p[21]);
	swap(p[7], p[22]);
	swap(p[8], p[23]);
	swap(p[12], p[17]);
	swap(p[9], p[11]);
	chmin(ret, solve(p, cnt));
	swap(p[6], p[21]);
	swap(p[7], p[22]);
	swap(p[8], p[23]);
	swap(p[12], p[17]);
	swap(p[9], p[11]);

	swap(p[0], p[27]);
	swap(p[1], p[28]);
	swap(p[2], p[29]);
	swap(p[14], p[15]);
	swap(p[20], p[18]);
	chmin(ret, solve(p, cnt));
	swap(p[0], p[27]);
	swap(p[1], p[28]);
	swap(p[2], p[29]);
	swap(p[14], p[15]);
	swap(p[20], p[18]);

	swap(p[0], p[23]);
	swap(p[3], p[26]);
	swap(p[6], p[29]);
	swap(p[20], p[9]);
	swap(p[15], p[17]);
	chmin(ret, solve(p, cnt));
	swap(p[0], p[23]);
	swap(p[3], p[26]);
	swap(p[6], p[29]);
	swap(p[20], p[9]);
	swap(p[15], p[17]);

	swap(p[2], p[21]);
	swap(p[5], p[24]);
	swap(p[8], p[27]);
	swap(p[18], p[11]);
	swap(p[14], p[12]);
	chmin(ret, solve(p, cnt));
	swap(p[2], p[21]);
	swap(p[5], p[24]);
	swap(p[8], p[27]);
	swap(p[18], p[11]);
	swap(p[14], p[12]);

	return ret;
}

int main() {

	int n;
	scanf("%d", &n);
	while (n--) {
		int p[30];
		rep(i, 30)scanf("%d", &p[i]);
		printf("%d\n", solve(p, 0));
	}

	Please AC;
}
accuracy: 1
time_ms: 10
memory_kb: 3568
final_rank: 6

code_snippet: #include<iostream>
#include <queue>
using namespace std;
struct cube {
	int p[30];
	int d;
	bool check() {
		int i;
		for (i = 0; i < 8; i++) if (p[i] != p[i + 1]) return false;
		for (i = 9; i < 11; i++) if (p[i] != p[i + 1]) return false;
		for (i = 12; i < 14; i++) if (p[i] != p[i + 1]) return false;
		for (i = 15; i < 17; i++) if (p[i] != p[i + 1]) return false;
		for (i = 18; i < 20; i++) if (p[i] != p[i + 1]) return false;
		for (i = 21; i < 29; i++) if (p[i] != p[i + 1]) return false;
		return true;
	}
	cube rotate(int x) {
		cube c; c.d = d + 1;
		for (int i = 0; i < 30; i++) c.p[i] = p[i];
		if (x == 0) {
			c.p[6] = p[21]; c.p[21] = p[6];
			c.p[7] = p[22]; c.p[22] = p[7];
			c.p[8] = p[23]; c.p[23] = p[8];
			c.p[12] = p[17]; c.p[17] = p[12];
			c.p[9] = p[11]; c.p[11] = p[9];
			return c;
		}
		if (x == 1) {
			c.p[2] = p[21]; c.p[21] = p[2];
			c.p[5] = p[24]; c.p[24] = p[5];
			c.p[8] = p[27]; c.p[27] = p[8];
			c.p[11] = p[18]; c.p[18] = p[11];
			c.p[12] = p[14]; c.p[14] = p[12];
			return c;
		}
		if (x == 2) {
			c.p[2] = p[29]; c.p[29] = p[2];
			c.p[1] = p[28]; c.p[28] = p[1];
			c.p[0] = p[27]; c.p[27] = p[0];
			c.p[14] = p[15]; c.p[15] = p[14];
			c.p[18] = p[20]; c.p[20] = p[18];
			return c;
		}
		if (x == 3) {
			c.p[6] = p[29]; c.p[29] = p[6];
			c.p[3] = p[26]; c.p[26] = p[3];
			c.p[0] = p[23]; c.p[23] = p[0];
			c.p[9] = p[20]; c.p[20] = p[9];
			c.p[15] = p[17]; c.p[17] = p[15];
			return c;
		}
	}
};
int main() {
	int n;
	cin >> n;
	while (n--) {
		int i, j, k;
		cube c;
		for (i = 0; i < 30; i++) cin >> c.p[i];
		c.d = 0;
		queue<cube> q;
		q.push(c);
		int o = 8;
		while (!q.empty()) {
			c = q.front(); q.pop();
			if (c.d >= 8) continue;
			if (c.check()) {
				o = min(o, c.d);
				continue;
			}
			for (i = 0; i < 4; i++) q.push(c.rotate(i));
		}
		cout << o << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 11356
final_rank: 14

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
const double pi = 3.141592653589793238462643383279;
using namespace std;
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

//container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).endf()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).endf(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).endf())
#define SORT(c) sort((c).begin(), (c).endf())

//repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const double EPS = 1e-4, PI = acos(-1);
//ここから編集    
typedef string::const_iterator State;
int ans;
bool check(vector<int>&v){
    bool f1 = (v[0]==v[1]&&v[1]==v[2]&&v[2]==v[3]&&v[3]==v[4]&&v[4]==v[5]&&v[5]==v[6]&&v[6]==v[7]&&v[7]==v[8]&&v[8]==v[0]);
    bool f2 = (v[27]==v[28]&&v[28]==v[29]&&v[29]==v[21]&&v[21]==v[22]&&v[22]==v[23]&&v[23]==v[24]&&v[24]==v[25]&&v[25]==v[26]&&v[26]==v[27]);
    bool f3 = (v[9]==v[10]&&v[10]==v[11]&&v[11]==v[9]);
    bool f4 = (v[15]==v[16]&&v[16]==v[17]&&v[17]==v[15]);
    bool f5 = (v[18]==v[19]&&v[19]==v[20]&&v[20]==v[18]);
    bool f6 = (v[12]==v[13]&&v[13]==v[14]&&v[14]==v[12]);
    return f1&&f2&&f3&&f4&&f5&&f6;
}

void dfs(vector<int>v, int ti){
    if(ti > 8) return ;
    if(check(v)){
        ans = min(ans, ti);
        return;
    }
    if(ans <= ti) return;
    vector<int> tmp = v;
    swap(tmp[0], tmp[23]);
    swap(tmp[3], tmp[26]);
    swap(tmp[6], tmp[29]);
    swap(tmp[20], tmp[9]);
    swap(tmp[15], tmp[17]);
    dfs(tmp, ti+1);

    tmp = v;
    swap(tmp[2], tmp[21]);
    swap(tmp[5], tmp[24]);
    swap(tmp[8], tmp[27]);
    swap(tmp[11], tmp[18]);
    swap(tmp[14], tmp[12]);
    dfs(tmp, ti+1);

    tmp = v;
    swap(tmp[0], tmp[27]);
    swap(tmp[1], tmp[28]);
    swap(tmp[2], tmp[29]);
    swap(tmp[15], tmp[14]);
    swap(tmp[20], tmp[18]);
    dfs(tmp, ti+1);
    tmp = v;
    swap(tmp[6], tmp[21]);
    swap(tmp[7], tmp[22]);
    swap(tmp[8], tmp[23]);
    swap(tmp[17], tmp[12]);
    swap(tmp[9], tmp[11]);
    dfs(tmp, ti+1);

}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    
    int t; cin >> t;
    while(t--){
        vector<int> v(30);
        REP(i,30) cin >> v[i];

        ans = INT_MAX;
        dfs(v, 0);
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3176
final_rank: 9

code_snippet: #include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define eps 1e-12 
//priority_queue<int,vector<int>, greater<int> > q2;

int ANS[] = { 1,1,1,1,1,1,1,1,1,2,2,2,4,4,4,6,6,6,5,5,5,3,3,3,3,3,3,3,3,3 };
VI A(30);


bool check() {
	REP(i, 30) {
		if (A[i] != ANS[i])return false;
	}
	return true;
}

void swap1() {
	swap(A[6], A[21]);
	swap(A[7], A[22]);
	swap(A[8], A[23]);
	swap(A[12], A[17]);
	swap(A[9], A[11]);
	return;
}

void swap2() {
	swap(A[0], A[27]);
	swap(A[1], A[28]);
	swap(A[2], A[29]);
	swap(A[15], A[14]);
	swap(A[20], A[18]);
}

void swap3() {
	swap(A[0], A[23]);
	swap(A[3], A[26]);
	swap(A[6], A[29]);
	swap(A[20], A[9]);
	swap(A[17], A[15]);
}

void swap4() {
	swap(A[2], A[21]);
	swap(A[5], A[24]);
	swap(A[8], A[27]);
	swap(A[12], A[14]);
	swap(A[18], A[11]);
}


int dfs(int cnt) {
	/*
	cout << cnt << endl;
	REP(i, 30)cout << A[i] << " ";
	cout << endl;
	*/
	int res = INF;
	if (cnt == 8)return INF;
	
	REP(i, 4) {
		if (i == 0)swap1();
		if (i == 1)swap2();
		if (i == 2)swap3();
		if (i == 3)swap4();

		if (check())chmin(res, cnt + 1);
		else chmin(res, dfs(cnt + 1));

		if (i == 0)swap1();
		if (i == 1)swap2();
		if (i == 2)swap3();
		if (i == 3)swap4();
	}
	return res;
}

int solve() {
	if (check())return 0;
	int res = 1020;
	REP(i, 4)chmin(res, dfs(0));
	return res;
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int t; cin >> t;
	while (t--) {
		REP(i, 30)cin >> A[i];
		cout << solve() << endl;
	}


	return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 3220
final_rank: 13

code_snippet: #include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define eps 1e-12 
//priority_queue<int,vector<int>, greater<int> > q2;

int ANS[] = { 1,1,1,1,1,1,1,1,1,2,2,2,4,4,4,6,6,6,5,5,5,3,3,3,3,3,3,3,3,3 };
VI A(30);


bool check(VI tmp) {
	REP(i, 30) {
		if (tmp[i] != ANS[i])return false;
	}
	return true;
}

void swap1() {
	swap(A[6], A[23]);
	swap(A[7], A[22]);
	swap(A[8], A[21]);
	swap(A[12], A[17]);
	swap(A[9], A[11]);
	return;
}

void swap2() {
	swap(A[0], A[29]);
	swap(A[1], A[28]);
	swap(A[2], A[27]);
	swap(A[15], A[14]);
	swap(A[20], A[18]);
}

void swap3() {
	swap(A[0], A[23]);
	swap(A[3], A[26]);
	swap(A[6], A[29]);
	swap(A[20], A[9]);
	swap(A[17], A[15]);
}

void swap4() {
	swap(A[2], A[21]);
	swap(A[5], A[24]);
	swap(A[8], A[27]);
	swap(A[12], A[14]);
	swap(A[18], A[11]);
}


int dfs(int cnt) {
	/*
	cout << cnt << endl;
	REP(i, 30)cout << A[i] << " ";
	cout << endl;
	*/
	int res = INF;
	if (cnt == 8)return INF;
	
	REP(i, 4) {
		if (i == 0)swap1();
		//cout << "L" << endl;
		if (i == 1)swap2();
		if (i == 2)swap3();
		if (i == 3)swap4();

		if (check(A))chmin(res, cnt + 1);
		else chmin(res, dfs(cnt + 1));

		if (i == 0)swap1();
		if (i == 1)swap2();
		if (i == 2)swap3();
		if (i == 3)swap4();
	}
	return res;
}

int solve() {
	if (check(A))return 0;
	int res = 1020;
	REP(i, 4)chmin(res, dfs(0));
	return res;
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int t; cin >> t;
	while (t--) {
		REP(i, 30)cin >> A[i];
		cout << solve() << endl;
	}


	return 0;
}
accuracy: 0.125
time_ms: 380
memory_kb: 3176
final_rank: 19