#include <bits/stdc++.h>
#define ll long long
#define vl vector<ll>
#define pl pair<ll,ll>
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,b) for(ll i=0;i<b;i++)
#define RFOR(i,a,b) for(ll i=b-1;i>=a;i--)
#define rsort(v) sort((v).rbegin(), (v).rend())
#define all(v) (v).begin(),(v).end()
using namespace std;
ll mod=1000000007;

deque<ll>deq;
ll q,p,d;
signed main(){
    ll n;
    cin>>n;
    rep(i,n) {
    cin>>q;
    if(q==0){
        cin>>d>>p;
        d==0?deq.push_front(p):deq.push_back(p);
    }else if(q==1){
        cin>>p;
        cout<<deq.at(p)<<endl;
    }else{
        cin>>d;
        d==0?deq.pop_front():deq.pop_back();
    }
    }
}
