#include <cstdio>
#include <vector>
#include <algorithm>
#include <random>
using namespace std;
int n, m;
int a[200020], b[200020], k[200020];
int c[200020], d[400040];
int a1[200020], b1[200020];
vector<int> g[400040];
bool root[400040];
int l[400040], r[400040], rev[400040];
int pr[400040];
int s[400040];
int main()
{
	scanf("%d %d", &n, &m);
	for(int i=0; i<m; i++){
		scanf("%d %d %d", &a[i], &b[i], &k[i]);
		a[i]--; b[i]--;
	}
	iota(c, c+n, 0);
	iota(d, d+n, 0);
	fill(root, root+n+m, 1);
	int cnt=n;
	for(int i=0; i<m; i++){
		pr[cnt]=c[a[i]];
		c[a[i]]=cnt++;
		d[c[a[i]]]=a[i];
		root[c[a[i]]]=0;
		a1[i]=c[a[i]], b1[i]=c[b[i]];
		g[b1[i]].push_back(a1[i]);
	}
	cnt=0;
	auto dfs=[&](auto dfs, int x)->void{
		l[x]=cnt++;
		rev[l[x]]=x;
		for(int i=(int)g[x].size()-1; i>=0; i--){
			dfs(dfs, g[x][i]);
		}
		r[x]=cnt;
	};
	for(int i=0; i<n; i++){
		if(root[i]) dfs(dfs, i);
	}
	for(int i=0; i<n+m; i++) if(i==c[d[i]]) s[l[i]+1]++;
	for(int i=0; i<n+m; i++) s[i+1]+=s[i];
	auto myon=[&](int k, int l, int r){
		return d[rev[lower_bound(s+l+1, s+r+1, s[l]+k)-s-1]];
	};
	for(int i=0; i<m; i++){
		int ans;
		int l1=l[a1[i]], r1=r[a1[i]], l2=l[pr[a1[i]]], r2=r[pr[a1[i]]];
		if(l2<l1){
			int c1=s[l2+1], c2=s[l1]-s[l2+1], c3=s[r1]-s[l1];
			if(k[i]<=c1) ans=myon(k[i], 0, l2+1);
			else if(k[i]<=c1+c3) ans=myon(k[i]-c1, l1, r1);
			else if(k[i]<=c1+c3+c2) ans=myon(k[i]-c1-c3, l2+1, l1);
			else ans=myon(k[i]-c1-c2-c3, r1, n+m);
		}else{
			int c1=s[l1], c2=s[r1]-s[l1], c3=s[l2+1]-s[r1];
			if(k[i]<=c1) ans=myon(k[i], 0, l1);
			else if(k[i]<=c1+c3) ans=myon(k[i]-c1, r1, l2+1);
			else if(k[i]<=c1+c3+c2) ans=myon(k[i]-c1-c3, l1, r1);
			else ans=myon(k[i]-c1-c2-c3, l2+1, n+m);
		}
		printf("%d\n", ans+1);
	}
    return 0;
}