#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=1e6+10,inf=1e9+10;
const ll INF=1e18+10;
class Deque{
private:
struct node{
int x;
node *pst,*nxt;
};
node *head,*tail;int len=0;
public:
Deque(){
node h,t;
head=&h,tail=&t;
head->nxt=tail;
tail->pst=head;
}
void push_back(int x){
node n={x,tail->pst,tail};
cout<<head<<" "<<n.pst<<" "<<tail->pst<<endl;
n.pst->nxt=&n;
tail->pst=&n;
len++;
return;
}
void push_front(int x){
node n={x,head,head->nxt};
n.nxt->pst=&n;
head->nxt=&n;
len++;
return;
}
void pop_back(){
tail->pst=tail->pst->pst;
tail->pst->nxt=tail;
return;
}
void pop_front(){
head->nxt=head->nxt->nxt;
head->nxt->pst=head;
return;
}
int size(){
return len;
}
int front(){
return head->nxt->x;
}
int back(){
return tail->pst->x;
}
bool empty(){
return !len;
}
};
Deque q[N];
int main(){
int t;
cin>>t;
while(t--){
string op;int i;
cin>>op>>i;
cout<<t<<'\n';
if(op=="push_back"){
int x;
cin>>x;
q[i].push_back(x);
}
if(op=="push_front"){
int x;
cin>>x;
q[i].push_front(x);
}
if(op=="pop_back"&&q[i].size())
q[i].pop_back();
if(op=="pop_front"&&q[i].size())
q[i].pop_front();
if(op=="size")
cout<<q[i].size()<<'\n';
if(op=="front"&&q[i].size())
cout<<q[i].front()<<'\n';
if(op=="back"&&q[i].size())
cout<<q[i].back()<<'\n';
}
return 0;
}