﻿using System;
using System.Collections.Generic;
using System.Linq;

public class Teamwork_Projects
{
    public static void Main()
    {
        var teamsCount = int.Parse(Console.ReadLine());

        var teams = new List<Team>();

        for (int i = 0; i < teamsCount; i++)
        {
            var teamData = Console.ReadLine()
                .Split('-');

            var teamCreator = teamData[0];
            var teamName = teamData[1];

            if (teams.Any(x => x.Name == teamName))
            {
                Console.WriteLine($"Team {teamName} was already created!");
            }

            else if (teams.Any(x => x.Creator == teamCreator))
            {
                Console.WriteLine($"{teamCreator} cannot create another team!");
            }

            else
            {
                var team = new Team(teamName, teamCreator, new List<string>());
                Console.WriteLine($"Team {teamName} has been created by {teamCreator}!");

                teams.Add(team);
            }
        }

        var line = Console.ReadLine();

        while (!line.Equals("end of assignment"))
        {
            var userData = line
                .Split("->".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            var userName = userData[0];
            var teamName = userData[1];

            if (teams.Any(x => x.Name == teamName))
            {
                var currentTeam = teams.
                    Where(x => x.Name == teamName)
                    .First();

                if (teams.Any(x => x.Creator == userName) || teams.Select(x => x.Members).Any(x => x.Contains(userName)))
                {
                    Console.WriteLine($"Member {userName} cannot join team {teamName}!");
                }

                else
                {
                    currentTeam.Members.Add(userName);
                }
            }

            else
            {
                Console.WriteLine($"Team {teamName} does not exist!");
            }

            line = Console.ReadLine();
        }

        var teamsWithMembers = teams
            .OrderByDescending(x => x.Members.Count())
            .ThenBy(x => x.Name)
            .Where(x => x.Members.Count > 0)
            .ToList();

        foreach (var team in teamsWithMembers)
        {
            Console.WriteLine(team.Name);
            Console.WriteLine($"- {team.Creator}");

            foreach (var member in team.Members.OrderBy(x => x))
            {
                Console.WriteLine($"-- {member}");
            }
        }

        var disbandTeams = teams
          .OrderBy(x => x.Name)
          .Where(x => x.Members.Count() == 0)
          .ToList();

        Console.WriteLine("Teams to disband:");

        foreach (var team in disbandTeams)
        {
            Console.WriteLine(team.Name);
        }
    }
}

public class Team
{
    public Team(string name, string creator, List<string> members)
    {
        Name = name;
        Creator = creator;
        Members = members;
    }

    public string Name { get; set; }

    public List<string> Members { get; set; }

    public string Creator { get; set; }
}