/* ACM ICPC Asia 98 Tokyo Problem G: BUT We Need a Diagram by Naoyuki Tamura (tamura@kobe-u.ac.jp) */ import java.io.*; import java.util.*; public class G { static boolean dbg = false; static PushbackInputStream in = new PushbackInputStream(System.in); static G_BUT but; static void debug(String msg) { if (dbg) System.err.print(msg); } public static void main(String args[]) { if (args.length > 0) dbg = true; try { for (int n = 1; ; n++) { but = G_BUT.parseBUT(in); debug("# " + but + "\n"); System.out.println(n + ":"); but.locate(); but.print(); char c = G_BUT.read(in); if (c != ';') break; } } catch (Exception e) { e.printStackTrace(); } } } class G_BUT { String label; G_BUT le = null; G_BUT ri = null; int d; int p[]; String t[]; G_BUT(char l) { label = l + ""; } static String repeat(int n, String c) { String s = ""; for (int i = 0; i < n; i++) s += c; return s; } static String removeTrailing(String s) throws Exception { int i; for (i = s.length() - 1; i >= 0; i--) if (s.charAt(i) != ' ') break; return s.substring(0, i+1); } void shift(int k) { for (int i = 0; i < d; i++) p[i] += k; } void locate() { if (le == null) { // 0-ary d = 1; p = new int[d]; t = new String[d]; p[0] = 0; t[0] = label; return; } else if (ri == null) { // unary le.locate(); d = le.d + 2; p = new int[d]; t = new String[d]; for (int i = 2; i < d; i++) { p[i] = le.p[i-2]; t[i] = le.t[i-2]; } p[1] = 0; t[1] = "-"; p[0] = 0; t[0] = label; return; } // binary le.locate(); ri.locate(); int k0 = Integer.MIN_VALUE; for (int i = 0; i < le.d && i < ri.d; i++) { int k = le.p[i] + le.t[i].length() + 1; k0 = Math.max(k0, k - ri.p[i]); } int dl = k0 / 2; int dr = k0 - dl; le.shift(-dl); ri.shift(dr); d = Math.max(le.d, ri.d) + 2; p = new int[d]; t = new String[d]; for (int i = 0; i < d-2; i++) { if (i < le.d && i < ri.d) { p[i+2] = le.p[i]; int k = ri.p[i] - (le.p[i] + le.t[i].length()); t[i+2] = le.t[i] + repeat(k, " ") + ri.t[i]; } else if (i < le.d) { p[i+2] = le.p[i]; t[i+2] = le.t[i]; } else if (i < ri.d) { p[i+2] = ri.p[i]; t[i+2] = ri.t[i]; } } p[1] = -dl; t[1] = repeat(dl+dr+1, "-"); p[0] = p[1] + 1; t[0] = repeat(dl-1, " ") + label + repeat(dr-1, " "); // debugPrint(); } void print() throws Exception { int k = Integer.MAX_VALUE; for (int i = d - 1; i >= 0; i--) k = Math.min(k, p[i]); for (int i = d - 1; i >= 0; i--) { System.out.print(repeat(p[i]-k, " ")); System.out.println(removeTrailing(t[i])); } } void debugPrint() { G.debug("# "); for (int i = d - 1; i >= 0; i--) { for (int k = 0; k < p[i]; k++) G.debug(" "); G.debug(t[i] + "\n"); } } public String toString() { String s = label; if (le != null) { s += "(" + le; if (ri != null) s += "," + ri; s += ")"; } return s; } static char read(PushbackInputStream in) throws Exception { char c; while (true) { c = (char)in.read(); if (c != ' ' && c != '\t' && c != '\n' && c != '\r') break; } return c; } static G_BUT parseBUT(PushbackInputStream in) throws Exception { G_BUT b = null; char c = read(in); if (('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z')) { b = new G_BUT(c); c = read(in); if (c == '(') { b.le = parseBUT(in); c = read(in); if (c == ',') { b.ri = parseBUT(in); c = read(in); } if (c != ')') { G.debug("# parse error\n"); } } else { in.unread((char)c); } } else { in.unread((char)c); } return b; } }