#include<cstdio>
#include<cmath>
#include<vector>
#include<iostream>
#include<string>
#include<queue>
#define maxn 12
#define inf 1000000000
using namespace std;
struct node {
int l, r;
}root[maxn];
int parents[maxn];
int findroot(int t) {
int temp = t;
while (parents[temp] != inf) {
temp = parents[temp];
}
return temp;
}
void show_level_order(int r, int N) {
queue<int> q;
q.push(r);
int i = 0;
while(!q.empty()) {
int t = q.front();
q.pop();
printf("%d", t);
if (i < N - 1) printf(" ");
else printf("\n");
i++;
if (root[t].r != inf) q.push(root[t].r);
if (root[t].l != inf) q.push(root[t].l);
}
return;
}
void show_inorder(int r, int &n) {
if (r == inf) return;
show_inorder(root[r].r, n);
printf("%d", r);
if (n > 0) printf(" ");
else printf("\n");
n--;
show_inorder(root[r].l, n);
return;
}
int main() {
int N, a, b;
string sa, sb;
cin >> N;
for (int i = 0; i < N; i++) {
parents[i] = inf;
}
for (int i = 0; i < N; i++) {
cin >> sa >> sb;
if (sa[0] == '-') {
root[i].l = inf;
}
else {
sscanf(sa.c_str(), "%d", &a);
root[i].l = a;
parents[a] = i;
}
if (sb[0] == '-') {
root[i].r = inf;
}
else {
sscanf(sb.c_str(), "%d", &b);
root[i].r = b;
parents[b] = i;
}
}
int r = findroot(0);
show_level_order(r, N);
int tempN = N - 1;
show_inorder(r, tempN);
return 0;
}