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

#define rep(i,s,n) for(ll i = (s); i < (n); i++)
#define rep0(i,n) rep(i,0,n)
#define rep1(i,n) rep(i,1,n+1)

#define BR "\n"

int main() {
  int n, q;
  cin >> n >> q;

  queue<int> Q[n];

  rep0(i, q) {
    int query; cin >> query;
    if (query == 0) {
      int t, x; cin >> t >> x;
      Q[t].push(x);
    } else if (query == 1) {
      int t; cin >> t;
      if (Q[t].size() > 0) cout << Q[t].front() << BR;
    } else {
      int t; cin >> t;
      if (Q[t].size() > 0) Q[t].pop();
    }
  }

  return 0;
}
