#include <stdio.h>
#include <string.h>
using namespace std;
char best[105];
int Max;
struct Node
{
int p;
Node* next[26];
Node(int prob)
{
p=prob;
for(int i=0;i<26;i++)
{
next[i]=NULL;
}
}
};
void AddNode(Node* root, char* p, int prob)
{
//printf("%s\n", p);
int index;
Node* np=root;
for(;*p!='\0';p++)
{
index=*p-'a';
if(np->next[index]!=NULL)
{
np->next[index]->p+=prob;
}
else
{
Node* newp=new Node(index);
np->next[index]=newp;
np->next[index]->p=prob;
}
np=np->next[index];
}
}
int num2char(int n, int &cnt)
{
switch(n)
{
case 2: cnt=3; return 'a'-'a';
case 3: cnt=3; return 'd'-'a';
case 4: cnt=3; return 'g'-'a';
case 5: cnt=3; return 'j'-'a';
case 6: cnt=3; return 'm'-'a';
case 7: cnt=4; return 'p'-'a';
case 8: cnt=3; return 't'-'a';
case 9: cnt=4; return 'w'-'a';
}
}
void dfs(char* text, int len, int dep, Node* np, int prob, char cur[105], bool &flag)
{
int i,cnt,num;
int ch;
if(dep==len)
{
if(prob > Max)
{
Max=prob;
strcpy(best, cur);
flag = true;
}
return;
}
num=text[dep]-'0';
if(num==0 || num==1)
{
return;
}
ch=num2char(num, cnt);
for(i=ch;i<ch+cnt;i++)
{
if(np->next[i]!=NULL)
{
for(int j=0;;j++)
{
if(cur[j]=='\0')
{
cur[j]=i+'a';
cur[j+1]='\0';
break;
}
}
dfs(text, len, dep+1, np->next[i], np->next[i]->p, cur, flag );
for(int j=0;;j++)
{
if(cur[j]=='\0' && j>0)
{
cur[j-1]='\0';
break;
}
}
}
}
return;
}
void Parse(Node* root, char *p, int len)
{
bool flag=false;
int i,num,cnt;
char ch, text[105],cur[105];
cur[0]='\0';
for(i=0;i<len-1;i++)
{
strncpy(text,p,i+1);
flag=false;
Max=0;
dfs(text, i+1, 0, root, 0, cur, flag);
if( flag )
{
printf("%s\n", best);
}
else
{
printf("MANUALLY\n");
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
int T,i,m,n,k,prob;
char word[105],oper[105];
scanf("%d", &T);
for(k=1;k<=T;k++)
{
printf("Scenario #%d:\n", k);
Node* root=new Node(0);
scanf("%d", &n);
for(i=0;i<n;i++)
{
scanf("%s %d", word, &prob);
AddNode(root, word, prob);
}
char cur[105];
scanf("%d", &m);
for(i=0;i<m;i++)
{
cur[0]='\0';
scanf("%s", oper);
Parse(root, oper, strlen(oper));
printf("\n");
}
printf("\n");
}
//while(1);
}
#include <string.h>
using namespace std;
char best[105];
int Max;
struct Node
{
int p;
Node* next[26];
Node(int prob)
{
p=prob;
for(int i=0;i<26;i++)
{
next[i]=NULL;
}
}
};
void AddNode(Node* root, char* p, int prob)
{
//printf("%s\n", p);
int index;
Node* np=root;
for(;*p!='\0';p++)
{
index=*p-'a';
if(np->next[index]!=NULL)
{
np->next[index]->p+=prob;
}
else
{
Node* newp=new Node(index);
np->next[index]=newp;
np->next[index]->p=prob;
}
np=np->next[index];
}
}
int num2char(int n, int &cnt)
{
switch(n)
{
case 2: cnt=3; return 'a'-'a';
case 3: cnt=3; return 'd'-'a';
case 4: cnt=3; return 'g'-'a';
case 5: cnt=3; return 'j'-'a';
case 6: cnt=3; return 'm'-'a';
case 7: cnt=4; return 'p'-'a';
case 8: cnt=3; return 't'-'a';
case 9: cnt=4; return 'w'-'a';
}
}
void dfs(char* text, int len, int dep, Node* np, int prob, char cur[105], bool &flag)
{
int i,cnt,num;
int ch;
if(dep==len)
{
if(prob > Max)
{
Max=prob;
strcpy(best, cur);
flag = true;
}
return;
}
num=text[dep]-'0';
if(num==0 || num==1)
{
return;
}
ch=num2char(num, cnt);
for(i=ch;i<ch+cnt;i++)
{
if(np->next[i]!=NULL)
{
for(int j=0;;j++)
{
if(cur[j]=='\0')
{
cur[j]=i+'a';
cur[j+1]='\0';
break;
}
}
dfs(text, len, dep+1, np->next[i], np->next[i]->p, cur, flag );
for(int j=0;;j++)
{
if(cur[j]=='\0' && j>0)
{
cur[j-1]='\0';
break;
}
}
}
}
return;
}
void Parse(Node* root, char *p, int len)
{
bool flag=false;
int i,num,cnt;
char ch, text[105],cur[105];
cur[0]='\0';
for(i=0;i<len-1;i++)
{
strncpy(text,p,i+1);
flag=false;
Max=0;
dfs(text, i+1, 0, root, 0, cur, flag);
if( flag )
{
printf("%s\n", best);
}
else
{
printf("MANUALLY\n");
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
int T,i,m,n,k,prob;
char word[105],oper[105];
scanf("%d", &T);
for(k=1;k<=T;k++)
{
printf("Scenario #%d:\n", k);
Node* root=new Node(0);
scanf("%d", &n);
for(i=0;i<n;i++)
{
scanf("%s %d", word, &prob);
AddNode(root, word, prob);
}
char cur[105];
scanf("%d", &m);
for(i=0;i<m;i++)
{
cur[0]='\0';
scanf("%s", oper);
Parse(root, oper, strlen(oper));
printf("\n");
}
printf("\n");
}
//while(1);
}