#include <cstdio>
#include <deque>
using namespace std;
 
int main() {
  int num_query; scanf("%d", &num_query);
  deque<int> nums;
   
  while (num_query--) {
    int command; scanf("%d", &command);
     
    switch (command) {
      case 0:
        int d1, x; scanf("%d %d", &d1, &x);
        if (d1) nums.push_back(x);
        else nums.push_front(x);
        break;
         
      case 1:
        int p; scanf("%d", &p);
        printf("%d\n", nums[p]);
        break;
      
      case 2:
        int d2; scanf("%d", &d2);
        if (d2) nums.pop_back();
        else nums.pop_front();
        break;
    }
  }
}
