//* Problem  : 
//* Contest  : acm.tju.edu.cn/toj
//* Type     : Solution
//* Date     : 2008.08.04
//* Author   : alt
//* Language : C++
//* Compiler : Microsoft Visual C++ 6.0

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <string>
#include <stack>
#include <set>
#include <map>
#include <algorithm>

using namespace std;

typedef pair <int,int> PIII;
typedef vector <int> VI;
typedef vector <vector<int> > VVI;
typedef vector <pair<int,int> > VPII;
typedef vector <vector<pair<int,int> > > VVPII;

#define int64 long long
#define PI (2.0 * acos(0.0))
#define MP make_pair
#define PB push_back
#define SZ(a) (int)a.size()
#define FOR(i, n) for (int i = 0; i < (int)n; i++)
#define FORSZ(i, a) FOR(i, SZ(a))
#define FORE(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)
#define FORR(i, a, b) for (int i = (int)(a); i >= int(b); i--)
#define INF 1000000000
#define INFL 1000000000000000000LL
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()

inline int si(){int tt; scanf("%d", &tt); return tt;}

struct rroot{
	string name;
	int count;
	int froot;
	int link;
	int cnt[1024];
}r[1024];

VVI a;

int n, d, root2;


void dfs(int v)
{
	int res = 0;
	FORSZ(i, a[v])
	{
		int t = a[v][i];
		dfs(t);
		FOR(i, 1023)
			r[v].cnt[i+1] += r[t].cnt[i];
	}
	r[v].cnt[0]++;
	if (d < 1024)
	r[v].count = r[v].cnt[d];
}

void solve()
{
	dfs(root2);
}

int cmp_f(const void *a, const void *b)
{
	rroot *aa = (rroot*)a;
	rroot *bb = (rroot*)b;
	int k = bb->froot - aa->froot;
	if (k) return k;
	k = bb->count - aa->count;
	if (k) return k;
	k = strcmp(aa->name.c_str(), bb->name.c_str());
	return k;
}
 
void result()
{
	qsort(&r[1], 1010, sizeof(r[0]), cmp_f);
	int i = 1, cnt = 0;
	while (i < 1024 && r[i].count && r[i].froot && cnt < 3)
	{
		int j = i;
		while (i < 1024 && r[i].count == r[j].count)
		{
			if (r[i].count)
			{
				printf("%s %d\n", r[i].name.c_str(), r[i].count);
				cnt++;
			}
			i++;
		}
	}
}

char s[1000];

char dict[1024][30];

int idict;

int index(char *s)
{
	FORE(i, 1, idict)
		if (!strcmp(dict[i], s)) return i;
	idict++;
	strcpy(dict[idict], s);
	return idict;
}


int main()
{
#ifdef _DEBUG
	freopen("1064", "r", stdin);
//	freopen("1064w", "w", stdout);
#endif
	int nt = si();
	FORE(it, 1, nt)
	{
		//memset(r, 0, sizeof(r));
		FOR(i, 1024)
		{			
			r[i].count = 0;
			r[i].froot = 0;
			r[i].link = 0;
			memset(r[i].cnt, 0, sizeof(r[i].cnt));
		}
		idict = 0;
		printf("Tree %d:\n", it);
		n = si(); d = si();
		a.assign(1024, VI());
		FOR(i, n)
		{
			scanf("%s", s);
			int k = index(s);
			r[k].name = string(s);
			r[k].froot = 1;
			int kk;
			scanf("%d", &kk);
			FOR(j, kk)
			{
				scanf("%s", s);
				int t = index(s);
				a[k].PB(t);
				r[t].link = 1;
			}
		}
		FORE(i, 1, idict)
			if (!r[i].link)
				root2 = i;
		solve();
		result();
		if (it != nt)
		printf("\n");
	}
	return 0;
}

