#include<bits/stdc++.h>
#define llu unsigned long long
#define ll long long
#define pi 3.141592
#define nl printf("\n")
#define f(i,l1,l2) for(i=l1;i<l2;i++)
#define gc getchar_unlocked
#define vi vector<int>
#define vit vi::iterator
#define all(c) c.begin(), c.end()
#define pb push_back
#define endl "\n"
using namespace std;
/*void fin(int &x) //does not compile in codeblocks but does in online judges
{ //use if input too large ( only for integers )
int i=0;x=0;
register int c=gc();
while(c<48||c>57)
c=gc();
while(c>47&&c<58)
{
x=(x<<1)+(x<<3) + c-48;
i++;
c=gc();
}
}*/
int n,m;
int a[19];
int ab[19][19];
int dp[1<<18][18][19];
map<string,int> h;
int func(int mask, int i, int k)
{
//cout<<i<<" "<<k<<endl;
if(!mask)
return 0;
int ans = dp[mask][i][k];
if(ans!=-1)
return ans;
int j;
f(j,0,n)
{
if(mask&(1<<j))
{
if(ab[i][j]!=INT_MIN)
{ans = max(ans, func(mask&(~(1<<j)),j,k+1) + (k)*a[j]);
//cout<<ans<<" "<<k<<" "<<i<<" "<<endl;
}
}
}
return (dp[mask][i][k] = ans);
}
int main()
{
//std::ios::sync_with_stdio(false);
//cin.tie(0);
int t,i,j,e;
cin>>t;
while(t--)
{
cin>>n>>m;
cin.ignore();
f(i,0,n)
{
char rr[50];
//cout<<a;
int pos;
for(int g=len-1; g>=0; g--)
{
if(rr[g]==' ')
{
pos = g;
break;
}
}
string s;
for(int g=0; g<=pos-1; g++)
s.pb(rr[g]);
int y=0;
for(int g=pos+1; g<len; g++)
{
y = y*10 + (rr[g]-'0');
}
h[s] = i;
a[i] = y;
//cout<<s<<":";
//cout<<y<<endl;
}
map<string,int> :: iterator it;
/*for(it= h.begin();it!=h.end();it++)
{
cout<<it->first<<" "<<it->second<<endl;
}*/
/*f(i,0,n)
cout<<a[i]<<" ";
cout<<endl;*/
f(i,0,19)
f(j,0,19)
ab[i][j] = 0;
f(i,0,m)
{
string s1,s2;
// string x;
// cin>>s1>>x>>s2;
char rr[100];
int hpos, apos;
for(int g=0; g<len; g++)
{
if(rr[g]=='-')
{
hpos = g;
break;
}
}
for(int g=0; g<len; g++)
{
if(rr[g]=='>')
{
apos = g;
break;
}
}
for(int g=0; g<=hpos-2; g++)
{
s1.pb(rr[g]);
}
for(int g=apos+2; g<len; g++)
{
s2.pb(rr[g]);
}
// cout<<s1<<" "<<s2<<endl;
ab[h[s1]][h[s2]] = 0;
ab[h[s2]][h[s1]] = INT_MIN;
}
/*f(i,0,19)
f(j,0,19)
if(ab[i][j] != 0)
ab[i][j] = INT_MIN;
*/
f(i,0,1<<n)
f(j,0,n)
for(int k=0;k<=n;k++)
dp[i][j][k] = -1;
int mask;
int ans = 0;
f(mask,1,1<<n)
{
f(j,0,n)
{
//int k = 1;
if(mask&(1<<j))
{
//if(mask==7)
// cout<<a[j]<<" "<<k<<(mask&(~(1<<j)))<<endl;
ans = max(ans, a[j] + func(mask&(~(1<<j)), j, 2));
}
}
}
/*f(i,0,1<<n)
{
f(j,0,n)
cout<<dp[i][j]<<" ";
cout<<endl;
}*/
cout<<ans<<endl;
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KCiNkZWZpbmUgbGx1IHVuc2lnbmVkIGxvbmcgbG9uZwojZGVmaW5lIGxsIGxvbmcgbG9uZwojZGVmaW5lIHBpIDMuMTQxNTkyCiNkZWZpbmUgbmwgcHJpbnRmKCJcbiIpCiNkZWZpbmUgZihpLGwxLGwyKSBmb3IoaT1sMTtpPGwyO2krKykKI2RlZmluZSBnYyBnZXRjaGFyX3VubG9ja2VkCiNkZWZpbmUgdmkgdmVjdG9yPGludD4KI2RlZmluZSB2aXQgdmk6Oml0ZXJhdG9yCiNkZWZpbmUgYWxsKGMpIGMuYmVnaW4oKSwgYy5lbmQoKQojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIGVuZGwgIlxuIgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKLyp2b2lkIGZpbihpbnQgJngpICAgICAgIC8vZG9lcyBub3QgY29tcGlsZSBpbiBjb2RlYmxvY2tzIGJ1dCBkb2VzIGluIG9ubGluZSBqdWRnZXMKeyAgICAgICAgICAgICAgICAgICAgICAgIC8vdXNlIGlmIGlucHV0IHRvbyBsYXJnZSAoIG9ubHkgZm9yIGludGVnZXJzICkKaW50IGk9MDt4PTA7CnJlZ2lzdGVyIGludCBjPWdjKCk7CndoaWxlKGM8NDh8fGM+NTcpCmM9Z2MoKTsKd2hpbGUoYz40NyYmYzw1OCkKewp4PSh4PDwxKSsoeDw8MykgKyBjLTQ4OwppKys7CmM9Z2MoKTsKfQp9Ki8KaW50IG4sbTsKaW50IGFbMTldOwppbnQgYWJbMTldWzE5XTsKaW50IGRwWzE8PDE4XVsxOF1bMTldOwptYXA8c3RyaW5nLGludD4gaDsKCmludCBmdW5jKGludCBtYXNrLCBpbnQgaSwgaW50IGspCnsKICAgIC8vY291dDw8aTw8IiAiPDxrPDxlbmRsOwogICAgaWYoIW1hc2spCiAgICAgICAgcmV0dXJuIDA7CiAgICBpbnQgYW5zID0gZHBbbWFza11baV1ba107CgogICAgaWYoYW5zIT0tMSkKICAgICAgcmV0dXJuIGFuczsKCiAgICBpbnQgajsKICAgIGYoaiwwLG4pCiAgICB7CiAgICAgICAgaWYobWFzayYoMTw8aikpCiAgICAgICAgewogICAgICAgICAgICBpZihhYltpXVtqXSE9SU5UX01JTikKICAgICAgICAgICAgICAgIHthbnMgPSBtYXgoYW5zLCBmdW5jKG1hc2smKH4oMTw8aikpLGosaysxKSArIChrKSphW2pdKTsKICAgICAgICAgICAgICAgIC8vY291dDw8YW5zPDwiICI8PGs8PCIgIjw8aTw8IiAiPDxlbmRsOwogICAgICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIHJldHVybiAoZHBbbWFza11baV1ba10gPSBhbnMpOwp9CgppbnQgbWFpbigpCnsKICAgIC8vc3RkOjppb3M6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICAvL2Npbi50aWUoMCk7CiAgICBpbnQgdCxpLGosZTsKICAgIGNpbj4+dDsKICAgIHdoaWxlKHQtLSkKICAgIHsKICAgICAgICBjaW4+Pm4+Pm07CiAgICAgICAgY2luLmlnbm9yZSgpOwogICAgICAgIGYoaSwwLG4pCiAgICAgICAgewoKICAgICAgICAgICAgY2hhciBycls1MF07CiAgICAgICAgICAgIGdldHMocnIpOwogICAgICAgICAgICBpbnQgbGVuID0gc3RybGVuKHJyKTsKICAgICAgICAgICAgLy9jb3V0PDxhOwogICAgICAgICAgICBpbnQgcG9zOwogICAgICAgICAgICBmb3IoaW50IGc9bGVuLTE7IGc+PTA7IGctLSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaWYocnJbZ109PScgJykKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBwb3MgPSBnOwogICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIHN0cmluZyBzOwogICAgICAgICAgICBmb3IoaW50IGc9MDsgZzw9cG9zLTE7IGcrKykKICAgICAgICAgICAgICAgIHMucGIocnJbZ10pOwogICAgICAgICAgICBpbnQgeT0wOwogICAgICAgICAgICBmb3IoaW50IGc9cG9zKzE7IGc8bGVuOyBnKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHkgPSB5KjEwICsgKHJyW2ddLScwJyk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaFtzXSA9IGk7CiAgICAgICAgICAgIGFbaV0gPSB5OwogICAgICAgICAgICAvL2NvdXQ8PHM8PCI6IjsKICAgICAgICAgICAgLy9jb3V0PDx5PDxlbmRsOwoKCiAgICAgICAgfQogICAgICAgIG1hcDxzdHJpbmcsaW50PiA6OiBpdGVyYXRvciBpdDsKICAgICAgICAvKmZvcihpdD0gaC5iZWdpbigpO2l0IT1oLmVuZCgpO2l0KyspCiAgICAgICAgewogICAgICAgICAgICBjb3V0PDxpdC0+Zmlyc3Q8PCIgIjw8aXQtPnNlY29uZDw8ZW5kbDsKICAgICAgICB9Ki8KICAgICAgICAvKmYoaSwwLG4pCiAgICAgICAgICAgIGNvdXQ8PGFbaV08PCIgIjsKICAgICAgICAgICAgY291dDw8ZW5kbDsqLwogICAgICAgIGYoaSwwLDE5KQogICAgICAgICAgICBmKGosMCwxOSkKICAgICAgICAgICAgICAgIGFiW2ldW2pdID0gMDsKCiAgICAgICAgZihpLDAsbSkKICAgICAgICB7CiAgICAgICAgICAgIHN0cmluZyBzMSxzMjsKICAgICAgICAgICAgLy8gc3RyaW5nIHg7CiAgICAgICAgICAgIC8vIGNpbj4+czE+Png+PnMyOwogICAgICAgICAgICBjaGFyIHJyWzEwMF07CiAgICAgICAgICAgIGdldHMocnIpOwogICAgICAgICAgICBpbnQgbGVuID0gc3RybGVuKHJyKTsKICAgICAgICAgICAgaW50IGhwb3MsIGFwb3M7CgogICAgICAgICAgICBmb3IoaW50IGc9MDsgZzxsZW47IGcrKykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaWYocnJbZ109PSctJykKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBocG9zID0gZzsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBmb3IoaW50IGc9MDsgZzxsZW47IGcrKykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaWYocnJbZ109PSc+JykKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBhcG9zID0gZzsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQoKICAgICAgICAgICAgZm9yKGludCBnPTA7IGc8PWhwb3MtMjsgZysrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBzMS5wYihycltnXSk7CgogICAgICAgICAgICB9CgogICAgICAgICAgICBmb3IoaW50IGc9YXBvcysyOyBnPGxlbjsgZysrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBzMi5wYihycltnXSk7CgogICAgICAgICAgICB9CgoKICAgICAgICAgICAgLy8gY291dDw8czE8PCIgIjw8czI8PGVuZGw7CiAgICAgICAgICAgIGFiW2hbczFdXVtoW3MyXV0gPSAwOwogICAgICAgICAgICBhYltoW3MyXV1baFtzMV1dID0gSU5UX01JTjsKICAgICAgICB9CgoKCiAgICAgICAgLypmKGksMCwxOSkKICAgICAgICAgICAgZihqLDAsMTkpCiAgICAgICAgICAgICAgICBpZihhYltpXVtqXSAhPSAwKQogICAgICAgICAgICAgICAgICAgIGFiW2ldW2pdID0gSU5UX01JTjsKICAgICAgICAqLwogICAgICAgIGYoaSwwLDE8PG4pCiAgICAgICAgICAgIGYoaiwwLG4pCiAgICAgICAgICAgICAgICBmb3IoaW50IGs9MDtrPD1uO2srKykKICAgICAgICAgICAgICAgICAgICBkcFtpXVtqXVtrXSA9IC0xOwoKICAgICAgICBpbnQgbWFzazsKCiAgICAgICAgaW50IGFucyA9IDA7CiAgICAgICAgZihtYXNrLDEsMTw8bikKICAgICAgICB7CgogICAgICAgICAgICBmKGosMCxuKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAvL2ludCBrID0gMTsKICAgICAgICAgICAgICAgIGlmKG1hc2smKDE8PGopKQogICAgICAgICAgICAgICAgewoKICAgICAgICAgICAgICAgICAgICAvL2lmKG1hc2s9PTcpCiAgICAgICAgICAgICAgICAgICAgLy8gIGNvdXQ8PGFbal08PCIgIjw8azw8KG1hc2smKH4oMTw8aikpKTw8ZW5kbDsKICAgICAgICAgICAgICAgICAgICBhbnMgPSBtYXgoYW5zLCBhW2pdICsgZnVuYyhtYXNrJih+KDE8PGopKSwgaiwgMikpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICAvKmYoaSwwLDE8PG4pCiAgICAgICAgewogICAgICAgICAgICBmKGosMCxuKQogICAgICAgICAgICBjb3V0PDxkcFtpXVtqXTw8IiAiOwogICAgICAgICAgICBjb3V0PDxlbmRsOwogICAgICAgIH0qLwoKCiAgICAgICAgY291dDw8YW5zPDxlbmRsOwoKICAgIH0KCiAgICByZXR1cm4gMDsKfQ==