#include <algorithm>
#include<iostream>
#include<vector>
#include<deque>
#include<stack>
#include<queue>
#include<list>
#include<map>
#include<set>
#include<string>
#include <sstream>

#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<limits.h>

using namespace std;


int main(){
  long ii,jj,kk;
  vector<int> ret;
  queue<int> s[1001];
  
  int n,q,c,t,x;

  cin >> n >> q;

  for(ii=0;ii<q;ii++){
    //cout << ii << endl;
    cin >> c;
    switch(c){

    case 0:
      cin >> t >> x;
      s[t].push(x);
      break;

    case 1:
      cin >> t;
      if(!s[t].empty()){
        cout << s[t].front() << endl;
      }
      break;

    case 2:
      cin >> t;
      if(!s[t].empty()){
        s[t].pop();
      }
      break;

    }
  }
  
  return 0;
}

