#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define llint long long #define inf 1e18 #define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++) #define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++) #define chmin(x, y) (x) = min((x), (y)) #define chmax(x, y) (x) = max((x), (y)) using namespace std; typedef pair P; llint n, m; llint a[200005], b[200005], k[200005]; llint last[200005], qid[400005], val[400005]; vector G[400005]; llint l[200005], r[200005], pos[200005]; vector vec; void dfs(int v) { if(v > n) l[v-n] = (int)vec.size(); if(last[val[v]] == v) vec.push_back(val[v]); else pos[qid[v]] = (int)vec.size(); for(int i = (int)G[v].size()-1; i >= 0; i--) dfs(G[v][i]); if(v > n) r[v-n] = (int)vec.size()-1; } int main(void) { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for(int i = 1; i <= m; i++) cin >> a[i] >> b[i] >> k[i]; for(int i = 1; i <= n; i++){ last[i] = i, val[i] = i; if(i > 1) G[i-1].push_back(i); } for(int i = 1; i <= m; i++){ G[last[b[i]]].push_back(n+i); val[n+i] = a[i]; qid[last[a[i]]] = i; last[a[i]] = n+i; } dfs(1); //for(int i = 0; i < vec.size(); i++) cout << vec[i] << " "; cout << endl; //for(int i = 1; i <= m; i++) cout << l[i] << " " << r[i] << " " << pos[i] << endl; for(int i = 1; i <= m; i++){ llint q = k[i]-1; if(l[i] < pos[i]){ if(q < l[i]){ cout << vec[q] << endl; continue; } q -= l[i]; if(q < pos[i]-r[i]-1){ cout << vec[r[i]+1 + q] << endl; continue; } q -= pos[i]-r[i]-1; if(q < r[i]-l[i]+1){ cout << vec[l[i]+q] << endl; continue; } cout << vec[k[i]-1] << endl; } else{ if(q < pos[i]){ cout << vec[q] << endl; continue; } q -= pos[i]; if(q < r[i]-l[i]+1){ cout << vec[l[i]+q] << endl; continue; } q -= r[i]-l[i]+1; if(q < l[i]-pos[i]-1){ cout << vec[pos[i] + q] << endl; continue; } cout << vec[k[i]-1] << endl; } } return 0; }