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)

function Dice(num) {
  this.top    = num[0];
  this.front  = num[1];
  this.right  = num[2];
  this.left   = num[3];
  this.back   = num[4];
  this.bottom = num[5]

  this.roll = function(direction) {
    var tmp;
    switch (direction) {
      case 'E':
        tmp         = this.top;
        this.top    = this.left;
        this.left   = this.bottom;
        this.bottom = this.right;
        this.right  = tmp;
        break;
      case 'N':
        tmp         = this.top;
        this.top    = this.front;
        this.front  = this.bottom;
        this.bottom = this.back;
        this.back   = tmp;
        break;
      case 'S':
        tmp         = this.top;
        this.top    = this.back;
        this.back   = this.bottom;
        this.bottom = this.front;
        this.front  = tmp;
        break;
      case 'W':
        tmp         = this.top;
        this.top    = this.right;
        this.right  = this.bottom;
        this.bottom = this.left;
        this.left   = tmp
        break;
    }
  }

  this.turn = function(direction) {
    switch (direction) {
      case 'R':
        tmp        = this.front;
        this.front = this.right;
        this.right = this.back;
        this.back  = this.left;
        this.left  = tmp;
        break;
      case 'L':
        tmp        = this.front;
        this.front = this.left;
        this.left  = this.back;
        this.back  = this.right;
        this.right = tmp
        break;
    }
  }

  this.setTop = function(topNum) {
    switch (topNum) {
      case this.top:
        break;
      case this.front:
        this.roll('N');
        break;
      case this.right:
        this.roll('W');
        break;
      case this.left:
        this.roll('E');
        break;
      case this.back:
        this.roll('S');
        break;
      case this.bottom:
        this.roll('N');
        this.roll('N');
        break;
    }
  }

  this.setFront = function(frontNum) {
    switch (frontNum) {
      case this.top:
        break;
      case this.front:
        break;
      case this.right:
        this.turn('R');
        break;
      case this.left:
        this.turn('L');
        break;
      case this.back:
        this.turn('R');
        this.turn('R');
        break;
      case this.bottom:
        break;
    }
  }
}


var input = ""
process.stdin.resume();
process.stdin.setEncoding('utf-8');
process.stdin.on('data', function(chunk) {
  input += chunk;
});
 
process.stdin.on('end', function() {
  input = input.split("\n");

  var dice = new Dice(input[0].split(' ').map(Number));
  var q = Number(input[1]);

  for (var i = 2; i < 2 + q; i++) {
    var n = input[i].split(' ').map(Number);
    dice.setTop(n[0]);
    dice.setFront(n[1]);
    console.log(dice.right);
  }
});