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.


<image>


Constraints

* $0 \leq $ the integer assigned to a face $ \leq 100$
* The integers are all different
* $1 \leq q \leq 24$

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.

Example

Input

1 2 3 4 5 6
3
6 5
1 3
3 2


Output

3
5
6
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO ?????????????????????????????????????????????

		Scanner scan = new Scanner(System.in);
		
		// ?????????????????????
		Dise2 dise = new Dise2();
		// ?????¢?????°??????????????????
		for(int i = 0; i < 6; i++) {
			dise._values[i] = scan.nextInt();
		}
		//??????????????°???????????????
		int q = scan.nextInt();
		// ???????????°????????°?????????
		for(int i = 0; i < q; i++) {
			// ?????? ???????????????
			int top = scan.nextInt();
			int front = scan.nextInt();
			// ??????????????????????????????????´????????????°
			boolean result = false;
			
			// ?¢????
			for(int i2 = 0; i2 <= 6; i2++) {
				// ?????¢????¢????
				if (dise._values[1] == front) {
					// ?????¢????¢????
					result = topVarueCheck(dise, top, result);
				}
				else {
					dise.MoveSouth();
				}
				if (result == true) break;
				if (i2 == 3) {
					dise.MoveEast();
					dise.MoveSouth();
				}
			}
		}
		
	}

	private static boolean topVarueCheck(Dise2 dise, int top, boolean result) {
		// ?????¢????????£????????????????????¢??????????????§??????
		for(int i3 = 0; i3 < 4; i3++) {
			if (dise._values[0] == top) {
				// ?????¢?????????
				System.out.println(dise._values[2]);
				result = true;
				break;
			}
			else dise.MoveEast();
		}
		return result;
	}
}

// ??????????????????
class Dise2 {

	public int _values[] = new int[6];
	
	public void Dice(int values[]) {
    	for (int i = 0; i < values.length; i++)
    	this._values[i] = values[i];
	}
    
    public enum ValueType
    {
        Top,
        Front,
        Right,
        Left,
        Back,
        Bottom
    }
    
    public void MoveWest()
    {
        int tmp = this._values[0];
        this._values[0] = this._values[2];
        this._values[2] = this._values[5];
        this._values[5] = this._values[3];
        this._values[3] = tmp;
    }
    public void MoveEast()
    {
        int tmp = this._values[0];
        this._values[0] = this._values[3];
        this._values[3] = this._values[5];
        this._values[5] = this._values[2];
        this._values[2] = tmp;
    }
    public void MoveNorth()
    {
        int tmp = this._values[0];
        this._values[0] = this._values[1];
        this._values[1] = this._values[5];
        this._values[5] = this._values[4];
        this._values[4] = tmp;
    }
    public void MoveSouth()
    {
        int tmp = this._values[0];
        this._values[0] = this._values[4];
        this._values[4] = this._values[5];
        this._values[5] = this._values[1];
        this._values[1] = tmp;
    }


}