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 Main{
    public static void main(String[] args){
    	
    	Scanner sc = new Scanner(System.in);
    	
    	int Answer = 0;
    	String strnum = sc.nextLine();//サイコロの目
    	int q = sc.nextInt();//質問数
    	
    	for(int i = 0 ; i < q ; i++ ){//qの回数分繰り返す
    		
    		int ue = sc.nextInt();//質問　上
        	int mae = sc.nextInt();//質問　前
        	
        	Dice dice = new Dice( strnum );
        	
        	Answer = dice.judge( ue , mae );
        	
        	System.out.println(Answer);
        	
    	}
    }
}

//-----------------------------------------------------------------

class Dice{
	
	int[] dice = new int[6];
	String[] me ;
	
	int ue = 0;
	int mae = 0;
	int kari = 0;
	int Answer = 0;
	
	//mainクラスで入力されたものを格納
	public Dice( String strnum ){
				
		me = strnum.split(" ");
		
		for(int i = 0 ; i < me.length ; i++ ){
			dice[i] = Integer.parseInt(me[i]);
		}
	}
	
	//dice[0]=ue dice[1]=mae に合わせた時のdice[2]を返す
	public int judge( int ue , int mae){
		
		for( int i = 0 ; i < 4 ; i++ ){
			if( dice[0] == ue ){
				break;
			}
			N();
		}
		
		for( int i = 0 ; i < 4 ; i++ ){
			if( dice[0] == ue ){
				break;
			}
			E();
		}
		
		for( int i = 0 ; i < 4 ; i++ ){
			if( dice[1] == mae ){
				break;
			}
			N();
			E();
			S();
		}
		
		return dice[2];
		
	}
	
	
	public void E(){
		kari = dice[0];
        dice[0] = dice[3] ;
        dice[3] = dice[5] ;
        dice[5] = dice[2];
        dice[2] = kari;
        //return dice[0];
	}
	
	public void N(){
		kari = dice[0];
        dice[0] = dice[1] ;
        dice[1] = dice[5] ;
        dice[5] = dice[4];
        dice[4] = kari;
        //return dice[0];
	}
	public void S(){
		kari = dice[0];
	    dice[0] = dice[4] ;
	    dice[4] = dice[5] ;
	    dice[5] = dice[1];
	    dice[1] = kari;
	    //return dice[0];
	}
	public void W(){
		kari = dice[0];
	    dice[0] = dice[2] ;
	    dice[2] = dice[5] ;
	    dice[5] = dice[3];
	    dice[3] = kari;
	    //return dice[0];
	}
	
}