#include <iostream>
#include <deque>
#include <sstream>

int main() {
    std::stringstream ss;
    std::deque<int> a;
    int q;
    std::cin >> q;
    for (int i=0;i<q;++i) {
        int cmd;
        std::cin >> cmd;
        if (cmd == 0) {
            int d, x;
            std::cin >> d >> x;
            if (d == 0) a.push_front(x);
            if (d == 1) a.push_back(x);
        }
        if (cmd == 1) {
            int p;
            std::cin >> p;
            ss << a[p] << std::endl;
        }
        if (cmd == 2) {
            int d;
            std::cin >> d;
            if (d == 0) a.pop_front();
            if (d == 1) a.pop_back();
        }
    }
    std::cout << ss.str();
    return 0;
}
