#include <queue>
#include<set>
#include<vector>
#include<list>
#include<deque>
#include<map>
#include <stack>
#include<algorithm>
#include<iterator>
#include<iostream>
#include<string>

using namespace std;

int main() {
    queue<int> v[1005];
    int n,q;
    cin>>n>>q;
    int a,b,c;
    while(q--){
        cin>>a;
        if(a==0){
            cin>>b>>c;
            v[b].push(c);
        }
        else if(a==1){
            cin>>b;
            if(!v[b].empty()) cout<<v[b].front()<<endl;
        }
        else{
            cin>>b;
            if(!v[b].empty()) v[b].pop();
        }
    }
    return 0;
}


