dc = gets.split.map(&:to_i)
q = gets.to_i
q.times do
  t,f = gets.split.map(&:to_i).map{|i| dc.index(i)+1}
  case t
  when 1,6
    s = [2,3,5,4]
    while (s[0] != f) do
      s.unshift(s.pop)
    end
    puts (t == 1) ? dc[s[1]-1] : dc[s[-1]-1]
  when 2,5
    s = [1,4,6,3]
    while (s[0] != f) do
      s.unshift(s.pop)
    end
    puts (t == 2) ? dc[s[1]-1] : dc[s[-1]-1]
  when 3,4
    s = [1,2,6,5]
    while (s[0] != f) do
      s.unshift(s.pop)
    end
    puts (t == 3) ? dc[s[1]-1] : dc[s[-1]-1]
  end
end