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<bits/stdc++.h>

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define IINF (INT_MAX)
#define MAX_N 12
#define MAX_Q 16
#define MAX_K 101

using namespace std;

typedef pair<string,int> si;

typedef unsigned long long ull;

struct Edge{
  int to,dist;
  Edge(int to=IINF,int dist=IINF):to(to),dist(dist){}
};

struct Data{
  int cur,dist;
  ull bitmask;
  Data(int cur=IINF,int dist=IINF,ull bitmask=0):cur(cur),dist(dist),bitmask(bitmask){}
  bool operator < (const Data& data)const{ return dist > data.dist; }
};

const string N = "impossible";

int n,q,m,k[MAX_N];
si input_tmp[MAX_N][MAX_K];
int min_value[MAX_Q],min_cost,mindist[MAX_N][(1<<MAX_Q)];
vector<Edge> G[MAX_N];
ull supermarket_merchandise[MAX_N];
bool error;

inline void input(){

  rep(i,n){
    cin >> k[i];
    rep(j,k[i])cin >> input_tmp[i][j].first >> input_tmp[i][j].second;
  }

  map<string,int> getIndex;
  string line;
  int dex = 0;
  cin >> q;
  rep(i,q){
    cin >> line;
    if( getIndex.find(line) == getIndex.end() ) getIndex[line] = dex++;
    min_value[i] = IINF;
  }

  rep(i,n)rep(j,k[i]){
    if( getIndex.find(input_tmp[i][j].first) == getIndex.end() )continue;
    int index = getIndex[input_tmp[i][j].first];
    min_value[index] = min(min_value[index],input_tmp[i][j].second);
  }

  min_cost = 0;
  rep(i,dex){
    if( min_value[i] == IINF ) error = true;
    min_cost += min_value[i];
  }
  rep(i,n+1)G[i].clear(),supermarket_merchandise[i] = 0ULL;

  rep(i,n)rep(j,k[i]){
    if( getIndex.find(input_tmp[i][j].first) == getIndex.end() )continue;
    int index = getIndex[input_tmp[i][j].first];
    if( min_value[index] != input_tmp[i][j].second ) continue;
    supermarket_merchandise[i+1] |= (1<<index);
  }

  cin >> m;
  rep(i,m){
    int s,t,d;
    cin >> s >> t >> d;
    G[s].push_back(Edge(t,d));
    G[t].push_back(Edge(s,d));
  }

}

inline void dijkstra(){

  rep(i,n+1)rep(j,(1<<q))mindist[i][j] = IINF;
  mindist[0][0] = 0;
  priority_queue<Data> Q;
  Q.push(Data(0,0,0));
  while(!Q.empty()){

    Data data = Q.top(); Q.pop();
    int cur = data.cur;
    ull c_bitmask = data.bitmask;

    if( mindist[cur][c_bitmask] < data.dist ) continue;

    rep(i,G[cur].size()){
      int next = G[cur][i].to, next_dist = G[cur][i].dist + data.dist;
      ull next_bitmask = c_bitmask | supermarket_merchandise[next];
      if( mindist[next][next_bitmask] > next_dist ){
	mindist[next][next_bitmask] = next_dist;
	Q.push(Data(next,next_dist,next_bitmask));
      }
    }

  }

  if( mindist[0][(1<<q)-1] == IINF ) cout << N << endl;
  else                               cout << min_cost << ' ' << mindist[0][(1<<q)-1] << endl;

}

int main(){
  while(cin>>n,n){
    error = false;
    input();
    if( error ){
      cout << N << endl;
      continue;
    }
    dijkstra();
  }
  return 0;
}