#include <bits/stdc++.h>
using namespace std;

int main(){
	int q;
	deque<int> Q;
	cin >> q;
	while(q--){
		int a,b,c;
		cin >> a >> b;
		if(a == 0){
			cin >> c;
			if(b) Q.push_back(c);
			else Q.push_front(c); 
		}
		else if(a == 1) cout << Q[b] <<endl;
		else{
			if(b) Q.pop_back();
			else Q.pop_front();
		}
	}
	return 0;
}

