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

int main()
{
    deque<int> d;
    int t, s, x, q;
    cin >> t;
    while (t--)
    {
        cin >> q;
        if (q == 0)
        {
            cin >> s >> x;
            if (s == 0)
            {
                d.push_front(x);
            }
            else if (s == 1)
            {
                d.push_back(x);
            }
        }
        if (q == 1)
        {
            cin >> s;
            cout << d[s] << endl;
        }
        if (q == 2)
        {
            cin >> s;
            if (s == 0)
            {
                d.pop_front();
            }
            else if (s == 1)
            {
                d.pop_back();
            }
        }
    }
    return 0;
}
