#include<iostream>
#include<deque>
using namespace std;
int q;
int main()
{
	cin>>q;
	deque<int>a;
	for(int i=0;i<q;i++)
	{
		int c;cin>>c;
		if(c==0)
		{
			int d,x;cin>>d>>x;
			if(d)a.push_back(x);
			else a.push_front(x);
		}
		else if(c==1)
		{
			int p;cin>>p;
			cout<<a[p]<<endl;
		}
		else
		{
			int d;cin>>d;
			if(d)a.pop_back();
			else a.pop_front();
		}
	}
}
