private static void GetLegionsData(int n, string pattern, Dictionary <string, Legion> allLegions)
        {
            for (int i = 0; i < n; i++)
            {
                string inputLine = Console.ReadLine();
                Match  match     = Regex.Match(inputLine, pattern);

                Legion legion = new Legion();

                legion.Name     = match.Groups["name"].Value;
                legion.Activity = long.Parse(match.Groups["activity"].Value);
                string legionType  = match.Groups["type"].Value;
                long   legionCount = long.Parse(match.Groups["count"].Value);
                legion.Armada = new Dictionary <string, long>();
                legion.Armada.Add(legionType, legionCount);

                if (!allLegions.ContainsKey(legion.Name))
                {
                    allLegions[legion.Name]          = new Legion();
                    allLegions[legion.Name].Armada   = new Dictionary <string, long>();
                    allLegions[legion.Name].Activity = legion.Activity;
                }

                if (!allLegions[legion.Name].Armada.ContainsKey(legionType))
                {
                    allLegions[legion.Name].Armada[legionType] = 0L;
                }

                allLegions[legion.Name].Armada[legionType] += legionCount;

                if (allLegions[legion.Name].Activity < legion.Activity)
                {
                    allLegions[legion.Name].Activity = legion.Activity;
                }
            }
        }
        public static void Main()
        {
            //{lastActivity} = {legionName} -> {soldierType}:{soldierCount}
            string pattern = @"^(\d+) = (.+) -> (.+):(\d+)$";
            int    n       = int.Parse(Console.ReadLine());

            Dictionary <string, Legion> results = new Dictionary <string, Legion>();

            for (int i = 0; i < n; i++)
            {
                string input = Console.ReadLine();
                Match  m     = Regex.Match(input, pattern);
                if (m.Success)
                {
                    int    lastAcativity = int.Parse(m.Groups[1].Value);
                    string legionName    = m.Groups[2].Value;
                    string solgierType   = m.Groups[3].Value;
                    int    solgierNum    = int.Parse(m.Groups[4].Value);

                    if (!results.ContainsKey(legionName))
                    {
                        results[legionName] = new Legion();
                    }

                    if (results[legionName].LastActivity < lastAcativity)
                    {
                        results[legionName].LastActivity = lastAcativity;
                    }

                    if (!results[legionName].SolgierTypes.ContainsKey(solgierType))
                    {
                        results[legionName].SolgierTypes[solgierType] = 0;
                    }

                    results[legionName].SolgierTypes[solgierType] += solgierNum;
                }
            }

            string pattern1 = @"(\d+)\\(.+)";
            string line     = Console.ReadLine();
            Match  match    = Regex.Match(line, pattern1);

            if (match.Success)
            {
                int    activity    = int.Parse(match.Groups[1].Value);
                string soldierType = match.Groups[2].Value;

                var result = results.Where(x => (x.Value.LastActivity < activity && x.Value.SolgierTypes.ContainsKey(soldierType)))
                             .OrderByDescending(x => x.Value.SolgierTypes[soldierType])
                             .ToDictionary(x => x.Key, x => x.Value);

                foreach (var item in result)
                {
                    Console.WriteLine("{0} -> {1}", item.Key, item.Value.SolgierTypes[soldierType]);
                }
            }
            else
            {
                string soldierType = line;
                var    result      = results.Where(x => x.Value.SolgierTypes.ContainsKey(soldierType))
                                     .OrderByDescending(x => x.Value.LastActivity)
                                     .ToDictionary(x => x.Key, x => x.Value);

                foreach (var item in result)
                {
                    Console.WriteLine("{0} : {1}", item.Value.LastActivity, item.Key);
                }
            }
        }