#include <stdio.h>
#include<string.h>
int main(){
int T;
scanf("%d", &T);
for(int i = 0; i < T; i++){
int N;
scanf("%d", &N);
char NAMA[N][20];
int NILAI[N];
for(int j = 0; j < N; j++){
scanf("%[^#]#%d\n", NAMA[j], &NILAI[j]);
}
char ty[20];
scanf("%s", ty);
int res = 0;
for(int j = 0; j < N; j++){
if(strcmp(NAMA[j], ty) == 0){
res = j;
}
}
int rank = 1;
for(int j = 0; j < N; j++){
if(NILAI[j] > NILAI[res]){
rank++;
}
}
printf("Case #%d: %d\n", i+1, rank);
}
return 0;
}