#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,o,p,d,x;
    deque<long long int>dq;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&o,&p);
        if(o==0)
        {
            scanf("%d",&x);
            if(p==0) dq.push_front(x);
            else dq.push_back(x);
        }
        else if(o==1) cout<<dq[p]<<endl;
        else {
            if(p==0) dq.pop_front();
            else dq.pop_back();
        }
    }
    return 0;
}

