g=gets.split
x=[0,2,5,3]
y=[0,4,5,1]
gets
while gets
a,b=$_.split.map{|s|g.index s}
n=[*g]
m=->r,d{r.map{|i|n[i]}.rotate(d).map.with_index{|v,i|n[r[i]]=v}}
m[rand(2)>0?x:y,rand(2)>0?1:-1]until g[a]==n[0]&&g[b]==n[1]
puts n[2]
end