array = []
gets.to_i.times {
    input = gets.chomp.split.map(&:to_i)
    case input[0]
    when 0
        # push
        if input[1] == 0
            array.unshift(input[2])
        else
            array << input[2]
        end
    when 1
        # randomAccess
        puts array[input[1]]
    when 2
        # pop
        if input[1] == 0
            array.shift
        else
            array.pop
        end
    end
}

