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;

class Dice {
	private int _t, _s, _e, _w, _n, _b;
	int getTop() {
		return _t;
	}
	void setTop(int t) {
		_t = t;
	}
	int getSouth() {
		return _s;
	}
	void setSouth(int s) {
		_s = s;
	}
	int getEast() {
		return _e;
	}
	void setEast(int e) {
		_e = e;
	}
	int getWest() {
		return _w;
	}
	void setWest(int w) {
		_w = w;
	}
	int getNorth() {
		return _n;
	}
	void setNorth(int n) {
		_n = n;
	}
	int getBottom() {
		return _b;
	}
	void setBottom(int b) {
		_b = b;
	}
	void rollX(boolean d) {
		if (d) {
			int temp = _t;
			_t = _n;
			_n = _b;
			_b = _s;
			_s = temp;
		} else {
			int temp = _t;
			_t = _s;
			_s = _b;
			_b = _n;
			_n = temp;
		}
	}
	void rollY(boolean d) {
		if (d) {
			int temp = _s;
			_s = _w;
			_w = _n;
			_n = _e;
			_e = temp;
		} else {
			int temp = _s;
			_s = _e;
			_e = _n;
			_n = _w;
			_w = temp;
		}
	}
	void rollZ(boolean d) {
		if (d) {
			int temp = _e;
			_e = _b;
			_b = _w;
			_w = _t;
			_t = temp;
		} else {
			int temp = _e;
			_e = _t;
			_t = _w;
			_w = _b;
			_b = temp;
		}
	}
	void anchor_t_s(int t, int s) {
		if (t == _e) {
			rollZ(true);
		} else if (t == _w) {
			rollZ(false);
		} else {
			while (t != _t) {
				rollX(true);
			}
		}
		while (s != _s) {
			rollY(true);
		}
	}
}

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Dice d = new Dice();
		d.setTop(sc.nextInt());
		d.setSouth(sc.nextInt());
		d.setEast(sc.nextInt());
		d.setWest(sc.nextInt());
		d.setNorth(sc.nextInt());
		d.setBottom(sc.nextInt());
		sc.nextLine();
		int q = sc.nextInt();
		sc.nextLine();
		for (int i = 0; i < q; ++i) {
			int t = sc.nextInt(), s = sc.nextInt();
			sc.nextLine();
			d.anchor_t_s(t, s);
			System.out.println(d.getEast());
		}
		sc.close();
	}
}
