#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 1e6 + 10;
int h[N], e[N], ne[N], idx;
int t, n, m;
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
int main()
{
cin >> t;
while (t--)
{
memset(h, -1, sizeof h);
idx = 0;
int n, m;
scanf("%d%d", &n, &m);
while (m--)
{
int a, b;
scanf("%d%d", &a, &b);
add(a, b);
}
for (int i = 1; i <= n; i++)
{
for (int j = h[i]; j != -1; j = ne[j])
{
printf("%d ", e[j]);
}
puts("");
}
}
return 0;
}