#include <iostream>
#include <deque>

using namespace std;

int main()
{
    deque<int> data;
    int N; 
    cin >> N;
    
    for(int i = 0; i < N; ++i)
    {
        int Command; cin >> Command;
        
        if(Command == 0)
        {
            int d; cin >> d;
            int val; cin >> val;
            
            if(d==0) data.push_front(val);
            else data.push_back(val);
            
        }
        else if(Command == 1)
        {
            int p; cin >> p;
            cout << data[p] << endl;
        }
        else 
        {
            int d; cin >> d;
            
            if(d==0) data.pop_front();
            else data.pop_back();
            
        }
        
    }
    
    return 0;
    
}
