using System;
using System.Collections.Generic;
using System.Text;
public class JobControl
{
public static void Main()
{
List<string> lst = new List<string>();
while(true)
{
string line = Console.ReadLine();
if( line == null || line == string.Empty )
{
break;
}
lst.Add(line);
}
proc(lst);
}
private static Dictionary<string, Job> dicJob;
private static void proc(List<string> lst)
{
List<Job> jobList = new List<Job>();
foreach(string line in lst)
{
jobList.Add(new Job(line));
}
dicJob = new Dictionary<string, Job>();
foreach(Job job in jobList)
{
if( job.parent != null)
{
foreach(string pjob in job.parent)
{
if( dicJob.ContainsKey(pjob) )
{
if( job.priority >= dicJob[pjob].priority + 1 )
{
job.priority = dicJob[pjob].priority + 1;
}
}
else
{
Job tmp = new Job();
tmp.name = pjob;
dicJob.Add(pjob, tmp);
job.priority = tmp.priority + 1;
}
}
if( dicJob.ContainsKey(job.name) == false )
{
dicJob.Add(job.name, job);
}
}
else
{
//依存するジョブ無し
if( dicJob.ContainsKey(job.name) == false )
{
dicJob.Add(job.name, job);
}
}
}
//出力準備・・・無駄が多いかな
Dictionary<int, string> dicOutput = new Dictionary<int, string>();
int maxPriority = 0;
foreach(string key in dicJob.Keys)
{
int p = dicJob[key].priority;
if( dicOutput.ContainsKey(p) )
{
dicOutput[p] += " "+ dicJob[key].name;
}
else
{
dicOutput.Add(p, dicJob[key].name);
}
if( maxPriority < p )
{
maxPriority = p;
}
}
//出力
for(int i = 1; i <= maxPriority; i++)
{
string[] tmpArg = dicOutput[i].Trim().Split(' ');
Array.Sort(tmpArg);
Console.WriteLine(string.Join(" ", tmpArg));
}
}
private class Job
{
internal int priority { get; set; }
internal string name { get; set; }
internal List<string> parent { get; private set; }
internal Job()
{
priority = 1;
name = string.Empty;
parent = null;
}
internal Job(string line)
{
priority = 1;
if( line.IndexOf(':') == -1 )
{
name = line;
parent = null;
}
else
{
name = line.Split(':')[0].Trim();
parent = new List<string>();
string[] dependJob = line.Split(':')[1].Trim().Split(' ');
foreach(string dj in dependJob)
{
parent.Add(dj.Trim());
}
}
}
}
}