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 <fstream>
#include <sstream>
#include <iomanip> 
#include <complex> 
#include <string>
#include <vector> 
#include <list>
#include <deque> 
#include <stack> 
#include <queue> 
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm> 
#include <numeric> 
#include <typeinfo> 
#include <cstdio>
#include <cstdlib> 
#include <cstring>
#include <cmath>
#include <climits> 
#include <ctime>
using namespace std;

typedef pair<string,int> P;
int n,q,m;

vector<P> v[11];
vector<string> name;
map<string,int> sale;
int root[11][11];
const int INF=10000000;
int dpc[11][1<<15];
int dpd[11][1<<15];
int ac;
int ad;

void rec(int x,int bit,int c,int d){
	if(dpc[x][bit]<c)return;
	if(dpc[x][bit]==c && dpd[x][bit]<d)return;
	dpc[x][bit]=c;
	dpd[x][bit]=d;
	int nbit=bit;
	int nc=c;
	for(int i=0;i<v[x].size();i++){
		for(int j=0;j<name.size();j++){
			if(name[j]==v[x][i].first && !(bit >> j & 1) && sale[name[j]]==v[x][i].second){
				nbit |= 1<<j;
				nc+=v[x][i].second;
			}
		}
	}
	if(nbit==(1<<q)-1){
		if(nc<ac || (nc==ac && d+root[0][x]<ad)){
			ac=nc;
			ad=d+root[0][x];
		}
	}else{
		for(int i=1;i<=n;i++){
			int nd=d+root[i][x];
			if(dpc[i][nbit]>nc || (dpc[i][nbit]==nc && dpd[i][nbit]>nd)){
				dpc[i][nbit]=nc;
				dpd[i][nbit]=nd;
				rec(i,nbit,nc,nd);
			}
		}
	}
}


int main(void){
	while(1){
		ac=INF;
		ad=INF;
		bool ch=true;
		for(int i=0;i<=10;i++){
			for(int j=0;j<=10;j++){
				root[i][j]=INF;
			}
			root[i][i]=0;
			v[i].clear();
		}
		for(int i=0;i<=10;i++){
			for(int j=0;j<=(1<<15)-1;j++){
				dpc[i][j]=INF;
				dpd[i][j]=INF;
			}
		}
		name.clear();
		sale.clear();
		cin >> n;
		if(n==0)break;
		for(int i=1;i<=n;i++){
			int k;
			cin >> k;
			for(int j=0;j<k;j++){
				string str;
				int cost;
				cin >> str >> cost;
				v[i].push_back(P(str,cost));
				if(sale[str]==0)sale[str]=cost;
				else sale[str]=min(cost,sale[str]);
			}
		}
		cin >> q;
		for(int i=0;i<q;i++){
			string str;
			cin >> str;
			name.push_back(str);
			if(sale[str]==0)ch=false;
		}
		cin >> m;
		for(int i=0;i<m;i++){
			int s,t,d;
			cin >> s >> t >> d;
			root[s][t]=min(d,root[s][t]);
			root[t][s]=min(d,root[t][s]);
		}
		for(int k=0;k<=n;k++){
			for(int i=0;i<=n;i++){
				for(int j=0;j<=n;j++){
					root[i][j]=min(root[i][j],root[i][k]+root[k][j]);
				}
			}
		}
		if(ch)rec(0,0,0,0);
		if(ac>=INF || ch==false)cout << "impossible" << endl;
		else cout << ac << " " << ad << endl;
	}
    return 0;
}