#include <iostream>
#include <fstream>
#include <queue>
//#define LOCAL
using namespace std;
typedef long long ll;
const int mx=1e3+10;
queue<ll>que[mx];
int main() {
#ifdef LOCAL
ifstream fin("1.txt");
streambuf*p;
p=cin.rdbuf(fin.rdbuf());
#endif // LOCAL
ll n, q;
cin>>n>>q;
for(ll i=1;i<=q;i++){
    ll qn, sub, value;
 //   int sub, que, value;
    cin>>qn;
    switch(qn){
        case 0: cin>>sub>>value;
                que[sub].push(value);
                break;
        case 1: cin>>sub;
                if(!que[sub].empty()) cout<<que[sub].front()<<endl;
                break;
        case 2: cin>>sub;
                if(!que[sub].empty()) que[sub].pop();
    }
}


#ifdef LOCAL
fin.close();
#endif // LOCAL
}


