import java.util.TreeMap;
import java.util.TreeSet;
import java.util.LinkedList;
import java.util.Scanner;


public class E_JJG
{
	public static void main(String args[])
	{
		new E_JJG();
	}

	LinkedList<ChemicalPool> pools;
	LinkedList<ChemicalPool> expand_queue;
	LinkedList<Reaction> reactions;
	TreeMap<String, Integer> chem_ids;
	TreeMap<Integer, String> chem_names;

	public E_JJG()
	{
		Scanner in=new Scanner(System.in);

		while(true)
		{
			pools=new LinkedList<ChemicalPool>();
			expand_queue=new LinkedList<ChemicalPool>();
			reactions=new LinkedList<Reaction>();
			chem_ids=new TreeMap<String, Integer>();
			chem_names=new TreeMap<Integer, String>();
			
			while(true) /* read reactions */
			{
				String line = in.nextLine();
				if (line.equals("ENDOFINPUT")) return;
				if (line.equals("0 0 0")) break;

				Scanner in2 = new Scanner(line);
			
				String chem_a = in2.next();
				String chem_b = in2.next();
				int temp = in2.nextInt();

				int a = assignChem(chem_a);
				int b = assignChem(chem_b);

				Reaction r=new Reaction();
				r.a = a;
				r.b = b;
				r.heat = temp;

				reactions.add(r);
			
			}
			ChemicalPool start=new ChemicalPool();
			while(true) /* read pool */
			{
				String line = in.nextLine();
				if (line.equals("0 0")) break;

				Scanner in2 = new Scanner(line);

				String chem = in2.next();
				int grams = in2.nextInt();
				int c = assignChem(chem);

				start.chems_left[c]+=grams;
			}
			
			expand_queue.add(start);

			while(expand_queue.size() > 0)
			{
				LinkedList<ChemicalPool> ex=expand_queue;
				expand_queue = new LinkedList<ChemicalPool>();

				for(ChemicalPool cp : ex)
				{
					boolean good=true;
					for(ChemicalPool p : pools)
					{
						if (cp.isWorseThan(p))
						{
							good=false;
							break;
						}

					}
					if (good)
					{
						pools.add(cp);
						for(Reaction r : reactions)
						{
							int max_r = Math.min(cp.chems_left[r.a],cp.chems_left[r.b]);

							//for(int i=1; i<=max_r; i++)
							int i = max_r;
							{
								ChemicalPool cp2=new ChemicalPool(cp);

								cp2.chems_left[r.a]-=i;
								cp2.chems_left[r.b]-=i;
								cp2.temp_change += i * r.heat;

								expand_queue.add(cp2);
							}
						}
					}
				}
			}

			int max=0;
			for(ChemicalPool cp : pools)
			{
				max = Math.max(cp.temp_change, max);
				//System.out.println(cp);
			}
			System.out.println("The temperatue in the jar will change by at most " + max + " degrees.");
			//System.out.println(chem_names);

		}


	}

	int assignChem(String a)
	{
		if (chem_ids.containsKey(a)) return chem_ids.get(a);

		int id = chem_ids.size();
		chem_ids.put(a,id);
		chem_names.put(id,a);
		if (id==24) System.err.println("over 24 chemicals");
		return id;


	}

	class Reaction
	{
		int a;
		int b;
		int heat;
	}

	class ChemicalPool
	{
		int temp_change;
		int chems_left[];

		public ChemicalPool()
		{
			temp_change=0;
			chems_left = new int[24];
		}
		public ChemicalPool(ChemicalPool p)
		{
			temp_change=p.temp_change;

			chems_left = new int[24];
			for(int i=0; i<24; i++)
			{
				chems_left[i] = p.chems_left[i];
			}
		}

		public String toString()
		{
			StringBuffer sb=new StringBuffer();

			sb.append(temp_change);
			sb.append(" - " );
			for(int i=0; i<24; i++)
			{
				sb.append(chems_left[i]);
				sb.append(":");
			}

			return sb.toString();
		}

		boolean isWorseThan(ChemicalPool p)
		{

			for(int i=0; i<24; i++)
			{ /* if we have more of anything, we are good */
				if (chems_left[i] > p.chems_left[i]) return false;
			}

			/* if we don't ahve more of anything, but have a better temp
			 * then we are good */
			if (temp_change > p.temp_change) return false;

			/* we are indeed worse or the same */
			return true;


		}

	}


}


