   #depth first
N=int(input())
V=[]
a=[0]
for i in range(N):
    V.append([int(i) for i in input().split()])
    w=V[i][2:]
    w.sort()
    V[i]=V[i][:2]+w
    a.append(0)
V=sorted(V,key=lambda x:x[0])
c=1
t=1
i=0
r=[1]
a[1]=1
p=[[1,t]]
q=[]
while t<N*2:
    j=2
    while t<N*2 and j-2<V[i][1] and V[i][1]>0 and a[V[i][j]]==1:
        j+=1
    t+=1
    if j-2<V[i][1]:
        a[V[i][j]]=1
        c+=1
        r.append(V[i][j])
        p.append([V[i][j],t])
        i=V[i][j]-1
    else:
        q.append(p[-1]+[t])
        r.pop(-1)
        p.pop(-1)
        if len(r)>0:
            i=r[-1]-1
        else:
            j=1
            while j<N and a[j]==1:
                j+=1
            if j<N:
                t+=1
                i=j-1
                r.append(j)
                p.append([j,t])
q=sorted(q,key=lambda x:x[0])
for i in range(N):
    for j in range(3):
        q[i][j]=str(q[i][j])
    print(" ".join(q[i])) N=int(input()) N int(input()) int input() input V=[] V [] a=[0] a [0] 0 for i in range(N):
    V.append([int(i) for i in input().split()])
    w=V[i][2:]
    w.sort()
    V[i]=V[i][:2]+w
    a.append(0) i range(N) range N int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i V.append([int(i) for i in input().split()]) V.append V append [int(i) for i in input().split()] w=V[i][2:] w V[i][2:] [i] V i 2: 2 w.sort() w.sort w sort V[i]=V[i][:2]+w V[i] V i V[i][:2]+w V[i][:2] [i] V i :2 2 w a.append(0) a.append a append 0 V=sorted(V,key=lambda x:x[0]) V sorted(V,key=lambda x:x[0]) sorted V key=lambda x:x[0] key lambda x:x[0] x[0] x 0 x c=1 c 1 t=1 t 1 i=0 i 0 r=[1] r [1] 1 a[1]=1 a[1] a 1 1 p=[[1,t]] p [[1,t]] [1,t] 1 t q=[] q [] while t<N*2:
    j=2
    while t<N*2 and j-2<V[i][1] and V[i][1]>0 and a[V[i][j]]==1:
        j+=1
    t+=1
    if j-2<V[i][1]:
        a[V[i][j]]=1
        c+=1
        r.append(V[i][j])
        p.append([V[i][j],t])
        i=V[i][j]-1
    else:
        q.append(p[-1]+[t])
        r.pop(-1)
        p.pop(-1)
        if len(r)>0:
            i=r[-1]-1
        else:
            j=1
            while j<N and a[j]==1:
                j+=1
            if j<N:
                t+=1
                i=j-1
                r.append(j)
                p.append([j,t]) t<N*2 t N*2 N 2 j=2 j 2 while t<N*2 and j-2<V[i][1] and V[i][1]>0 and a[V[i][j]]==1:
        j+=1
     t<N*2 and j-2<V[i][1] and V[i][1]>0 and a[V[i][j]]==1 t<N*2 and j-2<V[i][1] and V[i][1]>0 t<N*2 and j-2<V[i][1] t<N*2 t N*2 N 2 j-2<V[i][1] j-2 j 2 V[i][1] [i] V i 1 V[i][1]>0 V[i][1] [i] V i 1 0 a[V[i][j]]==1 a[V[i][j]] a V[i][j] [i] V i j 1 j+=1 j 1 t+=1 t 1 if j-2<V[i][1]:
        a[V[i][j]]=1
        c+=1
        r.append(V[i][j])
        p.append([V[i][j],t])
        i=V[i][j]-1
    else:
        q.append(p[-1]+[t])
        r.pop(-1)
        p.pop(-1)
        if len(r)>0:
            i=r[-1]-1
        else:
            j=1
            while j<N and a[j]==1:
                j+=1
            if j<N:
                t+=1
                i=j-1
                r.append(j)
                p.append([j,t]) j-2<V[i][1] j-2 j 2 V[i][1] [i] V i 1 a[V[i][j]]=1 a[V[i][j]] a V[i][j] [i] V i j 1 c+=1 c 1 r.append(V[i][j]) r.append r append V[i][j] [i] V i j p.append([V[i][j],t]) p.append p append [V[i][j],t] V[i][j] [i] V i j t i=V[i][j]-1 i V[i][j]-1 V[i][j] [i] V i j 1 q.append(p[-1]+[t]) q.append q append p[-1]+[t] p[-1] p -1 [t] t r.pop(-1) r.pop r pop -1 p.pop(-1) p.pop p pop -1 if len(r)>0:
            i=r[-1]-1
        else:
            j=1
            while j<N and a[j]==1:
                j+=1
            if j<N:
                t+=1
                i=j-1
                r.append(j)
                p.append([j,t]) len(r)>0 len(r) len r 0 i=r[-1]-1 i r[-1]-1 r[-1] r -1 1 j=1 j 1 while j<N and a[j]==1:
                j+=1
             j<N and a[j]==1 j<N j N a[j]==1 a[j] a j 1 j+=1 j 1 if j<N:
                t+=1
                i=j-1
                r.append(j)
                p.append([j,t]) j<N j N t+=1 t 1 i=j-1 i j-1 j 1 r.append(j) r.append r append j p.append([j,t]) p.append p append [j,t] j t q=sorted(q,key=lambda x:x[0]) q sorted(q,key=lambda x:x[0]) sorted q key=lambda x:x[0] key lambda x:x[0] x[0] x 0 x for i in range(N):
    for j in range(3):
        q[i][j]=str(q[i][j])
    print(" ".join(q[i])) i range(N) range N for j in range(3):
        q[i][j]=str(q[i][j])
     j range(3) range 3 q[i][j]=str(q[i][j]) q[i][j] [i] q i j str(q[i][j]) str q[i][j] [i] q i j print(" ".join(q[i])) print " ".join(q[i]) " ".join " " join q[i] q i j=1 1 j j+=1 1 j j+=1 1 j i=0 0 i q=[] [] q r=[1] [1] r t+=1 1 t t+=1 1 t N=int(input()) int(input()) N c+=1 1 c a=[0] [0] a w=V[i][2:] V[i][2:] w j=2 2 j c=1 1 c i=V[i][j]-1 V[i][j]-1 i i=j-1 j-1 i t=1 1 t V=[] [] V p=[[1,t]] [[1,t]] p q=sorted(q,key=lambda x:x[0]) sorted(q,key=lambda x:x[0]) q V=sorted(V,key=lambda x:x[0]) sorted(V,key=lambda x:x[0]) V i=r[-1]-1 r[-1]-1 i
   #depth first
N=int(input())
V=[]
a=[0]
for i in range(N):
    V.append([int(i) for i in input().split()])
    w=V[i][2:]
    w.sort()
    V[i]=V[i][:2]+w
    a.append(0)
V=sorted(V,key=lambda x:x[0])
c=1
t=1
i=0
r=[1]
a[1]=1
p=[[1,t]]
q=[]
while t<N*2:
    j=2
    while t<N*2 and j-2<V[i][1] and V[i][1]>0 and a[V[i][j]]==1:
        j+=1
    t+=1
    if j-2<V[i][1]:
        a[V[i][j]]=1
        c+=1
        r.append(V[i][j])
        p.append([V[i][j],t])
        i=V[i][j]-1
    else:
        q.append(p[-1]+[t])
        r.pop(-1)
        p.pop(-1)
        if len(r)>0:
            i=r[-1]-1
        else:
            j=1
            while j<N and a[j]==1:
                j+=1
            if j<N:
                t+=1
                i=j-1
                r.append(j)
                p.append([j,t])
q=sorted(q,key=lambda x:x[0])
for i in range(N):
    for j in range(3):
        q[i][j]=str(q[i][j])
    print(" ".join(q[i]))