#include <iostream>
#include <deque>

int main(){
    std::deque<int> deq;
    int q;
    std::cin >> q;
    for (int i{0};i<q;i++){
        int a,b,c;
        std::cin >> a;
        if(a==0){
            std::cin >> b >> c;
            if(b==0){deq.emplace_front(c);}
            if(b==1){deq.emplace_back(c);}
        }
        if(a==1){
            std::cin >> b;
            std::cout << deq[b] << std::endl;
        }
        if(a==2){
            std::cin >> b;
            if(b==0){deq.pop_front();}
            if(b==1){deq.pop_back();}
        }
    }
}
