#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
typedef long long ll;
const ll inf = 1e9;
int main(){
    int q; cin >> q;
    deque<int> dq;
    rep(i,q){
        int com; cin >> com;
        int d, p;
        if(com == 0 || com == 2)cin >> d;
        if(com == 0 || com == 1)cin >> p;
        if(com == 0){
            if(d == 0)dq.push_front(p);
            else dq.push_back(p);
        }else if(com == 1)cout << dq[p] << endl;
        else {
            if(d == 0)dq.pop_front();
            else dq.pop_back();
        }
    }
    return 0;
} 
