// Arup Guha
// 2/9/2015
// Solution to 2007 MCPC Problem F: Electronic Document Security

import java.util.*;

public class f {

	final public static int ALL = (1 << 26) - 1;

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int loop = 1;
		String line = stdin.nextLine();

		// Go through all cases.
		while (!line.equals("#")) {

			// Need to tokenize the input.
			StringTokenizer tok = new StringTokenizer(line, ",");

			// Store each entity's access as a bit mask.
			int[] access = new int[26];

			// Process each change in order.
			while (tok.hasMoreTokens()) {

				String next = tok.nextToken();
				ArrayList<Integer> entities = new ArrayList<Integer>();

				// Get all entities.
				int i = 0;
				while (Character.isUpperCase(next.charAt(i))) {
					entities.add(next.charAt(i)-'A');
					i++;
				}

				// Store action.
				char action = next.charAt(i++);

				// Calculate this mask.
				int mask = 0;
				while (i < next.length()) {
					mask |= (1 << (next.charAt(i)-'a'));
					i++;
				}

				// Execute equal.
				if (action == '=') {
					for (int j=0; j<entities.size(); j++)
						access[entities.get(j)] = mask;
				}

				// Execute add.
				else if (action == '+') {
					for (int j=0; j<entities.size(); j++)
						access[entities.get(j)] |= mask;
				}

				// Execute take away - can't just subtract...
				else {
					for (int j=0; j<entities.size(); j++)
						access[entities.get(j)] &= (ALL-mask);
				}
			}

			// Create ordered list with non-empty access.
			ArrayList<pair> list = new ArrayList<pair>();
			for (int j=0; j<26; j++)
				if (access[j] > 0)
					list.add(new pair(j, access[j]));

			// Header.
			System.out.print(loop+":");

			// Go through all non-empty items.
			int cur = 0;
			while (cur < list.size()) {

				// Find all equal items.
				int next = cur;
				while (next < list.size() && list.get(next).mask == list.get(cur).mask)
					next++;

				// Print all entities with the same mask.
				for (int j=cur; j<next; j++)
					System.out.print((char)('A'+list.get(j).entity));

				// Print access.
				for (int j=0; j<26; j++)
					if ((list.get(cur).mask & (1 << j)) > 0)
						System.out.print((char)('a'+j));

				// Advance.
				cur = next;
			}
			System.out.println();

			// Get next case.
			line = stdin.nextLine();
			loop++;
		}

	}
}

class pair {

	int entity;
	int mask;

	public pair(int e, int m) {
		entity = e;
		mask = m;
	}
}