#include <iostream>
#include <vector>
#include <map>
using namespace std;

int v = 8; // number of vertices
vector <vector<int>> adjecancy(v + 1); // adjecncy list of the graph

void addEdge(int x,int y) //add edge to the graph
{
	adjecancy[x].push_back(y);
	//adjecancy[y].push_back(x);
}

void showAdjecancyList(vector <vector<int>> adjecancy)
{
	for (int i = 1; i < v+1; i++){
		cout << i << " : ";
		for (int j = 0; j < adjecancy[i].size(); j++) {
			cout << adjecancy[i][j] << " ";
		}
		cout << endl;
	}
}

int t = 0;
vector <int> arrival(v + 1);
vector <int> departure(v + 1);
map <pair<int, int>, string> edgeType;

void edgesTypeArrDepTime(int v)
{
	arrival[v] = ++t;

	for (int i : adjecancy[v])
	{
		if (arrival[i] == 0) //Not visited yet
		{
			edgeType[{v, i}] = "tree";
			edgesTypeArrDepTime(i);
		}
		else if (arrival[i] <= arrival[v])
		{
			if (departure[i] == 0) //still in recursion stack
				edgeType[{v, i}] = "back";
			else
				edgeType[{v, i}] = "cross";
		}
		else
			edgeType[{v, i}] = "forward";	
	}

	departure[v] = ++t;
}

void showEdgesType(vector <vector<int>> adjecancy)
{
	for (int i = 1; i < v + 1; i++) {
		for (int j = 0; j < adjecancy[i].size(); j++) {
			cout << i << " -> " << adjecancy[i][j] << " ( " << edgeType[{i, adjecancy[i][j]}] << " )\n";
		}
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	int n;
	cin >> n;
	int x, y;
	map <int, int> parent;
	while (n--)
	{
		cin >> x >> y;
		addEdge(x, y);
		parent[y] = x;
	}

	edgesTypeArrDepTime(1);

	vector <int> path;

	for (int i = 1; i < v + 1; i++) 
	{
		for (int j = 0; j < adjecancy[i].size(); j++) 
		{
			if (edgeType[{i, adjecancy[i][j]}] == "back")
			{
				path.push_back(adjecancy[i][j]);
				path.push_back(i);
				int x = i;
				while (x != adjecancy[i][j])
				{
					path.push_back(parent[x]);
					x = parent[x];
				}
				
				for (int k = path.size() - 1; k >= 0; k--)
					cout << path[k] << " ";
				cout << endl;
				path.clear();
			}
		}
	}
}