#include <iostream>
#include <string>
using namespace std;
int an[]={1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,4,1,2,3,1,2,3,4};//抄博友
int hao[]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9};
int main()
{
int T;
cin>>T;
for(int o=0;o<T;o++)
{
int a,b;
cin>>a>>b;
string s;
getline(cin,s);
getline(cin,s);
//cout<<s<<endl;
int jg=0;
int flag=0;
for(int i=0;i<s.size();i++)
{
if(s[i]!=' ')
{
jg=jg+an[s[i]-'A']*a;
if(flag==hao[s[i]-'A'])
{
jg=jg+b;
}
flag=hao[s[i]-'A'];
}else
{
jg=jg+a;
flag=0;
}
//cout<<jg<<endl;
}
cout<<jg<<endl;
}
return 0;
}