int main() { int n; while(cin>>n && n) { map<string,bool> sw; string s,abr; for(int i=0;i<n;i++){ cin>>s; sw[s]=1; //mark insignificant words } getline(cin,s); while(getline(cin,s) && s!="LAST CASE") { w.clear(); stringstream ss; ss<<s; ss>>a; abr=a; for(int i=0;i<a.size();i++) a[i]=tolower(a[i]); while(ss>>s) if(sw.find(s)==sw.end()) w.PB(s); memset(dp,-1,sizeof(dp)); int res=solve(0,0,0); if(res==0) cout<<abr<<" is not a valid abbreviation"<<endl; else cout<<abr<<" can be formed in "<<res<<" ways"<<endl; } } }