#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#define int long long
#define ui unsigned int
using namespace std;
typedef int LL;
const LL N = 3e5 + 10;
const LL mod = 998244353;
inline LL read(){
LL x=0,f=1;
char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-1;
for(; isdigit(ch);ch=getchar()) x=(x<<3)+(x<<1)+(ch^48);
return x*f;
}
ui s;
inline ui get(ui x) {
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
return s = x;
}
struct Edge{
int to,next;
}edge[N<<1];
int head[N],idx;
void add(int a,int b){
edge[++idx].to = b , edge[idx].next = head[a] , head[a] = idx;
}
int fa[N],siz[N],top[N],depth[N],son[N],dfn[N],id[N],cnt;
void dfs1(int u,int fath){
fa[u] = fath , depth[u] = depth[fath] + 1 , siz[u] = 1;
for(int i=head[u];i;i=edge[i].next){
int v = edge[i].to;
if(v == fath) continue;
dfs1(v,u);
siz[u] += siz[v];
if(siz[son[u]] < siz[v]) son[u] = v;
}
}
void dfs2(int u,int topf){
top[u] = topf;
dfn[u] = ++cnt;
id[dfn[u]] = u;
if(!son[u]) return;
dfs2(son[u],topf);
for(int i=head[u];i;i=edge[i].next){
int v = edge[i].to;
if(v == fa[u] || v == son[u]) continue;
dfs2(v,v);
}
}
int ans[N];
int find(int u,int k){
k = depth[u] - k;
while(depth[top[u]] > k){
u = fa[top[u]];
}
return id[dfn[u] - (depth[u] - k)];
}
signed main(){
LL n = read() , q = read();
scanf("%u",&s);
for(int i=1;i<=n;i++){
int x = read();
fa[i] = x;
if(x == 0) s = i;
else{
add(x,i);add(i,x);
}
}
dfs1(1,0);
dfs2(1,0);
LL ans = 0;
int res = 0;
for(int i=1;i<=q;i++){
int x = ((get(s) ^ res) % n) + 1;
int k = (get(s) ^ res) % depth[x];
cout<<"Case "<<i<<": "<<x<<" "<<k<<endl;
res = find(x,k);
cout<<res<<endl;
ans ^= (i * res);
}
printf("%d\n",ans);
return 0;
}