// Arup Guha
// 1/18/2017
// Solution to 2014 NAQ Problem J: Units

import java.util.*;

public class units {

	public static int n;
	public static HashMap<String,Integer> map;
	public static String[] names;
	public static ArrayList[] graph;

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		n = stdin.nextInt();

		// Process each case.
		while (n != 0) {

			// Store unit names both ways.
			map = new HashMap<String,Integer>();
			names = new String[n];
			for (int i=0; i<n; i++) {
				names[i] = stdin.next();
				map.put(names[i], i);
			}

			// Set up graph.
			graph = new ArrayList[n];
			for (int i=0; i<n; i++)
				graph[i] = new ArrayList<edge>();

			// Read and add in edges. (mult = false means divide...)
			for (int i=0; i<n-1; i++) {
				int v1 = map.get(stdin.next());
				stdin.next();
				int factor = stdin.nextInt();
				int v2 = map.get(stdin.next());
				graph[v1].add(new edge(v2, true, factor));
				graph[v2].add(new edge(v1, false, factor));
			}

			// Solve it!
			item[] res = solve();

			// For output ordering.
			Arrays.sort(res);

			// Here is how they want the output.
			System.out.print(res[0].factor+res[0].name);
			for (int i=1; i<n; i++)
				System.out.print(" = "+res[i].factor+res[i].name);
			System.out.println();

			// Get next case.
			n = stdin.nextInt();
		}
	}

	public static item[] solve() {

		// Try DFS from each spot.
		for (int i=0; i<n; i++) {

			// Run DFS from i.
			int[] mults = new int[n];
			boolean[] used = new boolean[n];
			dfs(i, 1, mults, used);

			// This reached everyone with integer multiples from i.
			if (allTrue(used)) {
				item[] res = new item[n];
				for (int j=0; j<n; j++)
					res[j] = new item(names[j], mults[j]);
				return res;
			}

		}

		// Should never get here.
		return null;
	}

	public static void dfs(int v, int factor, int[] mults, boolean[] used) {

		// Mark node v.
		mults[v] = factor;
		used[v] = true;

		// Go through neighbors.
		for (int i=0; i<graph[v].size(); i++) {

			// Just for ease of naming.
			edge e = (edge)(graph[v].get(i));

			// Been there.
			if (used[e.to]) continue;

			// Not an integer multiple of our original starting point.
			if (!e.mult && factor%e.ratio != 0) continue;

			// Run appropriate DFS on next unit.
			if (e.mult) dfs(e.to, factor*e.ratio, mults, used);
			else        dfs(e.to, factor/e.ratio, mults, used);
		}

	}

	// Returns true iff arr is all true.
	public static boolean allTrue(boolean[] arr) {
		for (int i=0; i<arr.length; i++)
			if (!arr[i])
				return false;
		return true;
	}
}

class item implements Comparable<item> {

	public String name;
	public int factor;

	public item(String s, int f) {
		name = s;
		factor = f;
	}

	public int compareTo(item other) {
		return this.factor - other.factor;
	}
}

class edge {

	public int to;
	public boolean mult;
	public int ratio;

	public edge(int v, boolean dir, int r) {
		to = v;
		mult = dir;
		ratio = r;
	}
}