#include<bits/stdc++.h>
using namespace std;
int main(){
    int p;
    deque <int>a;
    cin>>p;
    while(p){
        p--;
        int n,i,q;
        cin>>n;
        if(n==0){
            cin>>q>>i;
            if(q==1)
            a.push_back(i);
            else
            a.push_front(i);
        }
        if(n==1){
            cin>>i;
            cout<<a[i]<<endl;
        }
        if(n==2){
        	cin>>q;
        	if(q==1)
            a.pop_back();
            else
            a.pop_front();
        }
    }
}
