Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Dice II

Construct a dice from a given sequence of integers in the same way as Dice I.

You are given integers on the top face and the front face after the dice was rolled in the same way as Dice I. Write a program to print an integer on the right side face.

Input

In the first line, six integers assigned to faces are given in ascending order of their corresponding labels.
In the second line, the number of questions $q$ is given.

In the following $q$ lines, $q$ questions are given. Each question consists of two integers on the top face and the front face respectively.

Output

For each question, print the integer on the right side face.

Constraints

$0 \leq $ the integer assigned to a face $ \leq 100$

The integers are all different

$1 \leq q \leq 24$

Sample Input

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

Sample Output

3
5
6

Solution in Ruby (judge verdict: Accepted)

#!/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