using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int t = int.Parse(Console.ReadLine());
List<string> output = new List<string>();
for (int i = 0; i < t; i++)
{
List<string> prodDetails = new List<string>();
int c = int.Parse(Console.ReadLine());
for (int j = 0; j < c; j++)
{
prodDetails.Add(Console.ReadLine());
}
GetTopProductDetails(prodDetails, output);
}
output.ForEach(x => Console.WriteLine(x));
}
private static List<string> GetTopProductDetails(List<string> prodDetails, List<string> output)
{
Dictionary<string, int=""> keyValuePairs = new Dictionary<string, int="">();
for (int i = 0; i < prodDetails.Count; i++)
{
if (prodDetails[i].ToLower() != "top")
{
string[] x = prodDetails[i].Split(' ');
string P = x[0];
int U = int.Parse(x[1]);
if (keyValuePairs.ContainsKey(P))
{
int unit = 0;
keyValuePairs.TryGetValue(P, out unit);
keyValuePairs[P] = unit + U;
}
else
{
keyValuePairs.Add(P, U);
}
}
else
{
var valuesList = keyValuePairs.Where(x => x.Value == keyValuePairs.Values.Max()).Select(v => v.Key).OrderBy(y => y).ToList();
string disp = string.Join(" ", valuesList);
output.Add(disp);
}
}
return output;
}
}
}