def b j,k,l
k.any?{m,*n=k.rotate!
o=m.index j[l].swapcase
o&&([*0..5]-[o,5-o]).any?{|p|yield m,n,o,p}}end
def e a,b
a==b.swapcase
end
y=(0..5).map{[]}
(a=[*1..4,2,4,1,3]).zip(a.rotate(4),[0]*4+[5]*4).map{|b|b.map{i,j,k=b=b.rotate;y[i][j]=k}}
while gets[0]>?0
z=$<.take(7)<<$_
puts z.any?{a,*zz=z.rotate!
(0..5).any?{|c|b(a,zz,c){|d,o,f,i|b(d,o,i){|g,o,tt,j|b(g,o,j){|p,o,r,k|([*0..5]-[c,5-c]).any?{|l|e(p[k],a[l])&&b(a,o,y[l][c]){|s,o,u,m|b(s,o,m){|v,o,x,n|e(d[y[f][i]],v[n])&&b(v,o,y[x][n]){|h,o,cc,q|e(h[q],g[y[tt][j]])&&b(h,o,y[cc][q]){|w,_,o,t|e(w[t],p[y[r][k]])&&e(w[y[o][t]],s[y[u][m]])}}}}}}}}}}?:YES:"NO"end