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

using System;
using System.Collections.Generic;
using System.Text;

namespace ITP1_11_B
{
	class Program
	{
		// faces[] = { 0, 1, 2, 3, 4, 5, 6 };
		//   0
		// 3 1 2 4
		//   5
		static void Main(string[] args)
		{
			string s1 = Console.ReadLine();
			string[] faces = s1.Split();

			string s2 = Console.ReadLine();
			int count = int.Parse(s2);
			for (int i = 0; i < count; i++) {
				string s3 = Console.ReadLine();
				string[] str = s3.Split();

				Dice dice = new Dice(faces);
				// 上面を探す
				int c = 0;
				while (dice.Top != str[0]) {
					if (c < 3) {
						dice.MoveN();
					} else if (c < 4) {
						dice.MoveE();
					} else if (c < 5) {
						dice.MoveE();
						dice.MoveE();
					} else if (c < 6) {
						break;
					}
					c++;
				}
				// 正面を探す
				c = 0;
				while (dice.South != str[1]) {
					if (c < 3) {
						dice.MoveCW();
					} else {
						break;
					}
					c++;
				}
				// 右側面
				Console.WriteLine(dice.East);
			}
			//Console.WriteLine("Hit Any Key");
			//Console.ReadLine();
		}
	}

	class Dice
	{
		//   1
		// 4 2 3 5
		//   6
		public string Top { get; private set; }
		public string North { get; private set; }
		public string South { get; private set; }
		public string East { get; private set; }
		public string West { get; private set; }
		public string Bottom { get; private set; }
		public Dice(string[] faces)
		{
			Top = faces[0];
			North = faces[4];
			South = faces[1];
			East = faces[2];
			West = faces[3];
			Bottom = faces[5];
		}
		public void MoveN()
		{
			// T<-S<-B<-N<-T
			string tmp = Top;
			Top = South;
			South = Bottom;
			Bottom = North;
			North = tmp;
		}
		public void MoveS()
		{
			// T<-N<-B<-S<-T
			string tmp = Top;
			Top = North;
			North = Bottom;
			Bottom = South;
			South = tmp;
		}
		public void MoveE()
		{
			// T<-W<-B<-E<-T
			string tmp = Top;
			Top = West;
			West = Bottom;
			Bottom = East;
			East = tmp;
		}
		public void MoveW()
		{
			// T<-E<-B<-W<-T
			string tmp = Top;
			Top = East;
			East = Bottom;
			Bottom = West;
			West = tmp;
		}
		public void MoveCW()
		{
			// N<-W<-S<-E<-N
			string tmp = North;
			North = West;
			West = South;
			South = East;
			East = tmp;
		}
		public void MoveCCW()
		{
			// N<-E<-S<-W<-N
			string tmp = North;
			North = East;
			East = South;
			South = West;
			West = tmp;
		}
	}
}