Problem

In 19XX, the Allied Forces were formed in n countries. Each country belonging to the Allied Forces has set up one or more bases in the country to protect itself from the invasion of enemy forces and is on the alert. Figure 1 shows an example of an Allied country, where the rectangle represents each country and the circle within it represents the base of that country.


Figure 1
Figure 1


When an enemy army invades, the information is to be transmitted from the base where it was first discovered to the bases of other countries or own countries belonging to the Allied Forces. It costs a positive integer to convey information to other locations.

In addition, each country has exactly one base to which soldiers belonging to the Allied Translator and Interpreter Section (ATIS) are assigned. Only this soldier can speak the language of all countries belonging to the Allied Forces, and the other soldiers can only speak the language of their own country. Therefore, when the base that received the information tries to convey the information to another country, if the base does not have soldiers belonging to ATIS, the information is transmitted from there to the base having soldiers belonging to ATIS in the home country and the information is sent to other countries. Need to be sent.

Each country is assigned a number from 0 to [the number of bases in that country-1], and the 0th base has soldiers belonging to ATIS. In other words, only the 0th base can transmit information to the bases in other countries, and communication from other bases to other countries is not possible. In addition, if the information is transmitted to the 0th base of the country, it is assumed that the information is transmitted to the whole country.

Communication is not possible between all bases for various reasons. Even if information can be transmitted from one base to another, the opposite is not always true. In addition, the cost of transmitting information from one base to another is not always the same as the cost of transmitting information to another base.

Based on these facts, I would like to know the transmission method with the lowest total cost of disseminating information to the entire Allied army when a base discovers an enemy army. It is assumed that the information has been distributed to the entire Allied Forces when the information is transmitted from the base where the enemy army is first found to the 0th base of all the countries belonging to the Allied Forces.

Since the base where the enemy army was found is given as a question, output the method of transmitting the information to the entire Allied army and its cost at the lowest cost.

For example, consider the following case (Sample Input 4, first question)

Suppose the first base of the country neepal discovers an enemy army. In this case, since it is not possible to communicate from the first base to another country, the information is first transmitted to the 0th base in the home country.

Next, the information is transmitted from the 0th base to the country luxenbourg, noway. It is possible to communicate directly from neepal to the 0th base of luxenbourg, but it is cheaper to convey the information to the 3rd base and then to the 0th base. In this case, the minimum cost to convey information to the 0th country of all countries is 12. There are multiple transmission methods that cost only 12, but one of them may be output as the answer.

The red arrow in Figure 2 below is one of the answers.


<image>
Figure 2

Constraints

* 1 ≤ n ≤ 26
* 1 ≤ mi ≤ 100
* 0 ≤ e ≤ 322400
* 0 ≤ costi ≤ 100
* 1 ≤ q ≤ 10
* countryname is a character string consisting of lowercase letters ‘a’ to ‘z’ and having a length of 1 or more and 20 or less.
* No more than one country with the same name will be given
* When c1i and c2i of the pair of communicable bases are the same, v1i and v2i are different
* When c1i and c2i of the pair of communicable bases are different, v1i is always 0.

Input


n
countryname0 m0
countryname1 m1
...
countrynamen-1 mn-1
e
c10 v10 c20 v20 cost0
c11 v11 c21 v21 cost1
...
c1e-1 v1e-1 c2e-1 v2e-1 coste-1
q
c30 v30
c31 v31
...
c3q-1 v3q-1



* n represents the number of countries belonging to the Allied Forces
* countrynamei is the name of the i-th country belonging to the Allied Forces, and mi is the number of bases in the i-th country.
* e represents the number of pairs of bases that can communicate
* The following e-line is given the cost cost of transmitting information from the base v1i in country c1i to the base v2i in country c2i.
* q represents the number of questions
* The following line q is given the name c3i of the country where the enemy army was found and its base v3i.
* c1i, c2i, c3i are any of the countries belonging to the Allied Forces
* v1i, v2i, v3i can never be more than the number of bases in the country

Output

The answer to each question is output in the following format.

