|
#include <iostream>
using namespace std;

const int maxn=205;
int qun[maxn];
char res[maxn];
char str[maxn];
int t[maxn];
int n;
void getT()
  {
int i,j;
for(i=1;i<=n;i++)
 {
if(t[i]==0)
 {
int cnt=1;
int top=-1;
int same[maxn];
int pos=qun[i];
while(pos!=i)
 {
same[++top]=pos;
pos=qun[pos];
cnt++;
}
t[i]=cnt;
for(j=0;j<=top;j++)
t[same[j]]=cnt;
}
}
}
int main()
  {

while(scanf("%d",&n)!=EOF&&n)
 {
int i,j;
for(i=1;i<=n;i++)
scanf("%d",&qun[i]);
memset(t,0,sizeof(t));
getT();
int k;
while(scanf("%d",&k)!=EOF&&k)
 {
memset(res,'\0',sizeof(res));
memset(str,'\0',sizeof(str));
gets(str);
for(i=1;i<=n;i++)
 {
int tmp=i;
for(j=0;j<k%t[i];j++)
tmp=qun[tmp];
if(str[i]=='\0') res[tmp]=' ';
else res[tmp]=str[i];
}
printf("%s\n",res+1);
}
printf("\n");
}
return 0;
}
|