#include<iostream>
#include<deque>
using namespace std;
int main() {
	long n;
	cin >> n;
	deque <long long> x;
	for (long i = 0; i < n; i++)
	{
		int y;
		cin >> y;
		if (y == 0)
		{
			int n;
			cin >> n;
			long long m;
			cin >> m;
			if (n == 0)
				x.push_front(m);
			else
				x.push_back(m);
		}
		else if (y == 1)
		{
			long p;
			cin >> p;
			cout << x[p] << endl;
		}
		else if (y == 2)
		{
			int n;
			cin >> n;
			if (n == 0)
				x.pop_front();
			else
				x.pop_back();
		}
	}
	x.clear();
	return 0;
}
