import std.stdio;
import std.string, std.conv;
import std.algorithm, std.array;
import std.typecons;

void main() {
  for(int N; N = readln.chomp.to!int, N; ) {
    int[string] buy;
    foreach(i; 0 .. N) {
      string[] s = readln.split.array;
      buy[s[0]] = s[1].to!int;
    }

    int M = readln.chomp.to!int;

    alias string[] from;
    from[string] make;
    foreach(i; 0 .. M) {
      string[] s = readln.split;
      string name = s[0];
      make[name] = s[2 .. $];
    }

    for(;;) {
      bool f = false;
      foreach(key; make.keys) {
        int sum = 0;
        foreach(value; make[key]) {
          sum += buy[value];
        }

        if((key in buy) == null || sum < buy[key]) {
          buy[key] = sum;
          f = true;
        }
      }
      if(f == false) break;
    }

    writeln(buy[readln.chomp]);
  }
}