import std.stdio, std.string, std.array, std.conv;

struct Dice6 {
    int[string] dice6;
    int[] _dice6 = new int[6];

    this(int[] _dice6) {
        dice6["top"] = _dice6[0];
        dice6["front"] = _dice6[1];
        dice6["right"] = _dice6[2];
        dice6["left"] = _dice6[3];
        dice6["back"] = _dice6[4];
        dice6["bottom"] = _dice6[5];
    }

    void roll(char c) {
        int tmp = dice6["top"];
        if (c == 'E') {
            dice6["top"] = dice6["left"];
            dice6["left"] = dice6["bottom"];
            dice6["bottom"] = dice6["right"];
            dice6["right"] = tmp;
        } else if (c == 'N') {
            dice6["top"] = dice6["front"];
            dice6["front"] = dice6["bottom"];
            dice6["bottom"] = dice6["back"];
            dice6["back"] = tmp;
        } else if (c == 'S') {
            dice6["top"] = dice6["back"];
            dice6["back"] = dice6["bottom"];
            dice6["bottom"] = dice6["front"];
            dice6["front"] = tmp;
        } else {
            dice6["top"] = dice6["right"];
            dice6["right"] = dice6["bottom"];
            dice6["bottom"] = dice6["left"];
            dice6["left"] = tmp;
        }
    }

    void clockwise() {
        int tmp = dice6["front"];
        dice6["front"] = dice6["right"];
        dice6["right"] = dice6["back"];
        dice6["back"] = dice6["left"];
        dice6["left"] = tmp;
    }

    int surface(string dir) {
        int result;
        if (dir == "top") {
            result = dice6["top"];
        } else if (dir == "front") {
            result = dice6["front"];
        } else if (dir == "right") {
            result = dice6["right"];
        } else if (dir == "left") {
            result = dice6["left"];
        } else if (dir == "back") {
            result = dice6["back"];
        } else {
            result = dice6["bottom"];
        }
        return result;
    }

    int right(int[] x) {
        if (x[0] == dice6["front"]) roll('N');
        if (x[0] == dice6["back"]) roll('S');
        while (x[0] != dice6["top"]) roll('E');
        while (x[1] != dice6["front"]) clockwise;
        return dice6["right"];
    }
}

void main() {
    int[] _dice = readln.chomp.split.to!(int[]);
    int q = readln.chomp.to!int;
    foreach (i; 0 .. q) {
        Dice6 dice = Dice6(_dice);
        int[] tmp = readln.chomp.split.to!(int[]);
        dice.right(tmp).writeln;
    }
}
