#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
int tab[100];
for (int i=0; i<n; i++){
cin>>tab[i];
}
int k,x=0;
cin>>k;
while(k--){
cout<<tab[x]<<" ";
x=tab[x]-1;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG1haW4oKSB7CgkKCWludCBuOwoJY2luPj5uOwoJCglpbnQgdGFiWzEwMF07CgkKCWZvciAoaW50IGk9MDsgaTxuOyBpKyspewoJCWNpbj4+dGFiW2ldOwoJfQoJCglpbnQgayx4PTA7CgljaW4+Pms7CgkKCXdoaWxlKGstLSl7CgkJY291dDw8dGFiW3hdPDwiICI7CgkJeD10YWJbeF0tMTsKCX0KCglyZXR1cm4gMDsKfQ==