#!/usr/bin/env ruby

result = []
dice = gets.split.map(&:to_i)
loop_number = gets.to_i
loop_number.times do |index|
    face = gets.split.map(&:to_i)
    case face[0]
    when dice[0] then
        case face[1]
        when dice[1] then
            result << dice[2]
        when dice[2] then
            result << dice[4]
        when dice[3] then
            result << dice[1]
        when dice[4] then
            result << dice[3]
        end
    end

    case face[0]
    when dice[1] then
        case face[1]
        when dice[2] then
            result << dice[0]
        when dice[0] then
            result << dice[3]
        when dice[3] then
            result << dice[5]
        when dice[5] then
            result << dice[2]
        end
    end

    case face[0]
    when dice[2] then
        case face[1]
        when dice[0] then
            result << dice[1]
        when dice[1] then
            result << dice[5]
        when dice[4] then
            result << dice[0]
        when dice[5] then
            result << dice[4]
        end
    end

    case face[0]
    when dice[3] then
        case face[1]
        when dice[0] then
            result << dice[4]
        when dice[1] then
            result << dice[0]
        when dice[4] then
            result << dice[5]
        when dice[5] then
            result << dice[1]
        end
    end

    case face[0]
    when dice[4] then
        case face[1]
        when dice[0] then
            result << dice[2]
        when dice[2] then
            result << dice[5]
        when dice[3] then
            result << dice[1]
        when dice[5] then
            result << dice[3]
        end
    end

    case face[0]
    when dice[5] then
        case face[1]
        when dice[1] then
            result << dice[3]
        when dice[2] then
            result << dice[1]
        when dice[3] then
            result << dice[4]
        when dice[4] then
            result << dice[2]
        end
    end
end

puts result
