public class Main {
    public static void main(String[] args) {

        // ??????????????????????????????.
        java.util.Scanner sc = new java.util.Scanner(System.in);

        // ???????????????????????????.
        dice d1 = new dice();
        int[] line = new int[6];
        for (int i = 0; i < 6; i ++) { line[i] = sc.nextInt(); }
        d1.setNumber(line);

        // ?????¢??¨?????¢???????????¢?????????????????????.
        int q = sc.nextInt();
        int right = - 1;
        for (int i = 0; i < q; i ++) {
            right = d1.getRight(sc.nextInt(), sc.nextInt());
            System.out.println(right);
        }
    }
}

class dice {
    // ?????????????????¶???
    private int start[]  = new int[6];  // ?????????
    private int before[] = new int[6];  // ?????????
    private int after[]  = new int[6];  // ?????????

    // ??????????????????????????????
    String command;

    // ???????????????????????????????????????.
    public void setNumber(int[] line) {
        for (int i = 0; i < 6; i ++) { after[i] = before[i] = start[i] = line[i]; }
    }

    // ?????????????????????????????¶????????????.
    public void backStart() {
        for (int i = 0; i < 6; i ++) { after[i] = before[i] = start[i]; }
    }

    // ?????????????????????????????¨?????????.
    public void showNumber() {
        System.out.print("after: ");
        for (int i = 0; i < 6; i ++) {
            System.out.print(after[i] + " "); }
        System.out.println();
    }

    // ???????????????????¨????????????????.
    public void roleRight() {
        for (int i = 0; i < 6; i ++) { before[i] = after[i]; }
        after[0] = before[0];
        after[1] = before[2];
        after[2] = before[4];
        after[3] = before[1];
        after[4] = before[3];
        after[5] = before[5];
    }

    // ?????????????????????????????????????????¢??????.
    public void toNorth() {
        for (int i = 0; i < 6; i ++) { before[i] = after[i]; }
        after[0] = before[1];
        after[1] = before[5];
        after[2] = before[2];
        after[3] = before[3];
        after[4] = before[0];
        after[5] = before[4];
    }
    public void toSouth() {
        for (int i = 0; i < 6; i ++) { before[i] = after[i]; }
        after[0] = before[4];
        after[1] = before[0];
        after[2] = before[2];
        after[3] = before[3];
        after[4] = before[5];
        after[5] = before[1];
    }
    public void toWest() {
        for (int i = 0; i < 6; i ++) { before[i] = after[i]; }
        after[0] = before[2];
        after[1] = before[1];
        after[2] = before[5];
        after[3] = before[0];
        after[4] = before[4];
        after[5] = before[3];
    }
    public void toEast() {
        for (int i = 0; i < 6; i ++) { before[i] = after[i]; }
        after[0] = before[3];
        after[1] = before[1];
        after[2] = before[0];
        after[3] = before[5];
        after[4] = before[4];
        after[5] = before[2];
    }

    // ?????¢??¨?????¢???????????¢???????????????.
    public int getRight(int top, int front) {

        // ?????????????????¶??? (????????????) ?????¨???????????????.
        // ???????????£????????¢??¨?????¢????????????????????´?????????????????????????????¢?????????.
        int right = - 1;
        for (int i = 0; i <= 5; i ++) {
            for (int j = 0; j <= 4; j ++) {
                backStart();
                switch (i) {
                case 0: { break; }
                case 1: { toNorth(); break; }
                case 2: { toSouth(); break; }
                case 3: { toWest(); break; }
                case 4: { toEast(); break; }
                case 5: { toNorth(); toNorth(); break; }
                }
                switch (j) {
                case 0: { break; }
                case 1: { roleRight(); break; }
                case 2: { roleRight(); roleRight(); break; }
                case 3: { roleRight(); roleRight(); roleRight(); break; }
                }
                if (top == after[0] && front == after[1]) { right = after[2]; }
            }
        }
        return right;
    }
}

Measured memory_bytes: 26000