#!/usr/bin/env python

import sys

# thanks: https://www.adamsmith.haus/python/answers/how-to-create-a-trie-in-python
def construct_trie(word_list):
    root = dict()
    for word in word_list:
        level = root
        for letter in word:
            if letter not in level:
                level[letter] = dict()
            level = level[letter]

    return root

def print_trie(trie):
    length = len(trie)

    if length == 0:
        return

    if length > 1:
        sys.stdout.write("{")

    count = 1

    for k in trie:
        sys.stdout.write(k)

        print_trie(trie[k])

        if count < length:
            sys.stdout.write(",")

        count += 1

    if length > 1:
        sys.stdout.write("}")

lines = []

for line in sys.stdin:
    stripped = line.strip()

    if stripped != "":
        lines.append(stripped)

trie = construct_trie(lines)
print(trie)
print_trie(trie)
sys.stdout.write("\n")
