#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<fstream>
using namespace std;
    
#define NUM 10000
queue<int> s[NUM];

int main(){

    int n, q, order, t;
    cin >> n >> q;
    for(int i=0;i<q;i++){
        cin >> order >> t;
        switch (order)
        {
        case 0:
            int num;
            cin >> num;
            s[t].push(num);
            break;
        case 1:
            if(!(s[t].empty())) cout << s[t].front() << endl;
            break;
        case 2:
            if(!(s[t].empty()))s[t].pop();
            break;
        default:
            break;
        }
    }
    return 0;
}