On the last line of the answer to each question, print one line of 5'-', "-----" (excluding ").

If the answer exists

The first line outputs the minimum cost required to disseminate information from the country base given in the question to the entire Allied Forces.

The transmission method at that time is output on the second and subsequent lines. The transmission method is expressed in the following format.


c40 v40 c50 v50
c41 v41 c51 v51
...


Indicates that information has been transmitted from the base v4i in country c4i to the base v5i in country c5i.

Be careful of the following.

* It is not necessary to output in the order in which communication was performed
* If there is one or more transmission methods that minimize the total cost, one of them should be output.
* Do not output the same communicable pair more than once
* c4i, c5i must be one of the countries given in the input
* v4i, v5i must be less than the number of bases in the country




If the answer does not exist

Print “Impossible” (excluding “)” on one line.

Examples

Input

3
frence 1
usi 1
powland 1
2
usi 0 frence 0 10
frence 0 powland 0 10
2
usi 0
powland 0


Output

20
usi 0 frence 0
frence 0 powland 0
-----
Impossible
-----


Input

2
usso 2
caneda 2
4
usso 1 usso 0 1
usso 0 caneda 0 10
usso 0 caneda 1 2
caneda 1 caneda 0 2
1
usso 1


Output

5
usso 1 usso 0
usso 0 caneda 1
caneda 1 caneda 0
-----


Input

3
chinax 1
ok 1
austraria 2
6
chinax 0 austraria 0 5
austraria 0 chinax 0 5
austraria 1 austraria 0 1
ok 0 austraria 0 5
austraria 0 ok 0 5
ok 0 austraria 1 1
2
chinax 0
ok 0


Output

10
chinax 0 austraria 0
austraria 0 ok 0
-----
7
ok 0 austraria 1
austraria 1 austraria 0
austraria 0 chinax 0
-----


Input

3
neepal 2
luxenbourg 4
noway 2
12
neepal 1 neepal 0 2
neepal 0 noway 1 2
neepal 0 luxenbourg 0 10
neepal 0 luxenbourg 3 2
luxenbourg 3 luxenbourg 1 2
luxenbourg 3 luxenbourg 2 2
luxenbourg 1 luxenbourg 0 2
luxenbourg 2 luxenbourg 0 2
noway 1 noway 0 2
noway 0 neepal 0 2
noway 0 luxenbourg 3 2
noway 0 luxenbourg 0 10
3
neepal 1
luxenbourg 3
noway 1


Output

12
neepal 1 neepal 0
neepal 0 luxenbourg 3
luxenbourg 3 luxenbourg 2
luxenbourg 2 luxenbourg 0
neepal 0 noway 1
noway 1 noway 0
-----
Impossible
-----
10
noway 1 noway 0
neepal 0 luxenbourg 3
luxenbourg 3 luxenbourg 2
luxenbourg 2 luxenbourg 0
noway 0 neepal 0
-----
#define _CRT_SECURE_NO_WARNINGS
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii;
typedef long long ll; typedef vector<long long> vl; typedef pair<long long,long long> pll; typedef vector<pair<long long,long long> > vpll;
typedef vector<string> vs; typedef long double ld;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }

struct GetStrID {
	static const int MaxLen = 21;	//書き換えること！
	static const int Alphas = 26;	//書き換えること！
	typedef char Alpha;
	static const int EndAlpha = -1;

private:
	template<typename T>
	struct Trie {
		bool terminate;
		T val;
		Trie<T> *next[Alphas];

		Trie(): terminate(false) { memset(next, 0, sizeof(next)); }
		~Trie() { for(Alpha a = 0; a < Alphas; ++ a) delete next[a]; }

		void insert(Alpha *s, const T &v) {
			if(*s == EndAlpha) {
				terminate = true;
				val = v;
				return;
			}else {
				if(!next[*s])
					next[*s] = new Trie;
				return next[*s]->insert(s+1, v);
			}
		}
		bool find(Alpha *s, T &out_v) const {
			if(*s == EndAlpha) {
				if(terminate) {
					out_v = val;
					return true;
				}else return false;
			}
			return next[*s] && next[*s]->find(s+1, out_v);
		}
	};

public:
	GetStrID(const char *alphas): cnt(0) {
		assert(strlen(alphas) <= Alphas);
		memset(idx, -1, sizeof(idx));
		for(const char *p = alphas; *p; ++ p)
			idx[*p] = p - alphas;
	}
	char idx[128];
	Trie<int> trie;
	int cnt;

