/****************************************************************************************************
去年天津区预赛水题,刚开始没想法,后来想应该是直接dfs就行了,然后就写了这个屎一样的代码。。。写得真难看啊。。。
****************************************************************************************************/
#include <iostream>
#include <functional>
#include <algorithm>
//#include <hash_map> //Visual C++ lib
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
using namespace std;
#define LOWBIT(x) ( (x) & ( (x) ^ ( (x) - 1 ) ) )
#define CLR(x, k) memset((x), (k), sizeof(x))
#define CPY(t, s) memcpy((t), (s), sizeof(s))
#define LEN(s) static_cast<int>( strlen((s)) )
#define SZ(s) static_cast<int>( (s).sizeof() )
typedef double LF;
typedef long long LL; //GNU C++
typedef unsigned long long ULL;
//typedef __int64 LL; //Visual C++ 2010
//typedef unsigned __int64 ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PDD;
//typedef hash_map<int, int>::iterator HMI;
typedef map<int, int>::iterator MI;
typedef vector<int>::iterator VI;
typedef list<int>::iterator LI;
typedef set<int>::iterator SI;
const int INF_INT = 0x3f3f3f3f;
const LL INF_LL = 0x7fffffffffffffffLL; //15f
const double oo = 10e9;
const double eps = 10e-9;
const double PI = acos(-1.0);
const int MAXP = 23;
struct Player
{
string name, pos;
int key;
}player[MAXP];
map<string, int>NP;
map<string, int>SP;
int pcnt[6], flag[6], maze[MAXP][MAXP];
bool sign[MAXP];
bool inline cmp(const Player &pa, const Player &pb)
{
return SP[ pa.pos ] < SP[ pb.pos ];
}
void preprocess()
{
SP.clear();
SP.insert( pair<string, int>("goalkeeper", 0) );
SP.insert( pair<string, int>("defender", 1) );
SP.insert( pair<string, int>("midfielder", 2) );
SP.insert( pair<string, int>("striker", 3) );
return ;
}
int maxi;
void dfs(int crt, int state, int tot, int gk_cnt, int df_cnt, int mf_cnt, int sk_cnt)
{
if (0 == state && gk_cnt >= 1)
{
crt = flag[state] - 1;
++state;
}
if (1 == state && df_cnt >= 4)
{
crt = flag[state] - 1;
++state;
}
if (2 == state && mf_cnt >= 4)
{
crt = flag[state] - 1;
++state;
}
if (3 == state && sk_cnt >= 2)
{
crt = flag[state] - 1;
++state;
}
if (4 == state)
{
maxi = max(maxi, tot);
return ;
}
for (int nxt = crt + 1; nxt < flag[state]; ++nxt)
{
int buf = tot + player[nxt].key;
for (int ind = 0; ind < nxt; ++ind)
{
if (sign[ind])
{
buf += maze[ind][nxt];
}
}
sign[nxt] = true;
dfs(nxt, state, buf, 0 == state ? gk_cnt + 1 : gk_cnt, 1 == state ? df_cnt + 1 : df_cnt, 2 == state ? mf_cnt + 1 : mf_cnt, 3 == state ? sk_cnt + 1 : sk_cnt);
sign[nxt] = false;
}
return ;
}
void ace()
{
int m, val;
string na, nb;
while (cin >> player[0].name >> player[0].key >> player[0].pos)
{
for (int i = 1; i < 23; ++i)
{
cin >> player[i].name >> player[i].key >> player[i].pos;
}
sort(player, player + 23, cmp);
NP.clear();
CLR(pcnt, 0);
for (int i = 0; i < 23; ++i)
{
++pcnt[ SP[ player[i].pos ] ];
NP.insert( pair<string, int>(player[i].name, i) );
}
CLR(maze, 0);
for (cin >> m; m--; )
{
cin >> na >> nb >> val;
maze[ NP[na] ][ NP[nb] ] = maze[ NP[nb] ][ NP[na] ] = val;
}
if (pcnt[0] < 1 || pcnt[1] < 4 || pcnt[2] < 4 || pcnt[3] < 2)
{
puts("impossible");
continue ;
}
flag[0] = pcnt[0];
for (int i = 1; i < 4; ++i)
{
flag[i] = flag[i - 1] + pcnt[i];
}
CLR(sign, false);
maxi = -INF_INT;
dfs(-1, 0, 0, 0, 0, 0, 0);
printf("%d\n", maxi);
}
return ;
}
int main()
{
preprocess();
ace();
return 0;
}