#include<bits/stdc++.h>
using namespace std;

signed main(){

  int q;
  cin>>q;
  deque<int> deq;
  while(q--){
    int com;cin>>com;
    if(com==0){
      int d,x;cin>>d>>x;
      if(d==0)deq.push_front(x);
      else deq.push_back(x);
    }
    else if(com==1){
      int p;cin>>p;
      cout<<deq[p]<<endl;
    }
    else if(com==2){
      int d;cin>>d;
      if(d==0)deq.pop_front();
      else deq.pop_back();
    }
  }

}
