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)

class Dice

    attr_accessor :dice_pos_1,:dice_pos_2,:dice_pos_3,:dice_pos_4,:dice_pos_5,:dice_pos_6

    def initialize(dice_num:[])
        @dice_pos_1 = dice_num[0]
        @dice_pos_2 = dice_num[1]
        @dice_pos_3 = dice_num[2]
        @dice_pos_4 = dice_num[3]
        @dice_pos_5 = dice_num[4]
        @dice_pos_6 = dice_num[5]
    end
    def parse_spin(spin_directions:[])
        spin_directions.each do |spin|
            if spin == "N"
                turn_north()
            elsif spin == "S"
                turn_south()
            elsif spin == "W"
                turn_west()
            elsif spin == "E"
                turn_east()
            end
        end
        return self
    end
    def turn_north
        @dice_pos_1,@dice_pos_2,@dice_pos_5,@dice_pos_6 = @dice_pos_2,@dice_pos_6,@dice_pos_1,@dice_pos_5
        return self
    end
    def turn_south
        @dice_pos_1,@dice_pos_2,@dice_pos_5,@dice_pos_6 = @dice_pos_5,@dice_pos_1,@dice_pos_6,@dice_pos_2
        return self
    end
    def turn_west
        @dice_pos_1,@dice_pos_3,@dice_pos_4,@dice_pos_6 = @dice_pos_3,@dice_pos_6,@dice_pos_1,@dice_pos_4
        return self
    end
    def turn_east
        @dice_pos_1,@dice_pos_3,@dice_pos_4,@dice_pos_6 = @dice_pos_4,@dice_pos_1,@dice_pos_6,@dice_pos_3
        return self
    end
end
dice_info = gets.split.map(&:to_i)
answers = []
gets.to_i.times do
    answers.push(gets.split.map(&:to_i))
end

answers.each do |answer|
    dices = [Dice.new(dice_num:dice_info)]
    new_dices = []
    break_flag = true
    while break_flag
        dices.each do |dice|
            if answer[0] == dice.dice_pos_1 && answer[1] == dice.dice_pos_2
                puts dice.dice_pos_3
                break_flag = false
                break
            else
                new_dices.push(dice.clone.turn_north)
                new_dices.push(dice.clone.turn_south)
                new_dices.push(dice.clone.turn_east)
                new_dices.push(dice.clone.turn_west)
            end
        end
        dices = new_dices
    end
end

=begin
=end