#include<cstdio>
#include<iostream>
#include<deque>
#include<vector>
using namespace std;
int main()
{
	int t;
	deque<int>que;
	cin >> t;
	while (t--)
	{
		int a;
		cin >> a;
		if (a == 0)//插入 0头1尾
		{
			int b, c;
			cin >> b >> c;
			if (b == 0)
			{
				que.push_front(c);
			}
			else
			{
				que.push_back(c);
			}
		}
		if (a == 1)
		{
			int b;
			cin >> b;
			cout << que.at(b)<<endl;
		}
		if (a == 2)
		{
			int b;
			cin >> b;
			if (b == 0)
				que.pop_front();
			else que.pop_back();
		}
	}
}
