#include <bits/stdc++.h>
#include <queue>
#define NUM 1000
using namespace std;
int main(){
    int n, q, query, x, t;
    queue<int> s[NUM];
    scanf("%d %d", &n, &q);
    for(int i = 0; i < q; i++){
        scanf("%d %d", &query, &t);
        switch(query){
            case(0):
                scanf("%d", &x);
                s[t].push(x);
                break;
            case(1):
                if(!s[t].empty()) printf("%d\n", s[t].front());
                break;
            case(2):
                if(!s[t].empty()) s[t].pop();
                break;
        }
    }
}
