# include <iostream>
# include <deque>
using namespace std;

int n;
deque<int> dq;

int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        int q, y, x; cin >> q >> y;
        if (q == 0) cin >> x;
        
        if (q == 0) {
            if (y == 0) dq.push_front(x);
            else dq.push_back(x);
        }
        else if (q == 1) cout << dq[y] << endl;
        else {
            if (y == 0) dq.pop_front();
            else dq.pop_back();
        }
    }
    
    return 0;
}
