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

#define rep(i,s,n) for(ll i = (s); i < (n); i++)
#define rep0(i,n) rep(i,0,n)
#define rep1(i,n) rep(i,1,n+1)

#define BR "\n"

int main() {
  int q;
  cin >> q;

  deque<int> que;

  rep0(i, q) {
    int query; cin >> query;
    if (query == 0) {
      int d, x; cin >> d >> x;
      if (d == 0) que.push_front(x);
      else que.push_back(x);

    } else if (query == 1) {
      int p; cin >> p;
      cout << que[p] << BR;

    } else {
      int d; cin >> d;
      if (d == 0) que.pop_front();
      else que.pop_back();
    }
  }

  return 0;
}
