#include <bits/stdc++.h>
#define MAX 100005
#define endl '\n'
using namespace std;
typedef long long ll;
typedef double lf;
typedef vector<ll> vi;
ll next_table[MAX], group[MAX], pos_in_group[MAX], tag_table[MAX], table_tag[MAX];
vi cycles[MAX];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
for (ll i = 0; i < n; ++i) {
ll x;
cin >> x;
cin >> next_table[--x];
next_table[x]--;
tag_table[i] = x;
table_tag[x] = i;
}
vector<bool> analizados(n);
int grupo = 0;
for (ll j = 0; j < n; ++j) {
if (analizados[j])
continue;
ll pos = j, p = 0;
while (!analizados[pos]) {
group[pos] = grupo;
pos_in_group[pos] = p++;
cycles[grupo].push_back(pos);
analizados[pos] = true;
pos = next_table[pos];
}
grupo++;
}
ll p, time;
for (ll i = 0; i < k; ++i) {
cin >> p >> time;
ll len = cycles[group[table_tag[--p]]].size();
ll q = pos_in_group[table_tag[p]];
cout << tag_table[cycles[group[table_tag[p]]][(time + q) % len]] + 1 << endl;
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CgojZGVmaW5lIE1BWCAxMDAwMDUKI2RlZmluZSBlbmRsICdcbicKCnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdHlwZWRlZiBkb3VibGUgbGY7CnR5cGVkZWYgdmVjdG9yPGxsPiB2aTsKCmxsIG5leHRfdGFibGVbTUFYXSwgZ3JvdXBbTUFYXSwgcG9zX2luX2dyb3VwW01BWF0sIHRhZ190YWJsZVtNQVhdLCB0YWJsZV90YWdbTUFYXTsKdmkgY3ljbGVzW01BWF07CgppbnQgbWFpbigpIHsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogICAgY2luLnRpZSgwKTsKCiAgICBpbnQgbiwgazsKICAgIGNpbiA+PiBuID4+IGs7CgogICAgZm9yIChsbCBpID0gMDsgaSA8IG47ICsraSkgewogICAgICAgIGxsIHg7CiAgICAgICAgY2luID4+IHg7CiAgICAgICAgY2luID4+IG5leHRfdGFibGVbLS14XTsKICAgICAgICBuZXh0X3RhYmxlW3hdLS07CiAgICAgICAgdGFnX3RhYmxlW2ldID0geDsKICAgICAgICB0YWJsZV90YWdbeF0gPSBpOwogICAgfQogICAgdmVjdG9yPGJvb2w+IGFuYWxpemFkb3Mobik7CiAgICBpbnQgZ3J1cG8gPSAwOwogICAgZm9yIChsbCBqID0gMDsgaiA8IG47ICsraikgewogICAgICAgIGlmIChhbmFsaXphZG9zW2pdKQogICAgICAgICAgICBjb250aW51ZTsKICAgICAgICBsbCBwb3MgPSBqLCBwID0gMDsKICAgICAgICB3aGlsZSAoIWFuYWxpemFkb3NbcG9zXSkgewogICAgICAgICAgICBncm91cFtwb3NdID0gZ3J1cG87CiAgICAgICAgICAgIHBvc19pbl9ncm91cFtwb3NdID0gcCsrOwogICAgICAgICAgICBjeWNsZXNbZ3J1cG9dLnB1c2hfYmFjayhwb3MpOwogICAgICAgICAgICBhbmFsaXphZG9zW3Bvc10gPSB0cnVlOwogICAgICAgICAgICBwb3MgPSBuZXh0X3RhYmxlW3Bvc107CiAgICAgICAgfQogICAgICAgIGdydXBvKys7CiAgICB9CgogICAgbGwgcCwgdGltZTsKICAgIGZvciAobGwgaSA9IDA7IGkgPCBrOyArK2kpIHsKICAgICAgICBjaW4gPj4gcCA+PiB0aW1lOwoKICAgICAgICBsbCBsZW4gPSBjeWNsZXNbZ3JvdXBbdGFibGVfdGFnWy0tcF1dXS5zaXplKCk7CiAgICAgICAgbGwgcSA9IHBvc19pbl9ncm91cFt0YWJsZV90YWdbcF1dOwogICAgICAgIGNvdXQgPDwgdGFnX3RhYmxlW2N5Y2xlc1tncm91cFt0YWJsZV90YWdbcF1dXVsodGltZSArIHEpICUgbGVuXV0gKyAxIDw8IGVuZGw7CiAgICB9Cn0=