不过好像可以不用pair<ll,ll>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
typedef long long ll;
map< pair<ll,ll> ,int>mp1,mp2;
int mp[10][10];
struct zb{
int x,y;
zb(){}zb(int _x,int _y){
x=_x;y=_y;
}
}mm[25];
struct node{
int a[22];
zb o;
pair<ll,ll> hh;
pair<ll,ll> vl(){
ll x1=0,x2=0;
for(int i=1;i<=10;i++)x1=x1*10+a[i];
for(int i=11;i<=21;i++)x2=x2*10+a[i];
return make_pair(x1,x2);
}
};
int mx[]={-1,-1,1,1};
int my[]={0,-1,0,1};
bool isok(int x,int y){
return x>=y&&y<=6&&y>=1;
}
node q1[111111],q2[111111];
int main()
{
int tmp=1;
for(int i=1;i<=6;i++)
for(int j=1;j<=i;j++){
mp[i][j]=tmp++;
mm[mp[i][j]]=zb(i,j);
}
node ed;
for(int i=1;i<=6;i++){
for(int j=1;j<=i;j++){
ed.a[mp[i][j]]=i-1;
}
}
ed.o=zb(1,1);
ed.hh=ed.vl();
int t;
scanf("%d",&t);
while(t--){
int l1,l2,r1,r2;
l1=l2=r1=r2=0;
mp1.clear();mp2.clear();
node st;zb oo;
for(int i=1;i<=21;i++){
scanf("%d",&st.a[i]);
if(st.a[i]==0){
st.o=mm[i];
}
}
st.hh=st.vl();
if(st.hh==ed.hh)printf("0\n");
else {
q1[r1++]=st;
q2[r2++]=ed;
mp1[st.hh]=1;
mp2[ed.hh]=1;
int ans,step=0,f=0;
for(int ca=1;ca<=10;ca++){
step++;
int l=l1,r=r1;
while(l!=r&&f==0){
node now=q1[l++],tmp;
for(int i=0;i<4;i++){
tmp=now;
tmp.o.x+=mx[i];
tmp.o.y+=my[i];
if(isok(tmp.o.x,tmp.o.y)){
// printf("%d %d\n",tmp.o.x,tmp.o.y);
swap(tmp.a[mp[tmp.o.x][tmp.o.y]],tmp.a[mp[now.o.x][now.o.y]]);
tmp.hh=tmp.vl();
if(mp1.find(tmp.hh)==mp1.end()){
if(mp2.find(tmp.hh)!=mp2.end()){
f=1;
ans=step;
}
else {
mp1[tmp.hh]=1;
q1[r1++]=tmp;
}
}
}
if(f)break;
}
}
l1=l;
if(f)break;
step++;
l=l2,r=r2;
while(l!=r&&f==0){
node now=q2[l++],tmp;
for(int i=0;i<4;i++){
tmp=now;
tmp.o.x+=mx[i];
tmp.o.y+=my[i];
if(isok(tmp.o.x,tmp.o.y)){
swap(tmp.a[mp[tmp.o.x][tmp.o.y]],tmp.a[mp[now.o.x][now.o.y]]);
tmp.hh=tmp.vl();
if(mp2.find(tmp.hh)==mp2.end()){
if(mp1.find(tmp.hh)!=mp1.end()){
f=1;
ans=step;
}
else {
mp2[tmp.hh]=1;
q2[r2++]=tmp;
}
}
}
if(f)break;
}
}
l2=l;
}
if(f)printf("%d\n",ans);
else printf("too difficult\n");
}
}
return 0;
}