#include<stdio.h>
#include<algorithm>
#include<functional>
#include<deque>
using namespace std;
int main(void)
{
	deque<int> dq;
	int q,x,d,p,i,n;
	scanf("%d",&q);
	for(i=0;i<q;i++)	{
		scanf("%d",&n);
		if(n==0)	{
			scanf("%d %d",&d,&x);
			if(d==0)	dq.push_front(x);
			else dq.push_back(x);
		}
		else if(n==1)	{
			scanf("%d",&p);
			printf("%d\n",dq[p]);
		}
		else {
			scanf("%d",&d);
			if(d==0)	dq.pop_front();
			else dq.pop_back();
		}
	}
	return 0;
}
