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<string>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<set>
#include<functional>

#define MAX_ITEM 256
using namespace std;

const int infty = 1<<27;

class Node{
public:
  vector<int> con;
  vector<int> cost;
  int item_num;
  pair<int,int> shop[MAX_ITEM];
};

typedef vector<Node> Graph;

inline bool lesscost(const pair<int,int> &p1, const pair<int,int> &p2){
  if( p1.first < p2.first )
    return true;
  if( p1.first == p2.first && p1.second < p2.second )
    return true;
  return false;
}
inline bool greatercost(const pair<int,int> &p1, const pair<int,int> &p2){
  if( p1.first > p2.first )
    return true;
  if( p1.first == p2.first && p1.second > p2.second )
    return true;
  return false;
}

class State{
public:
  int now;
  int have;
  pair<int,int> cost;
  bool operator>(const State &t)const{
    return greatercost(cost,t.cost);
  }
};

pair<int,int> dijkstra(int n, Graph &G, int q, State init){
  priority_queue< State,vector<State>,greater<State> > qs;
  pair<int,int> cost[1<<q][n];
  bool vis[1<<q][n];
  
  for(int i = 0; i < (1<<q); ++i){
    for(int j = 0; j < n; ++j){
      cost[i][j] = make_pair(infty,infty);
      vis[i][j] = false;
    }
  }
  
  qs.push(init);
  cost[0][0]=make_pair(0,0);
  
  while(!qs.empty()){
    State now = qs.top();
    qs.pop();
    
    if( vis[now.have][now.now] )
      continue;
    vis[now.have][now.now] = true;

    int id = now.now;

    /*
      cout << now.now << ' ' << " HAVING : " << now.have << ' ' ;
      for(int i=0;i<q;++i){
      if(now.have&(1<<i))cout << '1';
      else cout <<'0';
      }
      cout << " COST = " << now.cost.first << ',' << now.cost.second << endl;
    */

    for(int j = 0; j < G[id].item_num; ++j){
      State next=now;
      int jbit = (1<<G[id].shop[j].first);
      int jval = G[id].shop[j].second;
      
      if( now.have & jbit ){
	continue;
      }
      
      next.have |= jbit;
      next.cost.first += jval;
      
      if( lesscost( next.cost, cost[next.have][next.now] ) ){
	cost[next.have][next.now] = next.cost;
	if( !vis[next.have][next.now] ){
	  qs.push( next );
	}
      }
    }
    
    for(int i = 0; i < G[id].con.size(); ++i){
      State next = now;
      next.now = G[id].con[i];
      next.cost = make_pair( now.cost.first, now.cost.second + G[id].cost[i] );
      
      if( lesscost( next.cost, cost[next.have][next.now] ) ){
	cost[next.have][next.now] = next.cost;
	if( !vis[next.have][next.now] ){
	  qs.push( next );
	}
      }
    }
  }
  return cost[(1<<q)-1][0];
}

int main()
{

  while(true){
    int n;
    bool impossible=false;
    cin >> n;
    Graph G(n+1);
    map<string,int> shop[n+1];
    map<string,int> item_bit;
    if( n == 0 ) break;

    for(int i = 1; i < n+1; ++i){
      int k;
      cin >> k;
      for(int j = 0; j < k; ++j){
	string item;
	int val;
	cin >> item >> val;
	shop[i][item]=val;
      }
    }

    int q;
    cin >> q;
    for(int i = 0; i < q; ++i){
      string s;
      cin >> s;
      item_bit[s]=i;
    }

    for(int i = 0; i < n+1; ++i){
      G[i].item_num = 0;
      for(int j = 0; j < MAX_ITEM; ++j){
	G[i].shop[j] = make_pair(-1,infty);
      }
      for(map<string,int>::iterator itm = item_bit.begin(); itm != item_bit.end(); ++itm){
	if( shop[i].find( itm->first ) != shop[i].end() ){
	  G[i].shop[ G[i].item_num ] = make_pair(item_bit[itm->first],shop[i][itm->first]);
	  G[i].item_num++;
	}
      }
    }

    int m;
    cin >> m;
    for(int i = 0; i < m; ++i){
      int s,t,d;
      cin >> s >> t >> d;
      G[s].con.push_back(t);
      G[s].cost.push_back(d);
      G[t].con.push_back(s);
      G[t].cost.push_back(d);
    }

    State init;
    init.now = 0;
    init.have = 0;
    init.cost = make_pair(0,0);
    pair<int,int> ans;

    ans = dijkstra( n+1, G, q, init );
    if( ans.first >= infty ){
      cout << "impossible" << endl;
    }else{
      cout << ans.first << ' ' << ans.second << endl;
    }
  }
  return 0;
}