#include using namespace std; int main(){ int N, M; cin >> N >> M; static vector edges[400000]; vector V(N+M); vector active(N), bef(N+M); vector K(N+M); for(int i=0; i> a >> b >> k; a--; b--; k--; K[i] = k; edges[active[b]].push_back(i); V[i] = a; bef[i] = active[a]; active[a] = i; } for(int i=0; i order; vector subnum(N+M), pos(N+M); auto dfs = [&](auto&& dfs, int i)->int{ int res = 0; pos[i] = order.size(); if(active[V[i]] == i){ order.push_back(V[i]); res++; } for(int j : edges[i]) res += dfs(dfs, j); subnum[i] = res; return res; }; dfs(dfs, 0); for(int i=N; i= max(posbef, posaft)+num){ ans = order[K[i]]; }else if(posbef <= posaft){ int R = K[i] - posbef; if(R < num){ ans = order[posaft+R]; }else{ ans = order[K[i]-num]; } }else{ int R = K[i] - posaft; int mid = posbef-posaft-num; if(R < mid){ ans = order[K[i]+num]; }else{ ans = order[posaft+R-mid]; } } cout << ans+1 << endl; } return 0; }