#include<bits/stdc++.h>
#include<deque>
using namespace std;
int main() {

    int q,f,d,x,p;
    deque<int>Q;
    cin>>q;
    while(q--) {
        cin>>f;

        if(f==0) {
            cin>>d>>x;
            if(d==0)
                Q.push_front(x);
            if(d==1)
                Q.push_back(x);
        }

        else if(f==2) {
            cin>>d;

            if(d==0)
                Q.pop_front();

            if(d==1)
                Q.pop_back();
        } else if(f==1) {
            cin>>p;

            cout<<Q[p]<<endl;
        }
    }

    return 0;
}


