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 JavaScript (judge verdict: Accepted)

//config = {stdin: 'tmp', newline: '\r\n'}; // win
config = {stdin: '/dev/stdin', newline: '\n'}; // linux

line = require('fs').readFileSync(config.stdin, 'ascii')
       .trim()
       .split(config.newline)
       .map(function (line) { return line.split(' '); });

number = line[0];

dice_init = { 'T': number[0], 'S': number[1], 'E': number[2],
              'W': number[3], 'N': number[4], 'B': number[5] };
function assign(dst, src) {
  for (var i in src) dst[i] = src[i];
  return dst;
}
dice = assign({}, dice_init);
src = assign({}, dice);

function turn_east(dst, src) {
  dst.T = src.W; dst.S = src.S; dst.E = src.T;
  dst.W = src.B; dst.N = src.N; dst.B = src.E;
}
function turn_north(dst, src) {
  dst.T = src.S; dst.S = src.B; dst.E = src.E;
  dst.W = src.W; dst.N = src.T; dst.B = src.N;
}
function turn_south(dst, src) {
  dst.T = src.N; dst.S = src.T; dst.E = src.E;
  dst.W = src.W; dst.N = src.B; dst.B = src.S;
}
function turn_west(dst, src) {
  dst.T = src.E; dst.S = src.S; dst.E = src.B;
  dst.W = src.T; dst.N = src.N; dst.B = src.W;
}
function turn_clockwise(dst, src) {
  dst.T = src.T; dst.S = src.E; dst.E = src.N;
  dst.W = src.S; dst.N = src.W; dst.B = src.B;
}

for (i = 2; i < line.length; i++) {
  top = line[i][0];
  front = line[i][1];
  while (dice.T !== top && dice.S !== top && dice.N !== top) {
    tmp = src;
    src = dice;
    dice = tmp;
    turn_east(dice, src);
  }
  while (dice.T !== top) {
    tmp = src;
    src = dice;
    dice = tmp;
    turn_south(dice, src);
  }
  while (dice.S !== front) {
    tmp = src;
    src = dice;
    dice = tmp;
    turn_clockwise(dice, src);
  }
  console.log(dice.E);
}