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

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);

		int top    = sc.nextInt();
		int front  = sc.nextInt();
		int left   = sc.nextInt();
		int right  = sc.nextInt();
		int back   = sc.nextInt();
		int bottom = sc.nextInt();
		
		HashMap<Integer, HashMap<Integer, Integer>> dice = new HashMap<Integer, HashMap<Integer, Integer>>();
		dice.put(top, new HashMap<Integer, Integer>());
		dice.put(front, new HashMap<Integer, Integer>());
		dice.put(left, new HashMap<Integer, Integer>());
		dice.put(right, new HashMap<Integer, Integer>());
		dice.put(back, new HashMap<Integer, Integer>());
		dice.put(bottom, new HashMap<Integer, Integer>());
		dice.get(top).put(front, left);
		dice.get(top).put(left, back);
		dice.get(top).put(back, right);
		dice.get(top).put(right, front);
		dice.get(front).put(bottom, left);
		dice.get(front).put(left, top);
		dice.get(front).put(top, right);
		dice.get(front).put(right, bottom);
		dice.get(bottom).put(back, left);
		dice.get(bottom).put(left, front);
		dice.get(bottom).put(front, right);
		dice.get(bottom).put(right, back);
		dice.get(back).put(top, left);
		dice.get(back).put(left, bottom);
		dice.get(back).put(bottom, right);
		dice.get(back).put(right, top);
		dice.get(left).put(top, front);
		dice.get(left).put(front, bottom);
		dice.get(left).put(bottom, back);
		dice.get(left).put(back, top);
		dice.get(right).put(front, top);
		dice.get(right).put(top, back);
		dice.get(right).put(back, bottom);
		dice.get(right).put(bottom, front);
		
		final int q = sc.nextInt();
		for(int i = 0; i < q; i++){
			final int a = sc.nextInt();
			final int b = sc.nextInt();
			
			System.out.println(dice.get(a).get(b));
			
		}
		
	}
}