【題目敘述】https://cses.fi/problemset/task/1750/
#include <iostream>
using namespace std;
int n, q, a[200005][31], x, k;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; i++){
cin >> a[i][0];
}
for (int i = 1; i < 31; i++){
for (int j = 1; j <= n; j++){
a[j][i] = a[a[j][i-1]][i-1];
}
}
while (q--){
cin >> x >> k;
for (int i = 0; i < 31; i++){
if (k & (1<<i)){
x = a[x][i];
}
}
cout << x << "\n";
}
}