#include <stdlib.h>#include <stdio.h>
typedef struct tree{ int no; struct tree *left,*right;}NODE;NODE *root;int count = 0;
NODE *searchnode(NODE *root,int no){ NODE *t; if(root != NULL) { if(root->no == no) return root; else { t = searchnode(root->left,no); if( t == NULL) return searchnode(root->right,no); else return t; } } else { return NULL; }}
void visit(NODE *root,int num){ if(root != NULL) { printf("%d",root->no); count += 1; if(count == num) printf("/n"); else printf(" "); visit(root->left,num); visit(root->right,num); }}
void deltree(NODE *root){ if(root != NULL) { deltree(root->left); deltree(root->right); root = NULL; }}
int main(){ NODE *p,*r; int num,i,j,t; int a,b,c,rootno;
scanf("%d",&t); for(i=1;i<=t;i++) { scanf("%d",&num); scanf("%d",&rootno);
root = (NODE *)malloc(sizeof(NODE)); root->no = rootno; root->left=root->right=NULL;
for(j=1;j<num;j++) { scanf("%d %d %d",&a,&b,&c); r = searchnode(root,a); //!! p = (NODE *)malloc(sizeof(NODE)); p->no = b; p->left=p->right = NULL; if(c == 0) r->left = p; else r->right = p; }
visit(root,num); count = 0; deltree(root); }
return 0;}
