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) {
        Scanner sc = new Scanner(System.in);
        int[] num = new int[6];

        for(int i = 0; i < num.length; i++) {
        	num[i] = sc.nextInt();
        }
        Dice dice = new Dice(num);

        int count = sc.nextInt();
        for(int i = 0; i < count; i++) {
        	int top = sc.nextInt();
            int front = sc.nextInt();
            dice.setTop(top);
            dice.setFront(front);
            System.out.println(dice.getRight());
        }
    }
}

class Dice {
	private int top;
    private int front;
    private int right;
    private int left;
    private int back;
    private int bottom;

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

    void change(char dir) {
    	switch(dir) {
    	case 'N' : moveN(); break;
    	case 'S' : moveS(); break;
    	case 'E' : moveE(); break;
    	case 'W' : moveW(); break;
    	}
    }

    void setTop(int top) {
    	if(top != this.top) {
    		if(top == this.front) {
    			moveN();
    		} else if(top == this.right) {
    			moveW();
    		} else if(top == this.left) {
    			moveE();
    		} else if(top == this.back) {
    			moveS();
    		} else if(top == this.bottom) {
    			moveS();
    			moveS();
    		}
    	}
    }

    void setFront(int front) {
    	while(true) {
    		if(front == this.front) {
    			break;
    		}
    		moveClockwise();
    	}
    }

    void moveN() {
    	int temp = top;
    	top = front;
    	front = bottom;
    	bottom = back;
    	back = temp;
    }
    void moveS() {
    	int temp = top;
    	top = back;
    	back = bottom;
    	bottom = front;
    	front = temp;
    }
    void moveE(){
        int temp = top;
        top = left;
        left = bottom;
        bottom = right;
        right = temp;
    }
    void moveW(){
        int temp = top;
        top = right;
        right = bottom;
        bottom = left;
        left = temp;
    }
    void moveClockwise() {
    	int temp = front;
    	front = right;
    	right = back;
    	back = left;
    	left = temp;
    }

    int getTop() {
    	return top;
    }
    int getRight() {
    	return right;
    }
}
