#include<bits/stdc++.h>
using namespace std;
int main ()
{
     signed long long x,a,q,y,z,n,t;
    cin>>n;
    cin>>q;
    getchar();
    queue<int>S[n];
    queue<int>S2[n];
     for(int i=1; i<=q; i++)
    {
        cin>>a;
        if(a==0)
        {
            cin>>t;
            cin>>x;
            S[t].push(x);
        }
        else if(a==1)
        {
            cin>>t;
            //cout<<"S[t] = ";
            if(!S[t].empty())
            {
          cout<<S[t].front()<<endl;
            S2[t].push(S[t].front());
            S2[t].pop();
            }
        }
        else if(a==2)
        {
            cin>>t;
            if(!S[t].empty())
            {
                S[t].pop();
            }
        }
    }
    return 0;
}

