import java.io.*;

import java.util.StringTokenizer;

import java.util.*;



import javax.swing.JOptionPane;



public class Diagnostic {



	public static void main(String[] args) {

		try {

			Diagnostic d = new Diagnostic();

			String inFileName = ("Diagnostic.txt");

			FileReader reader = new FileReader(inFileName);

			BufferedReader ins = new BufferedReader(reader);

			d.convertFile(ins);



			String outFileName = JOptionPane.showInputDialog("Output file:");

			FileWriter outStream = new FileWriter(outFileName);

			PrintWriter outs = new PrintWriter(outStream);



			Node root = d.buildTree(0);

			d.help(root, outs);



			ins.close();

		} catch (IOException e) {

			System.out.println(e.getMessage());

			e.printStackTrace();

		}

	}



	public void help(Node cur, PrintWriter pw) {

		if (cur != null) {

			int next;

			String n = "0";

			if (cur.children.size() > 1)

				n = JOptionPane.showInputDialog(cur.msg + " (enter number)\n"

						+ cur.printChildren());

			else

				System.out.println(cur.msg);

			pw.println(cur.num + "\t" + cur.msg);

			pw.println(cur.printChildren());

			try {

				next = Integer.parseInt(n);

				ArrayList kids = cur.children;

				Node nd;

				for (int i = 0; i < kids.size(); i++) {

					nd = (Node) kids.get(i);

					if (nd == null)

						break;

					if (!cur.contains(next))

						throw new Exception();

					if (nd.num == next) {

						if (nd.children.size() > 1) {

							help(nd, pw);

						} else {

							help((Node) nd.children.get(0), pw);

						}

					}

				}

			} catch (Exception e) {

				System.out.println("invalid input!!");

			}

		}

		pw.close();

	}



	public ArrayList orderedFile = new ArrayList();



	public void convertFile(BufferedReader br) {

		String s;

		try {

			while ((s = br.readLine()) != null) {

				orderedFile.add(s);

			}

		} catch (IOException ex) {

			System.out.println("I/O error " + ex.getMessage());

			ex.printStackTrace();

		}

	}



	public Node buildTree(int pos) {

		if (pos == -1)

			return null;

		StringTokenizer st = new StringTokenizer((String) orderedFile.get(pos),

				"\t");

		int num = Integer.parseInt(st.nextToken());

		String msg = st.nextToken();

		ArrayList children = new ArrayList();

		;

		String kids = st.nextToken();

		StringTokenizer st2 = new StringTokenizer(kids, ",");

		while (st2.hasMoreElements()) {

			children.add(buildTree(Integer.parseInt(st2.nextToken())));

		}

		return new Node(num, msg, children);

	}



	public class Node {

		public int num;



		public String msg;



		public ArrayList children;



		public Node(int n, String m, ArrayList c) {

			num = n;

			msg = m;

			children = c;

		}



	public boolean contains(int next) {

			for (int i = 0; i < children.size(); i++) {

				if (next == ((Node) this.children.get(i)).num)

					return true;

			}

			return false;

		}



		public String printChildren() {

			String s = "\n";

			for (int i = 0; i < this.children.size(); i++) {

				if (this.children.get(i) != null)

					s = s + ((Node) this.children.get(i)).num + " "

							+ ((Node) this.children.get(i)).msg + "\n";

			}

			return s;

		}



		public void printit() {

			System.out.println();

			System.out.println();

			System.out.println("Node: " + num);

			System.out.println(msg);

			for (int i = 0; i < children.size(); i++) {

				if ((Node) children.get(i) == null) {

					System.out.println("done");

					break;

				}

				((Node) children.get(i)).printit();

			}

		}



	}

}