dice = gets.split.map(&:to_i)
n = gets(chomp: true).to_i
order = [].tap{|a|n.times{a << file.gets.split.map(&:to_i)}}

def vertical_roll(new_dice, upper)
  while new_dice[0] != upper
    new_dice[0], new_dice[1], new_dice[4], new_dice[5] = new_dice[1], new_dice[5], new_dice[0], new_dice[4]
  end
  new_dice
end

def horizontal_roll(new_dice, front)
  while new_dice[1] != front
    new_dice[1], new_dice[2], new_dice[4], new_dice[3] = new_dice[2], new_dice[4], new_dice[3], new_dice[1]
  end
  new_dice
end

order.each do |odr|
  new_dice = dice
  idx1 = dice.index(odr[0])
  idx2 = dice.index(odr[1])
  case idx1
  when 0,1,4,5
    case idx2
    when 0,1,4,5
      vertical_roll(new_dice, odr[0])
    else
      horizontal_roll(vertical_roll(new_dice, odr[0]), odr[1])
    end
  else
    vertical_roll(horizontal_roll(new_dice, odr[1]), odr[0])
  end
  puts new_dice[2]
end
