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"
using namespace std;

struct shop {
	int id;
	vector<int>prices;
};
struct Edge {
	int from;
	int to;
	int cost;
};
struct aa {
	int now;
	bitset<11>came;
	int cost;
};

class Compare {
public:
	bool operator()(const aa&l, const aa&r) {
		return l.cost > r.cost;
	}
};
int N, Q;
int solve(const vector<vector<Edge>>&edges, const bitset<11>&fst_came) {
	vector<vector<int>>memo(N + 1, vector<int>(1 << (N + 1), 1e9));
	priority_queue<aa, vector<aa>, Compare>que;
	que.push(aa{ 0,fst_came,0 });
	while (!que.empty()) {
		aa atop(que.top());
		que.pop();
		if (atop.now == 0 && atop.came.count() == N + 1)return atop.cost;
		else {
			for (auto e : edges[atop.now]) {
				bitset<11>nextcame(atop.came);
				nextcame[e.to] = true;
				const int nextcost = atop.cost + e.cost;
				if (memo[e.to][nextcame.to_ulong()] > nextcost) {
					memo[e.to][nextcame.to_ulong()] = nextcost;
					que.push(aa{ e.to,nextcame,nextcost });
				}
			}
		}
	}
	return 1e9;
}

int main() {
	while (1) { cin >> N;
		if (!N)break;
		vector<shop>shops;
		{
			map<string, int>mp;
			int num = 0;
			vector<vector<int>>sells(N,vector<int>(2000,1e7));
			for (int i = 0; i < N; ++i) {
				int k; cin >> k;
				shop ash;
				ash.id = i;
				for (int j = 0; j < k; ++j) {
					string st; cin >> st;
					int price; cin >> price;
					if (mp.find(st) == mp.end())mp[st] = num++;
					sells[i][mp[st]] = min(sells[i][mp[st]],price);
				}
			}
			 cin >> Q;
			vector<int>needs(Q);
			for (int i = 0; i < Q; ++i) {
				string st; cin >> st;
				if (mp.find(st) == mp.end())mp[st] = num++;
				needs[i] = mp[st];
			}
			for (int i = 0; i < N; ++i) {
				shop sh;
				sh.id = i;
				for (int j = 0; j < Q; ++j) {
					sh.prices.push_back(sells[i][needs[j]]);
				}
				shops.push_back(sh);
			}
		}
		vector<vector<Edge>>edges(N+1);
		{
			int M; cin >> M;
			for (int i = 0; i < M; ++i) {
				int s, t, d; cin >> s >> t >> d;
				edges[s].push_back(Edge{ s,t,d });
				edges[t].push_back(Edge{ t,s,d });
			}
		}
		pair<int, int>ans(1e7,1e8);
		for (int b = 0; b < (1 << N); ++b) {
			bitset<10>bs(b);
			int price_sum = 0;
			int aprice = 1e8;
			for (int j = 0; j < Q; ++j) {
				int aprice = 1e8;
				for (int i = 0; i < N; ++i) {
					if (bs[i])aprice = min(aprice, shops[i].prices[j]);
				}
				price_sum += aprice;
			}
			if (ans.first < price_sum)continue;
			else {
				bitset<11>came;
				came[0] = true;
				for (int i = 0; i < N; ++i) {
					if (!bs[i])came[i + 1] = true;
				}
				int cost = solve(edges, came);
				if (cost > 5e8)continue;
				else {
					ans = min(ans, make_pair(price_sum, cost));
				}
			}
		}
		if (ans.first > 6e6) {
			cout << "impossible" << endl;
		}
		else {
			cout << ans.first << " " << ans.second << endl;
		}
		
	}
	return 0;
}