#include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;

int main(){
  int q;
  deque<int>Q;
  cin>>q;
  while(q--){
    int a,b,c;
    cin>>a>>b;
    if(!a){
      cin>>c;
      if(b)Q.push_back(c);
      else Q.push_front(c);
    }
    else if(a==1)cout<<Q[b]<<endl;
    else{
      if(b)Q.pop_back();
      else Q.pop_front();
    }
  }
}
