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<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>
#include<numeric>
#include<cassert>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define rep(i,s,n) for(int i=(s); i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))
#define print(x) printf("%d\n",x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0,1,0,-1};
const int _dy[] = {-1,0,1,0};

int getInt(){
  int ret = 0,c;
  c = getchar();
  while(!isdigit(c)) c = getchar();
  while(isdigit(c)){
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

bool flag[11][1<<15];
int memo[11][1<<15];
int g[11][11];

int main(){
  int n;
  while(cin>>n, n){
    vector<pair<string,int> > v[n];
    map<string,int> m;
    
    REP(i,n){
      int l; cin>>l;
      v[i] = vector<pair<string,int> >(l);
      REP(j,l){
	string str;
	int mon;
	cin>>str>>mon;
	v[i][j].f = str;
	v[i][j].s = mon;

	if(m.count(str)) m[str] = min(m[str], mon);
	else m[str] = mon;
      }
    }
    int q; cin>>q;
    int costsum = 0;
    bool check = true;
    vector<string> name(q);
    vector<int> place(n+1);
    REP(i,q){
      cin>>name[i];
      if(m.count(name[i])) costsum += m[name[i]];
      else check = false;
    }

    REP(i,n+1) REP(j,(1<<q)){
      flag[i][j] = false;
      memo[i][j] = INT_MAX;
    }
    REP(i,n+1) REP(j,n+1) g[i][j] = 0;

    int mm; cin>>mm;
    REP(i,mm){
      int a,b,d; cin>>a>>b>>d;
      g[a][b] = g[b][a] = d;
    }

    REP(i,n){
      int f = 0;
      REP(j,v[i].size())
	if(m[v[i][j].f] == v[i][j].s)
	  REP(k,q)
	    if(name[k] == v[i][j].f)
	      f |= (1 << k);
      place[i+1] = f;
    }

    if(!check){
      puts("impossible");
      continue;
    }

    typedef pair<int,pair<int,int> > data;
    priority_queue<data,vector<data>,greater<data> > pq;

    pq.push(mp(0, mp(0, 0)));

    while(pq.size()){
      data d = pq.top(); pq.pop();
      int cost = d.f;
      int pos = d.s.f;
      int f = d.s.s;

      if(flag[pos][f]) continue;
      flag[pos][f] = true;
      memo[pos][f] = cost;

      if(pos == 0 && f == (1<<q)-1)
	break;

      REP(i,n+1) if(g[pos][i] != 0){
	int cc = cost + g[pos][i];
	int ff = f | place[i];

	if(!flag[i][ff] && memo[i][ff] > cc){
	  memo[i][ff] = cc;
	  pq.push(mp(cc, mp(i, ff)));
	}
      }
    }

    if(flag[0][(1<<q)-1])
      printf("%d %d\n",costsum,memo[0][(1<<q)-1]);
    else
      puts("impossible");
  }
  return 0;
}