#include<iostream>#include<cstring>#include<algorithm>usingnamespace std;constint N =100010;int n, m;int w[N], ne[N];int q[N];intmain(){int h;scanf("%d%d%d",&h,&n,&m);while(n --){int addr, data, next;scanf("%d%d%d",&addr,&data,&next);
w[addr]= data;
ne[addr]= next;}int cnt =0;for(int i = h; i !=-1; i = ne[i])
q[cnt ++]= i;reverse(q, q + cnt);for(int i = cnt -1; i >=0; i -= m)reverse(q +max(0, i - m +1), q + i +1);for(int i =0; i < cnt; i ++){int addr = q[i], next = q[i +1];printf("%05d %d ", addr, w[addr]);if(i == cnt -1)puts("-1");elseprintf("%05d\n", next);}return0;}