// Arup Guha
// 6/13/2014
// Solution to 2008 UCF High School Contest Problem: Balance That Equation

import java.util.*;
import java.io.*;

public class balance {

	public static void main(String[] args) throws Exception {

		Scanner fin = new Scanner(new File("balance.in"));
		int numCases = Integer.parseInt(fin.nextLine().trim());

		// Go through each case.
		for (int loop=1; loop<=numCases; loop++) {

			// Really annoying parsing.
			StringTokenizer tok = new StringTokenizer(fin.nextLine());
			int left = Integer.parseInt(tok.nextToken());
			int right = Integer.parseInt(tok.nextToken());
			tok = new StringTokenizer(fin.nextLine(),"-");
			String first = tok.nextToken();
			String second = tok.nextToken().substring(1);

			// Form both sides.
			ArrayList<compound> ans = form(first, left);
			side leftSide = new side(ans);
			ans = form(second, right);
			side rightSide = new side(ans);

			// Balance and print.
			balanceEqn(leftSide, rightSide);
			System.out.println("Equation "+loop+": "+leftSide+"-> "+rightSide);
		}
	}

	public static ArrayList<compound> form(String formula, int numC) {

		StringTokenizer tok = new StringTokenizer(formula);
		ArrayList<compound> ans = new ArrayList<compound>();

		// Go through each compound.
		for (int i=0; i<numC; i++) {

			// Break up this compound into parts to store.
			ans.add(new compound());

			// Go through each part.
			while (tok.hasMoreTokens()) {
				String element = tok.nextToken();
				if (element.equals("+")) break;
				long freq = Long.parseLong(tok.nextToken());
				ans.get(i).addTerm(new term(element, freq));
			}
		}

		return ans;
	}

	// Finally, we can solve the problem...
	public static void balanceEqn(side leftSide, side rightSide) {

		String badElem = leftSide.needsBalancing(rightSide);

		// Keep on trying to find an element to balance there are none left to balance.
		while (badElem != null) {
			leftSide.balanceElement(rightSide, badElem);
			badElem = leftSide.needsBalancing(rightSide);
		}
	}
}

class term {

	public String element;
	public long freq;

	public term(String e, long f) {
		element = e;
		freq = f;
	}

	// How we want a term to print. I add the space here...
	public String toString() {
		return element+" "+freq+" ";
	}
}

class compound {

	ArrayList<term> terms;
	public long mult;

	// Start empty.
	public compound() {
		terms = new ArrayList<term>();
		mult = 1;
	}

	// Allow adding.
	public void addTerm(term t) {
		terms.add(t);
	}

	// Useful for output.
	public String toString() {
		String ans = mult+" ";
		for (int i=0; i<terms.size(); i++)
			ans = ans + terms.get(i);
		return ans;
	}

	// Expolits the fact that a particular element appears only once on any side.
	public void multElement(String elem, long factor) {
		for (int i=0; i<terms.size(); i++)
			if (terms.get(i).element.equals(elem))
				mult *= factor;
	}
}

class side {

	ArrayList<compound> compounds;
	TreeMap<String,Long> elemList;

	public side(ArrayList<compound> c) {
		compounds = c;
		computeElemList();
	}

	// Returns the common factors of all multipliers.
	public long getCommon() {

		boolean flag = false;
		long ans = 1;

		// Go through each compound, doing iterative gcd.
		for (compound c: compounds) {
			if (!flag) {
				ans = c.mult;
				flag = true;
			}
			else
				ans = gcd(ans, c.mult);
		}
		return ans;
	}

	public void computeElemList() {

		elemList = new TreeMap<String,Long>();

		// Recompute frequencies of each element the old fashioned way!
		for (compound myCmp: compounds)
			for (term myT: myCmp.terms)
				elemList.put(myT.element, myT.freq*myCmp.mult);
	}

	// Assumes this and other have the same exact elements.
	public String needsBalancing(side other) {
		for (String s: elemList.keySet())
			if (!this.elemList.get(s).equals(other.elemList.get(s)))
				return s;

		return null;
	}

	// Pretty awful style here as I am really working on two objects, not one.
	public void balanceElement(side other, String myElement) {

		// Find the smallest common factor to balance this element.
		long left = this.elemList.get(myElement);
		long right = other.elemList.get(myElement);
		long common = gcd(left, right);
		this.mult(myElement, right/common);
		other.mult(myElement, left/common);

		// See if that introduced any overall common factors.
		this.computeElemList();
		other.computeElemList();
		long leftC = this.getCommon();
		long rightC = other.getCommon();
		long lastDiv = gcd(leftC, rightC);

		// Reduce all multipliers.
		if (lastDiv > 1) {
			this.divComp(lastDiv);
			other.divComp(lastDiv);
		}
		this.computeElemList();
		other.computeElemList();
	}

	// Divide each compound multiplier through by div.
	public void divComp(long div) {
		for (compound c: compounds)
			c.mult /= div;
	}

	// Mult each copy of myElement by factor (there will only be one!)
	public void mult(String myElement, long factor) {
		for (compound c: compounds)
			c.multElement(myElement, factor);
	}

	public static long gcd(long a, long b) {
		if (a == 0) return b;
		if (b == 0) return a;
		return gcd(b, a%b);
	}

	public String toString() {
		String ans = "";
		for (int i=0; i<compounds.size()-1; i++)
			ans = ans + compounds.get(i)+"+ " ;
		ans = ans + compounds.get(compounds.size()-1);
		return ans;
	}
}