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

signed main(){

  int n,q;
  cin>>n>>q;

  vector<queue<int>> a(n);
  while(q--){
    int com;
    cin>>com;
    if(com==0){
      int t,x;
      cin>>t>>x;
      a[t].push(x);
    }
    if(com==1){
      int t;
      cin>>t;
      if(a[t].empty())continue;
      cout<<a[t].front()<<endl;
    }
    if(com==2){
      int t;
      cin>>t;
      if(a[t].empty())continue;
      a[t].pop();
    }
  }

}
