import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine(); //?????¢?????°??????????????????
        Dice dice = new Dice(str);  //???????????????????????£??\??????

        int q = Integer.parseInt(br.readLine());    //???????????°q

        StringBuilder sb = new StringBuilder();
        int i, j = 0;

//      ??????q????????????????????§?????????
        while(q-- > 0){
            String[] str2 = br.readLine().split(" ");

            for(i = 0; i < str.length(); i++){
//              ?????¢??¨???????????????????????°?????????????????§????????¢?????????i???????????????????¢????
                if(dice.getNum(i).equals(str2[0])){
                    break;
                }
            }
//          ????????????i??????????????¢??????????????§??¢?????????
            switch(i){

//          ?????¢??¨???????????????????????°??????1?????¨???????????§???i = 0???????????????????????????????????????
            case 0:
                break;

//          ?????¢??¨???????????????????????°??????2?????¨??????N????????????1?????¢????????°i = 0??????????????????
            case 1:
                dice.moveN();
                break;

//          ??\???????§??????¢??????
            case 2:
                dice.moveW();
                break;

            case 3:
                dice.moveE();
                break;

            case 4:
                dice.moveS();
                break;

            case 5:
                dice.moveN();
                dice.moveN();
                break;
            }

            for(i = 1; i < str.length() - 1; i++){
//              ?????¢??¨???????????????????????°??????????????????i???????????????????¢????
                if(dice.getNum(i).equals(str2[1])){
                    break;
                }
            }
//          ?????¢??¨?????¢?????????????????????????????§????????´?????¢?????????j????¨????
            switch(i){

            case 1:
                j = 2;
                break;

            case 2:
                j = 4;
                break;

            case 3:
                j = 1;
                break;

            case 4:
                j = 3;
                break;
            }
            sb.append(dice.getNum(j)).append("\n");
        }
        System.out.print(sb);
    }
}

class Dice{
    private String[] str;
    private String tmp;

    Dice(String str){
        this.str = new String[6];
        this.str = str.split(" ");
    }
    public String getNum(int n){
        return this.str[n];

    }
    public void moveE(){
        tmp = this.str[0];
        this.str[0] = this.str[3];
        this.str[3] = this.str[5];
        this.str[5] = this.str[2];
        this.str[2] = tmp;
    }
    public void moveN(){
        tmp = this.str[0];
        this.str[0] = this.str[1];
        this.str[1] = this.str[5];
        this.str[5] = this.str[4];
        this.str[4] = tmp;
    }
    public void moveS(){
        tmp = this.str[0];
        this.str[0] = this.str[4];
        this.str[4] = this.str[5];
        this.str[5] = this.str[1];
        this.str[1] = tmp;
    }
    public void moveW(){
        tmp = this.str[0];
        this.str[0] = this.str[2];
        this.str[2] = this.str[5];
        this.str[5] = this.str[3];
        this.str[3] = tmp;
    }
}

Measured memory_bytes: 24356