a = gets.split.map(&:to_i)

def roll(a)
    tmp = a[0]
    a[0] = a[1]
    a[1] = a[5]
    a[5] = a[4]
    a[4] = tmp
end

def spin(a)
    tmp = a[1]
    a[1] = a[2]
    a[2] = a[4]
    a[4] = a[3]
    a[3] = tmp
end

n = gets.to_i

n.times do
    t, f = gets.split.map(&:to_i)
    i = 0
    while t != a[0]
        spin(a) if i == 3
        roll(a)
        i += 1
    end
    while a[1] != f
        spin(a)
    end
    p a[2]
end
