A = []
q = gets.chomp.to_i
q.times{
    input = gets.chomp
    choice, d, x = input.split(" ")
    case choice
    when "0"
        if d == "0"
            A.unshift(x)
        elsif d == "1"
            A.push(x)
        end
    when "1"
        puts A[d.to_i]
    when "2"
        if d == "0"
            A.shift()
        elsif d == "1"
            A.pop()
        end
    else
    end
}
