#!/usr/bin/ruby
# -*- coding: utf-8 -*-
=begin
ITP1_11-B
 
Structure and Class - Dice II
Time Limit : 1 sec, Memory Limit : 65536 KB
???????????? II

Dice I ??¨????§????????????§?????\??????????????°????????????????????????????????????????????????

????????????????????? Dice I ???????????§?????¢????????????????????¢??¨?????¢?????°??????????????¨??????????????????????????§????????´?????¢?????°???????????????????????°????????????????????????????????????



Input

?????????????????¢?????°???????????????????????????????????????????????§?????????????????????
???????????????????????° q

????????????????????????

?¶???? q

???????????????????????????????????????????????§????????¢??¨?????¢?????°????????¨??????????????´??°????????????????????§??????????????????????????????
Output

??????????????¨??????????????????????????´?????¢?????°??????????????????????????????????????????
Constraints

    0???

??\?????????????????????????????¢?????°??? ???100
??\?????????????????????????????¢?????°?????????????????°??????
1???q???24

Sample Input

1 2 3 4 5 6
3
6 5
1 3
3 2

Sample Output

3
5
6

=end
class Dice
  def initialize(f)
    @face = Array.new(7,"")
    (0.upto(f.length-1)).each {|i| @face[i+1] = f[i]} 
  end

  def dump
    puts @face[1] + " " +  @face[2] + " " + @face[6] + " " +  @face[5] + " " + @face[4] + " " +  @face[3]
  end

  def e
    tmp = @face[1] 
    @face[1] = @face[4]
    @face[4] = @face[6]
    @face[6] = @face[3] 
    @face[3] = tmp
    @face[1]
  end

  def w
    tmp = @face[1] 
    @face[1] = @face[3]
    @face[3] = @face[6]
    @face[6] = @face[4]
    @face[4] = tmp
    @face[1]
  end

  def n
    tmp = @face[1] 
    @face[1] = @face[2]
    @face[2] = @face[6]
    @face[6] = @face[5]
    @face[5] = tmp
    @face[1]
  end

  def s
    tmp = @face[1] 
    @face[1] = @face[5]
    @face[5] = @face[6]
    @face[6] = @face[2]
    @face[2] = tmp
    @face[1]
  end

  def upface
    @face[1]
  end

  def frontface
    @face[2]
  end

  def rightface
    @face[3]
  end

  def srchRight(s,t)
    (1..4).each {|i|
      break if self.frontface() == t;
      self.s
    }
    if self.frontface() != t then
      self.e
      (1..4).each {|i|
        break if self.frontface() == t;
        self.s
      }
    end
    abort "strange #{t} not found" if self.frontface() != t;
    (1..4).each {|i|
      break if self.upface() == s;
      self.e
    }
    abort "strange #{s} not found" if self.upface() != s;

    self.rightface()
  end
end

class Main
  def initialize
    fh = open("IPT1_11_B.txt")
    #fh = STDIN
    fl = fh.gets.chomp.split
    abort "small face data" if fl.length != 6
    d = Dice.new(fl)
    n = fh.gets.chomp.to_i
    (1..n).each {|i|
      fl = fh.gets.chomp.split
      puts d.srchRight(fl[0],fl[1])
    }
  end
end

if $0==__FILE__ then
  Main.new()
end