#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
typedef long long LL;
const int maxn=300+5;
const LL inf=1000000000000;
char to[maxn];
struct HeapNode{
LL d;
int u;
bool operator < (const HeapNode &rhs) const {
return d>rhs.d;
}
};
struct edge{
int from,to,dist;
};
struct Dijkstra{
int n,m;
vector<edge> edges;
vector<int> G[maxn];
bool done[maxn];
LL d[maxn];
int p[maxn];
void init(int n){
this->n=n;
for(int i=0;i<n;i++) G[i].clear();
edges.clear();
}
void addedge(int from,int to,int dist){
edges.push_back((edge){from,to,dist});
m=(int)edges.size();
G[from].push_back(m-1);
}
void print(int e){
if(p[e]==-1){
printf("%c\n",to[e]);
return ;
}
printf("%c-",to[e]);
print(edges[p[e]].from);
}
void dijkstra(int s,LL val){
priority_queue<HeapNode> pq;
for(int i=0;i<n;i++) d[i]=inf;
d[s]=val;
p[s]=-1;
memset(done,0,sizeof(done));
pq.push((HeapNode){val,s});
while(!pq.empty()){
HeapNode x=pq.top();pq.pop();
int u=x.u;
if(done[u]) continue;
done[u]=true;
for(int i=0;i<G[u].size();i++){
edge &e=edges[G[u][i]];
LL tmp;
if(e.dist) tmp=(LL)ceil(d[u]*1.0/19*20);
else tmp=d[u]+1;
if(d[e.to]>tmp||(d[e.to]==tmp&&to[u]<to[edges[p[e.to]].from])){
d[e.to]=tmp;
p[e.to]=G[u][i];
pq.push((HeapNode){d[e.to],e.to});
}
}
}
}
};
Dijkstra dij;
int n;
int vis[maxn];
int main()
{
int kase=0;
for(int i=0;i<26;i++){
vis['A'+i]=i;
to[i]='A'+i;
}
for(int i=0;i<26;i++){
vis['a'+i]=i+26;
to[i+26]='a'+i;
}
while(scanf("%d",&n)&&n!=-1){
dij.init(52);
char a[5],b[5];
LL dist;
for(int i=0;i<n;i++){
scanf("%s%s",a,b);
int u=vis[a[0]],v=vis[b[0]];
dij.addedge(u,v,a[0]<'a');
dij.addedge(v,u,b[0]<'a');
}
scanf("%lld%s%s",&dist,a,b);
int u=vis[a[0]],v=vis[b[0]];
dij.dijkstra(v,dist);
printf("Case %d:\n",++kase);
printf("%lld\n",dij.d[u]);
dij.print(u);
}
return 0;
}