var
j,i,now,time,t,n:longint;
s:array[0..200000]of string;
procedure sort(l,r:longint);
var
i,j:longint;
x,y:string;
begin
i:=l;
j:=r;
x:=s[(l+r) div 2];
repeat
while s[i]<x do inc(i);
while x<s[j] do dec(j);
if i<=j then
begin
y:=s[i];
s[i]:=s[j];
s[j]:=y;
inc(i);
dec(j);
end;
until i>j;
if i<r then sort(i,r);
if l<j then sort(l,j);
end;
begin
assign(input,'nickname.in'); reset(input);
assign(output,'nickname.out'); rewrite(output);
readln(t);
for time:=1 to t do
begin
readln(n);
for i:=1 to n do
begin
readln(s[i]);
for j:=1 to length(s[i]) do
if ord(s[i][j])<97 then s[i][j]:=chr(ord(s[i][j])+32);
end;
sort(1,n);
now:=1;
for i:=2 to n do
if s[i]=s[i-1] then inc(now)
else begin
writeln(s[i-1],' ',now);
now:=1;
end;
writeln(s[n],' ',now);
end;
close(input);
close(output);
end.