#include <algorithm>
#include<iostream>
#include<vector>
#include<deque>
#include<list>
#include<map>
#include<set>
#include<string>
#include <sstream>

#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<limits.h>

using namespace std;


int main(){
  long ii,jj,kk;
  vector<int> ret;
  deque<int> deq;

  int q,c,d,x;
  cin >> q;

  for(ii=0;ii<q;ii++){
    cin >> c;

    if(c==0){
      cin >> d >> x;
      if(d==0){
        //deq.insert(deq.begin(),x);
        deq.push_front(x);
      }
      else{
        deq.push_back(x);
      }
    }
    else if(c==1){
      cin >> d;
      cout << deq[d] << endl;
    }
    else{
      cin >> d;
      if(d==0){
        deq.pop_front();
      }
      else{
        deq.pop_back();
      }
    }
  }
  
  return 0;
}

