# frozen_string_literal: true

class Dice
  def initialize(params)
    @v = Array.new(6)
    (0..5).each do |i|
      @v[i] = params[i]
    end
    @right_side = {}
  end

  def slide(a, b, c, d)
    tmp = @v[a]
    @v[a] = @v[b]
    @v[b] = @v[c]
    @v[c] = @v[d]
    @v[d] = tmp
  end

  def rotate_atom(char)
    case char
    when 'N'
      slide(0, 1, 5, 4)
    when 'E'
      slide(0, 3, 5, 2)
    when 'W'
      slide(0, 2, 5, 3)
    when 'S'
      slide(0, 4, 5, 1)
    when 'D'
      slide(1, 3, 4, 2)
    when 'U'
      slide(1, 2, 4, 3)
    end
  end

  def rotate(string)
    string.chars.each do |c|
      rotate_atom(c)
    end
  end

  def top
    @v[0]
  end

  def register_z
    4.times do
      @right_side[[@v[0], @v[1]]] = @v[2]
      rotate_atom('D')
    end
  end

  def register
    4.times do
      register_z
      rotate_atom('S')
    end
    rotate_atom('W')
    register_z
    rotate_atom('E')
    rotate_atom('E')
    register_z
    rotate_atom('W')
  end

  def right_side(top, front)
    @right_side[[top,front]]
  end
end

dice = Dice.new(readline.split(' '))
dice.register
q = readline.chomp!.to_i
q.times do
  top, front = readline.split(' ')
  puts dice.right_side(top, front)
end