	void clear() { trie = Trie<int>(); cnt = 0; }
	int get(const char *x) {
		static char tmp[MaxLen+1];
		{	int i;
			for(i = 0; x[i]; i ++) tmp[i] = idx[x[i]];
			tmp[i] = EndAlpha;
		}
		int ret = -1;
		if(!trie.find(tmp, ret)) {
			ret = cnt ++;
			trie.insert(tmp, ret);
		}
		return ret;
	}
};

pair<int,int> detectCycle2(const vector<int> &f) {
	int n = f.size();
	vector<bool> ved(n);
	int power, lambda, tortoise, hare;
	outer:
	for(int s = 0;s < n;s++){
		if(!ved[s]){
			power = 1, lambda = 1;
			if(f[s] < 0)continue;
			tortoise = s;
			ved[s] = true;
			hare = f[s];
			while(tortoise != hare) {
				if(hare < 0)goto outer;
				ved[hare] = true;
				if(power == lambda) {
					tortoise = hare;
					power <<= 1;
					lambda = 0;
				}
				if(hare < 0)goto outer;
				hare = f[hare];
				lambda++;
			}
			if(lambda > 0){
				return mp(hare, lambda);
			}
		}
	}
	return mp(-1,-1);
}

typedef int Weight;
vector<int> buildMSA(const vector<vector<Weight> > &w, int root)
{
int n = w.size();
		
// critical graphをつくる
// root以外の全頂点に対し、そこに入る最小コスト辺を選ぶ
vi par(n, -1);
for(int i = 0;i < n;i++){
	if(i != root){
		Weight min = INF;
		int pi = -1;
		for(int j = 0;j < n;j++){
			if(j != i){
				if(min > w[j][i]){
					min = w[j][i];
					pi = j;
				}
			}
		}
		par[i] = pi;
	}
}
		
pii res = detectCycle2(par);
if(res.first != -1){
	// cycleが存在したら縮約
	vi map(n, -1);
	int shed = n-res.second;
	map[res.first] = shed;
			
	// cycleの要素を列挙
	Weight minc = w[par[res.first]][res.first];
	for(int k = par[res.first];k != res.first;k = par[k]){
		map[k] = shed;
		minc = min(minc, w[par[k]][k]);
	}
			
	// 縮約後のグラフとの対応
	vector<int> imap(n-res.second);
	int p = 0;
	for(int i = 0;i < n;i++){
		if(map[i] != shed){
			map[i] = p;
			imap[p] = i;
			p++;
		}
	}
			
	// 縮約後のグラフ
	vector<vector<Weight> > sw(shed+1, vector<Weight>(shed+1, INF));
			
	vi to(n); // cycle内の要素xで、辺i->xの最小weightを実現するもの
	vi from(n); // cycle内の要素xで、辺x->iの最小weightを実現するもの
	for(int i = 0;i < n;i++){
		if(map[i] != shed){
			for(int j = 0;j < n;j++){
				if(map[j] == shed){
					// 非cycle->cycle
					// weightを修正
					Weight V = w[i][j] - w[par[j]][j] + minc;
					if(sw[map[i]][shed] > V){
						sw[map[i]][shed] = V;
						to[i] = j;
					}
				}else{
					// 非cycle->非cycle
					sw[map[i]][map[j]] = w[i][j];
				}
			}
		}else{
			for(int j = 0;j < n;j++){
				if(map[j] != shed){
					// cycle->非cycle
					Weight V = w[i][j];
					if(sw[shed][map[j]] > V){
						sw[shed][map[j]] = V;
						from[j] = i;
					}
				}
			}
		}
	}
			
	vi smsa = buildMSA(sw, map[root]); // 再帰してMSAを求める。
	// cycle以外の頂点について、imap,fromに従って対応を戻す。
	for(int i = 0;i < shed;i++){
		if(smsa[i] == shed){
			par[imap[i]] = from[imap[i]]; // cycle->非cycle
		}else if(smsa[i] != -1){
			par[imap[i]] = imap[smsa[i]]; // 非cycle->非cycle
		}
	}
			
	// cycle内の頂点について、toに従って対応を戻す。
	int u = imap[smsa[shed]];
	par[to[u]] = u;
}
return par;
}

