#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
vector<long long> find_maija_positions(int n, int q, const vector<int>& flows, const vector<long long>& queries) {
vector<int> positions;
unordered_map<int, int> visited;
int current = 1;
int time = 0;
while (time < *max_element(queries.begin(), queries.end())) {
if (visited.find(current) != visited.end()) {
int cycle_start = visited[current];
int cycle_length = time - cycle_start;
vector<int> cycle_positions(positions.begin() + cycle_start, positions.end());
vector<long long> results;
for (long long a : queries) {
if (a < positions.size()) {
results.push_back(positions[a]);
} else {
long long effective_time = (a - cycle_start) % cycle_length + cycle_start;
results.push_back(positions[effective_time]);
}
}
return results;
}
visited[current] = time;
positions.push_back(current);
current = flows[current - 1];
time++;
}
vector<long long> results;
for (long long a : queries) {
results.push_back(positions[min(a, (long long)positions.size() - 1)]);
}
return results;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<int> flows(n);
vector<long long> queries(q);
for (int i = 0; i < n; ++i) {
cin >> flows[i];
}
for (int i = 0; i < q; ++i) {
cin >> queries[i];
}
vector<long long> results = find_maija_positions(n, q, flows, queries);
for (long long res : results) {
cout << res << " ";
}
cout << "\n";
return 0;
}