#include <iostream>
#include<deque>
using namespace std;

int main()
{
	int n, q;

	cin >> n >> q;

	deque<deque<int>> v(n);

	for (int i = 0; i < q; i++) {
		int command, t;
		cin >> command >> t;

		if (command == 0) {
			int x;
			cin >> x;

			v[t].push_back(x);
		}		
		else if (command == 1 && !v[t].empty()) {
			cout << v[t][0] << endl;
		}
		else if (command == 2 && !v[t].empty()) {
			v[t].pop_front();
		}
	}

	return 0;
}

