#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

template<class I> vi lis(const vector<I>& S) {
	if (S.empty()) return {};
	vi prev(sz(S));
	typedef pair<I, int> p;
	vector<p> res;
	rep(i,0,sz(S)) {
		// change 0 -> i for longest non-decreasing subsequence
		auto it = lower_bound(all(res), p{S[i], 0});
		if (it == res.end()) res.emplace_back(), it = res.end()-1;
		*it = {S[i], i};
		prev[i] = it == res.begin() ? 0 : (it-1)->second;
	}
	int L = sz(res), cur = res.back().second;
	vi ans(L);
	while (L--) ans[L] = cur, cur = prev[cur];
	return ans;
}


void solve(){
	int N, K;
	cin >> N >> K;
	vector<vector<int>> stuff(N+1);
	for(int i = 0; i < K; i++){
		int L;
		cin >> L;
		vector<int> A(L);
		for(int& x : A){
			cin >> x;
		}
		auto res = lis(A);
		for(int j = 0; j < res[0]; j++){
			stuff[0].push_back(A[j]);
		}
		for(int idx = 0; idx < res.size(); idx++){
			int st = res[idx];
			int en = idx+1 == res.size() ? A.size() : res[idx+1];
			for(int j = st; j < en; j++){
				stuff[A[st]].push_back(A[j]);
			}
		}
	}
	vector<int> ans;
	for(int i = 0; i <= N; i++) ans.insert(ans.end(), stuff[i].begin(), stuff[i].end());
	for(int i = 0; i < N; i++){
		cout << ans[i] << " \n"[i+1 == N];
	}
}

int main(){
	int T;
	cin >> T;
	while(T--) solve();
}
