#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Node Node;

struct Node{
    int data;
    Node *parent;
    Node *left;
    Node *right;
};

void initNode(Node*);
Node copyNode(Node*,Node*);
void insert(Node*,Node*,int);
void printTree(Node*);
void inorder(Node*);
void preorder(Node*);


int main(int argc, char** argv) {
    int commandSize;
    scanf("%d",&commandSize);
    Node tree[commandSize-1];
    for(int i = 0; i < commandSize; i++)
        initNode(&tree[i]);
    Node temp;
    int loc = 0;
    char cmd[7];
    for(int i = 0; i < commandSize; i++){
        initNode(&temp);
        scanf("%s",cmd);
        if(!strcmp(cmd,"insert")){
            scanf("%d", &temp.data);
            insert(&temp,tree,loc++);
        }
        else
            printTree(tree);            
    }
    
    return (EXIT_SUCCESS);
}

void initNode(Node *n){
    n->data = -1;
    n->parent = NULL;
    n->left = NULL;
    n->right = NULL;
}

void insert(Node *node, Node *tree, int loc){
    Node *temp;
    copyNode(temp,node);
    Node *parent = NULL;
    Node *current = tree;
    while(current != NULL && current->data != -1){
        parent = current;
        if(temp->data < current->data)
            current = current->left;
        else
            current = current->right;
    }
    temp->parent = parent;
    if(parent == NULL)
        copyNode(tree,temp);
    else if(node->data < parent->data){
        copyNode(&tree[loc],temp);
        parent->left = &tree[loc];
    }
    else{
        copyNode(&tree[loc],temp);
        parent->right = &tree[loc];
    }
}

Node copyNode(Node *dest, Node *orig){
    dest->data = orig->data;
    dest->parent = orig->parent;
    dest->left = orig->left;
    dest->right = orig->right;
}

void printTree(Node *root){
    inorder(root);
    printf("\n");
    preorder(root);
    printf("\n");
}

void inorder(Node *root){
    if(root->left != NULL)
        inorder(root->left);
    printf(" %d", root->data);
    if(root->right != NULL)
        inorder(root->right);
}

void preorder(Node *root){
    printf(" %d", root->data);
    if(root->left != NULL)
        preorder(root->left);
    if(root->right != NULL)
        preorder(root->right);
}

