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

int main(){
    deque<int> a;
    int n, q, d, num, i;
    scanf("%d", &n);
    for(i = 0; i < n; i ++){
        scanf("%d %d", &q, &d);
        if(q == 0){
            scanf("%d", &num);
            if(d == 0) a.push_front(num);
            else a.push_back(num);
        }
        else if(q == 1){
            printf("%d\n", a[d]);
        }
        else if(q == 2){
            if(d == 0) a.pop_front();
            else a.pop_back();
        }
    }
}
