Problem: Kotoko, a housewife, was enthusiastic about keeping food costs down during this recession. Be sure to check the newspaper advertisement every morning. Make a good list of what to buy and the cheapest-selling shops, and go shopping by riding a bicycle in a combat style called apron sandals while walking through the shops.

So, as a husband, you decided to create a housekeeping book program with your favorite programming in order to help Kotoko even a little. The program inputs the name and price (yen) of the items sold to each supermarket, the items that Kotoko needs, and outputs the minimum amount to collect all the items.

Kotoko has to leave the house, collect the necessary items, and return home. Therefore, you who care about Kotoko decided to add a function to report the distance of the route with the shorter distance, considering that there are multiple shopping routes with the same minimum amount. Therefore, the program also inputs information on the road connecting the supermarket and the house.

Let the number of supers be n, and each super is assigned a number from 1 to n. Furthermore, Kotoko's house is number 0. Road information is given by these number pairs and their distances (integers).



Input

Multiple datasets are given as input. The format of each dataset is as follows:

n (number of supermarkets: integer)
k1 name1 value1 name2 value2 ... namek1 valuek1 (number of item types in the first supermarket, first item name and price, second item name and price ,,,: integer and string blanks Separation)
k2 name1 value1 name2 value2 ... namek2 valuek2 (number of item types in the second supermarket, first item name and price, second item name and price ,,,: integer and string blanks Separation)
..
..
kn name1 value1 name2 value2 ... namekn valuekn (number of item types in the nth supermarket, first item name and price, second item name and price ,,,: integer and string blanks Separation)
q (Number of items required: Integer)
name1 (Name of the first required item: string)
name2 (name of the second required item: string)
..
..
nameq (name of the qth required item: string)
m (number of roads: integer)
s1 t1 d1 (Information on the first road: Blank-separated integers)
s2 t2 d2 (Second road information: space-separated integer)
..
..
sm tm dm (mth road information: blank-separated integer)


si ti di indicates that the sith supermarket (or house) and the tith supermarket (or house) can move back and forth in both directions, and the distance is di.

You will be given a way to get to all the supermarkets from your house (via the supermarket).

n is less than or equal to 10 and q is less than or equal to 15. ki is 100 or less, the string of the item name does not exceed 20 characters, and the price of the item does not exceed 10000. Also, the length of the road does not exceed 1000.

When n is 0, it is the end of input.

Output

For each dataset, separate the minimum amount and distance with a single space and output on one line. However, if you cannot collect all the necessary items, output "impossible".

Example

Input

3
3 apple 100 banana 200 egg 300
3 apple 150 banana 100 cola 200
3 apple 100 banana 150 cola 200
3
apple
banana
cola
5
0 2 4
0 1 3
0 3 3
1 2 3
2 3 5
3
3 apple 100 banana 200 egg 300
3 apple 150 banana 100 cola 200
3 apple 100 banana 150 cola 200
4
apple
banana
cola
jump
5
0 2 4
0 1 3
0 3 3
1 2 3
2 3 5
0


Output

400 10
impossible

Solution (in CPP):

Solution:
#include<iostream>
#include<vector>
#include<map>
#include<numeric>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)
const int N = 11;
const int inf = (1<<20);

template <class T> int getindex(T &a,map<T,int> & M){
  int index = M.size();
  if (M.find(a) == M.end())M[a]=index;
  return M[a];
}

int g[N][N];
int dp[(1<<N)][N];
void tsp(int n){
  rep(i,(1<<n))rep(j,n)dp[i][j]=inf;
  dp[1<<0][0]=0;
  rep(i,(1<<n)){
    rep(j,n){//visited
      if (((1<<j)&i) == 0)continue;
      rep(k,n){//unvisited
	if ((1<<k)&i)continue;
	int next=(1<<k)|i;
	dp[next][k]=min(dp[next][k],dp[i][j]+g[j][k]);
      }
    }
  }
}

void solve(int n,map<int,int> *c,vector<int> &req){
  int ac=inf,aw=inf;
  rep(i,(1<<n)){
    if ((1&i) == 0)continue;
    int tc=inf,tw=inf;
    vector<int> mini(req.size(),inf);
    rep(j,n){
      if (((1<<j)&i) == 0)continue;
      tw=min(tw,dp[i][j]+g[j][0]);
      rep(k,(int)req.size()){
	if (c[j].find(req[k]) != c[j].end())mini[k]=min(mini[k],c[j][req[k]]);
      }
      tc = accumulate(mini.begin(),mini.end(),0);
    }
    if (tc < ac || (tc == ac && tw < aw))ac=tc,aw=tw;
  }
  if (ac == inf)cout <<"impossible"<<endl;
  else cout << ac <<" " << aw << endl;
}

int main(){
  int n;
  while(cin>>n && n){
    rep(i,n+1){rep(j,n+1)g[i][j]=inf;g[i][i]=0;}
    map<string,int> M;
    map<int,int> c[n+1];
    rep(i,n){
      int num;cin>>num;
      rep(j,num){
	string name;int cost,ind;
	cin>>name>>cost;
	ind=getindex(name,M);
	c[i+1][ind]=cost;
      }
    }

    int req;
    cin>>req;
    vector<int> in(req);
    rep(i,req){
      string tmp;
      cin>>tmp;
      in[i]=getindex(tmp,M);
    }
    
    int m;
    cin>>m;
    rep(i,m){
      int f,t,c;cin>>f>>t>>c;
      g[f][t]=g[t][f]=c;
    }
    rep(k,n+1)rep(i,n+1)rep(j,n+1)g[i][j]=min(g[i][j],g[i][k]+g[k][j]);

    tsp(n+1);

    solve(n+1,c,in);
  }
}