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

int main() {
    int n, q;
    cin >> n >> q;
    queue<int> que[n];
    while (q--) {
        int t;
        cin >> t;
        if (t == 0) {
            int a, b;
            cin >> a >> b;
            que[a].push(b);
        } else if (t == 1) {
            int a;
            cin >> a;
            if (que[a].size()) {
                cout << que[a].front() << endl;
            }
        } else {
            int a;
            cin >> a;
            if (que[a].size()) {
                que[a].pop();
            }
        }
    }
}
