#include <bits/stdc++.h>
#define int long long
#define double long double
#define INF 1e18
using namespace std;
signed main() {
  deque<int> d;
  int N; cin >> N;
  for (int i = 0; i < N; i++) {
    int p, q, x;
    cin>>p>>q;
    if (p == 0) {
      cin>>x;
      if (q == 0) {
        d.push_front(x);
      } else {
        d.push_back(x);
      }
    } else if (p == 1) {
      cout << d[q] << endl;
    } else {
      if (q == 0) {
        d.pop_front();
      } else {
        d.pop_back();
      }
    }
  }  
}

