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