#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
using namespace std;
vector<int>father;
int find(int u) {
if (father[u] == u) {
return u;
}
else {
return father[u] = find(father[u]);
}
}
void join(int u, int v) {
u = find(u);
v = find(v);
if (u == v) return;
father[v] = u;
}
bool isSame(int u, int v) {
u = find(u);
v = find(v);
return u == v;
}
int main() {
int n;
cin >> n;
father = vector<int>(n + 1);
for (int i = 0; i <= n; i++) father[i] = i;
for (int i = 0; i < n; i++) {
int s, t;
cin >> s >> t;
if (isSame(s, t)) {
cout << s << ' ' << t << endl;
}
else {
join(s, t);
}
}
}