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

int main() {
    int n, q;
    cin >> n >> q;
    queue<int> que[n];

    for(int i = 0; i < q; i++) {
        int p;
        cin >> p;
        if(p == 0) {
            int t, x;
            cin >> t >> x;
            que[t].push(x);
        }
        else if(p == 1) {
            int t;
            cin >> t;
            if(que[t].size() == 0){
                continue;
            }
            cout << que[t].front() << endl;;
        }
        else {
            int t;
            cin >> t;
            if(que[t].size() == 0){
                continue;
            }
            que[t].pop();
        }
    }

}
