n = gets(chomp:true).to_i
array = []

n.times do
  order, value, value2 = gets.split.map(&:to_i)
  case order
  when 0
    value == 0 ? array.unshift(value2) : array << value2
  when 1
    puts array[value]
  when 2
    value == 0 ? array.shift : array.pop
  end
end