int n;
char name1[21], name2[21];
char names[26][21];
int ms[26];
int g[26][100][100];
int ginter[26][100][100];
int h[26][26];
int hg[26][26];
bool vis[26];
vector<pair<pii,pii> > anss;

int dfs(int c) {
	if(vis[c]) return 0;
	vis[c] = true;
	int r = 1;
	rep(d, n) if(h[c][d] < INF)
		r += dfs(d);
	return r;
}

void buildPath_g(int c, int s, int t) {
	int u = ginter[c][s][t];
	if(u == -1) {
		if(s != t) anss.push_back(mp(mp(c, c), mp(s, t)));
	}else buildPath_g(c, s, u), buildPath_g(c, u, t);
}

int main() {
	GetStrID country("abcdefghijklmnopqrstuvwxyz");
	scanf("%d", &n);
	rep(i, n) {
		scanf("%s%d", names[i], &ms[i]);
		country.get(names[i]);
	}
	int e;
	scanf("%d", &e);
	mset(g, INF);
	rep(c, n) rep(i, ms[c]) g[c][i][i] = 0;
	vector<pair<pii,pii> > edges;
	rep(i, e) {
		int v1, v2, cost;
		scanf("%s%d%s%d%d", name1, &v1, name2, &v2, &cost);
		int c1 = country.get(name1), c2 = country.get(name2);
		if(c1 == c2) {
			amin(g[c1][v1][v2], cost);
		}else {
			edges.pb(mp(mp(c1,c2),mp(v2,cost)));
		}
	}
	mset(ginter, -1);
	rep(c, n) {
		int m = ms[c];
		rep(k, m) rep(i, m) rep(j, m) {
			if(g[c][i][j] > g[c][i][k] + g[c][k][j]) {
				g[c][i][j] = g[c][i][k] + g[c][k][j];
				ginter[c][i][j] = k;
			}
		}
	}
	mset(h, INF);
	rep(c, n) h[c][c] = 0;
	each(i, edges) {
		int c1 = i->first.first, c2 = i->first.second;
		int v2 = i->second.first, cost = i->second.second;
		if(h[c1][c2] > cost + g[c2][v2][0]) {
			h[c1][c2] = cost + g[c2][v2][0];
			hg[c1][c2] = v2;
		}
	}
	vector<vi> hv(n, vi(n));
	rep(i, n) rep(j, n) hv[i][j] = h[i][j];
	int q;
	scanf("%d", &q);
	rep(ii, q) {
		int v;
		scanf("%s%d", name1, &v);
		int c = country.get(name1);
		bool ok = true;
		ok &= g[c][v][0] < INF;
		mset(vis, 0);
		ok &= dfs(c) == n;
		if(!ok) {
			puts("Impossible");
		}else {
			anss.clear();
			int cost = g[c][v][0];
			buildPath_g(c, v, 0);
			vi tree = buildMSA(hv, c);
			rep(i, n) if(i != c) {
				cost += hv[tree[i]][i];
				int v2 = hg[tree[i]][i];
				anss.push_back(mp(mp(tree[i], i), mp(0, v2)));
				buildPath_g(i, v2, 0);
			}
			printf("%d\n", cost);
//			sort(all(anss));
//			anss.erase(unique(all(anss)), anss.end());
			each(i, anss) {
				pii p = i->first, q = i->second;
				printf("%s %d %s %d\n",
					names[p.first], q.first,
					names[p.second], q.second);
			}
		}
		puts("-----");
	}
	return 0;
}
