#include <bits/stdc++.h>
using namespace std;

int main() {
	int n, q;
	int a, b, c;
	cin >> n >> q;
	queue<int> arr[n];
	while(q--) {
		cin >> a;
		if(a == 0) {
			cin >> b >> c;
			arr[b].push(c);
		}
		else if(a == 1) {
			cin >> b;
			if(arr[b].size() == 0) {
				
			}
			else cout << arr[b].front() << endl;
		}
		else {
			cin >> b;
			if(arr[b].size() == 0) {
				
			}
			else arr[b].pop();
		}
	}
	return 0;
}
