// Arup Guha
// 10/9/2015
// Solution to 2001 UCF HS Contest Problem: Forgetful Family

import java.util.*;

public class family {

	final public static int UNKNOWN = -1;

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int n = stdin.nextInt();

		// Process each case.
		while (n != 0) {

			HashMap<String,Integer> ages = new HashMap<String,Integer>();

			// Just read in all the data, without processing anything.
			String[][] data = new String[n][2];
			for (int i=0; i<n; i++)
				for (int j=0; j<2; j++)
					data[i][j] = stdin.next();

			// Keep iterating until no new items are filled.
			while (true) {

				boolean added = false;

				// Look for new ages to calculate.
				for (int i=0; i<n; i++) {
					if (ages.containsKey(data[i][0])) continue;

					// Straight age given - store it.
					if (!data[i][1].contains("+") && !data[i][1].contains("-")) {
						ages.put(data[i][0], Integer.parseInt(data[i][1]));
						added = true;
						continue;
					}

					// Parse out relative information.
					StringTokenizer tok = new StringTokenizer(data[i][1], "+-");
					String sibling = tok.nextToken();
					int offset = Integer.parseInt(tok.nextToken());
					if (data[i][1].contains("-")) offset = -offset;

					// Hey, we can figure out this age, so do it!
					if (ages.containsKey(sibling)) {
						ages.put(data[i][0], ages.get(sibling)+offset);
						added = true;
					}
				}

				// If no new ages have been determined, we've filled out all we can.
				if (!added) break;
			} // end while-true.

			// Process queries.
			int numQueries = stdin.nextInt();
			for (int i=0; i<numQueries; i++) {

				// Get age.
				String name = stdin.next();
				int res = ages.get(name);

				// Annoying format.
				if (res == 1)
					System.out.println(name+" is 1 year old.");
				else
					System.out.println(name+" is "+res+" years old.");
			}

			// Get next case.
			n = stdin.nextInt();
			System.out.println();
		}
	}
}