#include<stdio.h>
#include<stdlib.h>
#define N 10000
#define NIL NULL
struct node{
  struct node *l;
  struct node *r;
  struct node *p;
  int key;
};

typedef struct node * Node;
Node root;
/*
void insert(int k){
  Node y = NIL;
  Node x = root;
  Node z;
  z = malloc(sizeof(struct node));
  z->key = k;
  z->left = NIL;
  z->right = NIL;
  
  while(x != NIL){
    y = x;
    if(z->key < x->key)
      x = x->left;
    else{
      x = x->right;
    }
  }
  z->parent = y; 
    if(y==NIL){
      root=z;
    }
    else if(z->key < y->key){
      y->left = z;
    }
    else{
      y->right =z;
    }
}
*/
//Inorder Walk
void inorder(Node u){
  if(u != NIL){
    inorder(u->left);
    printf(" ");
    printf("%d",u->key);
    inorder(u->right);
  }
 
}
//Preorder Walk
void preorder(Node u){ 
    if (u != NIL) {
      printf(" ");
      printf("%d",u->key); 
      preorder(u->l);
      preorder(u->r);
    
    }
}
int main(void) {
  int i;
  int d;
  n = 25;
  for (i = 0; i < n; i++) T[i].p = T[i].l = T[i].r = NIL;
  for (i = 0; i < n; i++) {
    scanf("%d", &d);
    scanf("%d %d", &T[d].l, &T[d].r);
    T[T[d].l].p = T[T[d].r].p = d;
  }
      inorder(root);
      printf("\n");
      preorder(root);
      printf("\n");
    }
  }
  return 0;
}