#include<iostream>
#include <deque>
using namespace std;
int main(){
    deque<int> deq;
    int n;
    cin>>n;
    int a,b,c;
    while(n--){
        cin>>a;
        if(a==0){
            cin>>b>>c;
            if(b==0){
                deq.push_front(c);
            }
            else {
                deq.push_back(c);
            }
        }
        else if(a==1){
            cin>>b;
            cout<<deq[b]<<endl;
        }
        else{
            cin>>b;
            if(b==0){
                deq.pop_front();
            }
            else{
                deq.pop_back();
            }
        }
    }
    return 0;
}
