class dice:
    state={}
    writtenval={}
    def __init__(self, state_=None,writtenval_=None):
        self.writtenval = writtenval_
        

        self.state={}
        if state_ != None:
            for key in state_.keys():
                self.state[key]=writtenval.index(state_[key])
            for keyset in [["now","behind"], ["N","S"], ["S","N"], ["W","E"], ["E","W"], ["behind","now"]]:
                if not keyset[0] in self.state.keys():
                    if keyset[1] in self.state.keys():
                        self.state[keyset[0]]=7-self.state[keyset[1]]
            if len(self.state.keys()) == 4 :
                state=self.state
                keys=self.state.keys()
                #どのペアがnull?
                if not "N" in keys or not "S" in keys:
                    if state["now"] == 1:
                        if state["E"] == 2:
                            state["N"]=3
                            state["S"]=7-state["N"]
                        elif state["E"] == 3:
                            state["N"] = 5
                            state["S"]=7-state["N"]
                        elif state["E"] == 5:
                            state["N"] = 4
                            state["S"]=7-state["N"]
                        elif state["E"] == 4:
                            state["N"] = 2
                            state["S"]=7-state["N"]
                        else:
                            print("miss")
                    elif state["now"] == 2:
                        if state["E"] == 3:
                            state["N"]=1
                            state["S"]=7-state["N"]
                        elif state["E"] == 1:
                            state["N"] = 4
                            state["S"]=7-state["N"]
                        elif state["E"] == 4:
                            state["N"] = 6
                            state["S"]=7-state["N"]
                        elif state["E"] == 6:
                            state["N"] = 3
                            state["S"]=7-state["N"]
                        else:
                            print("miss")
                    elif state["now"]==3:
                        if state["E"] == 6:
                            state["N"]=2
                            state["S"]=7-state["N"]
                        elif state["E"] == 2:
                            state["N"] = 1
                            state["S"]=7-state["N"]
                        elif state["E"] == 1:
                            state["N"] = 5
                            state["S"]=7-state["N"]
                        elif state["E"] == 5:
                            state["N"] = 6
                            state["S"]=7-state["N"]
                        else:
                            print("miss")
                    elif state["now"]==4:
                        if state["E"] == 1:
                            state["N"]=2
                            state["S"]=7-state["N"]
                        elif state["E"] == 2:
                            state["N"] = 6
                            state["S"]=7-state["N"]
                        elif state["E"] == 6:
                            state["N"] = 5
                            state["S"]=7-state["N"]
                        elif state["E"] == 5:
                            state["N"] = 1
                            state["S"]=7-state["N"]
                        else:
                            print("miss")
                    elif state["now"]==5:
                        if state["E"] == 1:
                            state["N"]=4
                            state["S"]=7-state["N"]
                        elif state["E"] == 4:
                            state["N"] = 6
                            state["S"]=7-state["N"]
                        elif state["E"] == 6:
                            state["N"] = 3
                            state["S"]=7-state["N"]
                        elif state["E"] == 3:
                            state["N"] = 1
                            state["S"]=7-state["N"]
                        else:
                            print("miss")
                    elif state["now"]==6:
                        if state["E"] == 2:
                            state["N"]=3
                            state["S"]=7-state["N"]
                        elif state["E"] == 3:
                            state["N"] = 5
                            state["S"]=7-state["N"]
                        elif state["E"] == 5:
                            state["N"] = 4
                            state["S"]=7-state["N"]
                        elif state["E"] == 4:
                            state["N"] = 2
                            state["S"]=7-state["N"]
                        else:
                            print("miss")
                    else:
                        print("miss")
                elif not "W" in keys or not "E" in keys:
                    if state["now"] == 1:
                        if state["N"] == 5:
                            state["W"]=4
                            state["E"]=7-state["W"]
                        elif state["N"] == 2:
                            state["W"]=3
                            state["E"]=7-state["W"]
                        elif state["N"] == 4:
                            state["W"]=2
                            state["E"]=7-state["W"]
                        elif state["N"] == 3:
                            state["W"]=5
                            state["E"]=7-state["W"]
                        else:
                            print("miss")
                    elif state["now"] == 2:
                        if state["N"] == 1:
                            state["W"]=4
                            state["E"]=7-state["W"]
                        elif state["N"] == 3:
                            state["W"]=1
                            state["E"]=7-state["W"]
                        elif state["N"] == 6:
                            state["W"]=3
                            state["E"]=7-state["W"]
                        elif state["N"] == 4:
                            state["W"]=6
                            state["E"]=7-state["W"]
                        else:
                            print("miss")
                    elif state["now"] == 3:
                        if state["N"] == 1:
                            state["W"]=2
                            state["E"]=7-state["W"]
                        elif state["N"] == 2:
                            state["W"]=6
                            state["E"]=7-state["W"]
                        elif state["N"] == 6:
                            state["W"]=5
                            state["E"]=7-state["W"]
                        elif state["N"] == 5:
                            state["W"]=1
                            state["E"]=7-state["W"]
                        else:
                            print("miss")
                    elif state["now"] == 4:
                        if state["N"] == 1:
                            state["W"]=5
                            state["E"]=7-state["W"]
                        elif state["N"] == 2:
                            state["W"]=1
                            state["E"]=7-state["W"]
                        elif state["N"] == 6:
                            state["W"]=2
                            state["E"]=7-state["W"]
                        elif state["N"] == 5:
                            state["W"]=6
                            state["E"]=7-state["W"]
                        else:
                            print("miss")
                    elif state["now"] == 5:
                        if state["N"] == 1:
                            state["W"]=3
                            state["E"]=7-state["W"]
                        elif state["N"] == 4:
                            state["W"]=1
                            state["E"]=7-state["W"]
                        elif state["N"] == 6:
                            state["W"]=4
                            state["E"]=7-state["W"]
                        elif state["N"] == 3:
                            state["W"]=6
                            state["E"]=7-state["W"]
                        else:
                            print("miss")
                    elif state["now"] == 6:
                        if state["N"] == 2:
                            state["W"]=4
                            state["E"]=7-state["W"]
                        elif state["N"] == 3:
                            state["W"]=2
                            state["E"]=7-state["W"]
                        elif state["N"] == 5:
                            state["W"]=3
                            state["E"]=7-state["W"]
                        elif state["N"] == 4:
                            state["W"]=5
                            state["E"]=7-state["W"]
                        else:
                            print("miss")
                elif not "now" in keys or not "behind" in keys:
                    if state["N"] == 1:
                        if state["W"] == 2:
                            state["now"]=3
                            state["E"]=7-state["now"]
                        elif state["W"] == 5:
                            state["now"]=4
                            state["E"]=7-state["now"]
                        elif state["W"] == 4:
                            state["now"]=2
                            state["E"]=7-state["now"]
                        elif state["W"] == 3:
                            state["now"]=5
                            state["E"]=7-state["now"]
                        else:
                            print("miss")
                    elif state["N"] == 2:
                        if state["W"] == 3:
                            state["now"]=1
                            state["E"]=7-state["now"]
                        elif state["W"] == 6:
                            state["now"]=3
                            state["E"]=7-state["now"]
                        elif state["W"] == 4:
                            state["now"]=6
                            state["E"]=7-state["now"]
                        elif state["W"] == 1:
                            state["now"]=4
                            state["E"] = 7 - state["now"]
                        else:
                            print("miss")
                    elif state["N"] == 3:
                        if state["W"] == 5:
                            state["now"]=1
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 1:
                            state["now"]=2
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 6:
                            state["now"]=5
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 2:
                            state["now"]=6
                            state["E"] = 7 - state["now"]
                        else:
                            print("miss")
                    elif state["N"] == 4:
                        if state["W"] == 2:
                            state["now"]=1
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 6:
                            state["now"]=2
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 1:
                            state["now"]=5
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 5:
                            state["now"]=6
                            state["E"] = 7 - state["now"]
                        else:
                            print("miss")
                    elif state["N"] == 5:
                        if state["W"] == 4:
                            state["now"]=1
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 1:
                            state["now"]=3
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 6:
                            state["now"]=4
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 3:
                            state["now"]=6
                            state["E"] = 7 - state["now"]
                        else:
                            print("miss")
                    elif state["N"] == 6:
                        if state["W"] == 3:
                            state["now"]=2
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 5:
                            state["now"]=3
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 2:
                            state["now"]=4
                            state["E"] = 7 - state["now"]
                        elif state["W"] == 4:
                            state["now"]=5
                            state["E"] = 7 - state["now"]
                        else:
                            print("miss")
                    else:
                        print("miss")
            else:
                print("miss")
        else:
            self.state={"now":6,"N":5,"S":2,"W":4,"E":3,"behind":1}
        self.writtenval=writtenval_

    def chState(self):
        state=self.state
        for order in input():
            if order == "N":
                newstate = {"now": state["N"],
                            "N": state["behind"],
                            "S": state["now"],
                            "W": state["W"],
                            "E":state["E"],
                            "behind":state["S"]
                            }
                state=newstate
            elif order == "S":
                newstate = {"now": state["S"],
                            "N": state["now"],
                            "S": state["behind"],
                            "W": state["W"],
                            "E":state["E"],
                            "behind":state["N"]
                            }
                state=newstate
            elif order == "W":
                newstate = {"now": state["W"],
                            "N": state["N"],
                            "S": state["S"],
                            "W": state["behind"],
                            "E":state["now"],
                            "behind":state["E"]
                            }
                state=newstate
            elif order == "E":
                newstate = {"now": state["E"],
                            "N": state["N"],
                            "S": state["S"],
                            "W": state["now"],
                            "E":state["behind"],
                            "behind":state["W"]
                            }
                state = newstate
        self.state=state

writtenval=[-1]+[int(item) for item in input().split()]
loop=int(input())
for _ in range(loop):
    [behind,N]=[int(item) for item in input().split()]
    dice_ = dice({"N":N,"behind":behind},writtenval)
    print(dice_.writtenval[dice_.state["E"]])
#dice_ = dice()



Measured memory_bytes: 6356