#include <iostream>
#include <queue>
using namespace std;

int main(void){
    int n,q,x,y;
    cin>>n>>q;
    queue<int> Q[n];
    while(q--){
        cin>>x;
        if(x==0){
            cin>>x>>y;
            Q[x].push(y);
        }else if(x==1){
            cin>>x;
            if(Q[x].size()!=0) cout<<Q[x].front()<<endl;
        }else{
            cin>>x;
            if(Q[x].size()!=0)Q[x].pop();
        }
    }
}
