#include <iostream>
#include <string>
#include <queue>

using namespace std;

int main() {
	int n, q, query, t, x;

	cin >> n >> q;

	queue<int> Q[n];

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

		if (query == 0) {
			cin >> x;
			Q[t].push(x);
		}
		else if (query == 1) {
			if (Q[t].size() != 0)
				cout << Q[t].front() << endl;
		}
		else {
			if (Q[t].size() != 0)
				Q[t].pop();		
		}	
	}
}


