a = []
gets.to_i.times do
  command, num1, num2 = gets.split.map(&:to_i)
  case command
  when 0
    num1 == 0 ? a.unshift(num2) : a.push(num2)
  when 1
    puts a[num1]
  else
    num1 == 0 ? a.shift : a.pop
  end
end
