#include <stdio.h>
#include<vector>
#include<iostream>
#include<string>
#include<deque>
#include<queue>
#include<stack>
#include<list>
using namespace std;


int main() {
	queue<int> arr[1000];
	int n,q,t,x;
	int query;
	cin >> n>>q;
	for (int i = 0; i < q; i++)
	{
		cin >> query>>t;
		switch (query)
		{
		case 0:
			cin >> x;
			arr[t].push(x);
			break;
		case 1:
			if (arr[t].size() == 0)
			{
			}
			else
			{
				cout << arr[t].front() << endl;
			}
			break;
		case 2:
			if (arr[t].size() != 0)
			{
				arr[t].pop();
			}

			break;
		}
	}
	

	return 0;
}
