r=raw_input r() p={} def f(x): if x!=p.get(x,x):p[x]=x=f(p[x]) return x while 1: q,a,b=map(f,r().split()) if'0'