using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace rks
{
class Program
{
static StringBuilder result = new StringBuilder();
static void Main(string[] args)
{
//Console.SetIn( new StringReader(Test1) ); // do usunięcia przed wysłaniem
int t = int.Parse( Console.ReadLine() ); // t <= 5
for( int i = 0; i < t; i++)
{
Solve1();
Console.ReadLine();
}
Console.WriteLine( result );
}
static void Solve1()
{
int n = int.Parse( Console.ReadLine() ); // n <= 100 000
var sdict = new SortedDictionary<string, int>();
for( int i = 0; i < n; i++ )
{
string line = Console.ReadLine();
if( !sdict.TryAdd(line, 1) )
sdict[line]++;
}
foreach( var kvp in sdict )
//Console.WriteLine( $"{kvp.Key} {kvp.Value}" );
result.AppendLine($"{kvp.Key} {kvp.Value}");
//Console.WriteLine();
result.AppendLine();
}
const string Test1 = @"2
6
03 10103538 2222 1233 6160 0142
03 10103538 2222 1233 6160 0141
30 10103538 2222 1233 6160 0141
30 10103538 2222 1233 6160 0142
30 10103538 2222 1233 6160 0141
30 10103538 2222 1233 6160 0142
5
30 10103538 2222 1233 6160 0144
30 10103538 2222 1233 6160 0142
30 10103538 2222 1233 6160 0145
30 10103538 2222 1233 6160 0146
30 10103538 2222 1233 6160 0143";
}
}