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.Random;
import java.util.Scanner;

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

		int[] n = new int[6];
		for(int i=0; i<6; i++)
		{
			n[i] = scanner.nextInt();
		}
		
		int q = scanner.nextInt();
		
		for(int i=0; i<q; i++)
		{
			int[] in = new int[2];
			in[0] = scanner.nextInt();
			in[1] = scanner.nextInt();
			
			for(;;)
			{
				Random random = new Random();
				int d = random.nextInt(4);
				
				int[] buf = new int[6];
				for(int j=0; j<6; j++)
				{
					buf[j] = n[j];
				}
					
				switch(d)
				{
					case 0:
						n[0] = buf[4];
						n[1] = buf[0];
						n[5] = buf[1];
						n[4] = buf[5];
						break;
					case 1:
						n[0] = buf[1];
						n[1] = buf[5];
						n[5] = buf[4];
						n[4] = buf[0];
						break;
					case 2:
						n[0] = buf[2];
						n[2] = buf[5];
						n[5] = buf[3];
						n[3] = buf[0];
						break;
					case 3:
						n[0] = buf[3];
						n[3] = buf[5];
						n[5] = buf[2];
						n[2] = buf[0];
						break;
				}
				if(in[0]==n[0] && in[1]==n[1])
				{
					System.out.println(n[2]);
					break;
				}
			}
		}
	}
}