//Done by: K Ashwin
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define REP(i, a, b) \
for (int i = int(a); i <= int(b); i++) // a to b, and variable i is local!
#define TR(c, it) \
for (auto it = (c).begin(); it != (c).end(); it++)
#define s(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define set0(a) memset(a, 0, sizeof(a))
#define setdp(a) memset(a, -1, sizeof(a))
#define INF 2000000000
#define MOD 1000000007
vector <int> adj[20005], ra[20005], v, ans;
int vis[20005], in[20005];
void dfs(int cur)
{
vis[cur] = 1;
TR (adj[cur], it) {
if (!vis[*it])
dfs(*it);
}
}
int main()
{
int t, n, qu, k, x, y, cur;
cin >> t;
while (t--) {
cin >> n >> qu;
REP (i, 1, n) {
adj[i].clear();
ra[i].clear();
}
set0(vis);
set0(in);
REP (i, 1, n) {
s(k);
REP (j, 1, k) {
s(x);
adj[i].pb(x);
ra[x].pb(i);
in[i]++;
}
}
v.clear();
REP (i, 1, qu) {
s(x);
v.pb(x);
}
TR (v, it)
if (!vis[*it])
dfs(*it);
priority_queue <int, vector <int>, greater <int> > pq;
while (!pq.empty())
pq.pop();
REP (i, 1, n) {
//cout << i << " " << vis[i] << " " << in[i] << endl;
if (vis[i] && !in[i])
pq.push(i);
}
ans.clear();
while (!pq.empty()) {
cur = pq.top();
pq.pop();
ans.pb(cur);
TR (ra[cur], it) {
in[*it]--;
if (!in[*it] && vis[*it])
pq.push(*it);
}
}
cout << ans.size() << endl;
TR (ans, it)
cout << *it << " ";
cout << endl;
}
return 0;
}
Ly9Eb25lIGJ5OiBLIEFzaHdpbgoKI2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdHlwZWRlZiBwYWlyPGludCwgaW50PiBwaWk7CgojZGVmaW5lIFJFUChpLCBhLCBiKSBcCmZvciAo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pbik7CgogICAgICAgIFJFUCAoaSwgMSwgbikgewogICAgICAgICAgICBzKGspOwoKICAgICAgICAgICAgUkVQIChqLCAxLCBrKSB7CiAgICAgICAgICAgICAgICBzKHgpOwoKICAgICAgICAgICAgICAgIGFkaltpXS5wYih4KTsKICAgICAgICAgICAgICAgIHJhW3hdLnBiKGkpOwogICAgICAgICAgICAgICAgaW5baV0rKzsKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgdi5jbGVhcigpOwoKICAgICAgICBSRVAgKGksIDEsIHF1KSB7CiAgICAgICAgICAgIHMoeCk7CgogICAgICAgICAgICB2LnBiKHgpOwogICAgICAgIH0KCiAgICAgICAgVFIgKHYsIGl0KQogICAgICAgICAgICBpZiAoIXZpc1sqaXRdKQogICAgICAgICAgICAgICAgZGZzKCppdCk7CgogICAgICAgIHByaW9yaXR5X3F1ZXVlIDxpbnQsIHZlY3RvciA8aW50PiwgZ3JlYXRlciA8aW50PiA+IHBxOwoKICAgICAgICB3aGlsZSAoIXBxLmVtcHR5KCkpCiAgICAgICAgICAgIHBxLnBvcCgpOwoKICAgICAgICBSRVAgKGksIDEsIG4pIHsKICAgICAgICAgICAgLy9jb3V0IDw8IGkgPDwgIiAiIDw8IHZpc1tpXSA8PCAiICIgPDwgaW5baV0gPDwgZW5kbDsKICAgICAgICAgICAgaWYgKHZpc1tpXSAmJiAhaW5baV0pCiAgICAgICAgICAgICAgICBwcS5wdXNoKGkpOwogICAgICAgIH0KCiAgICAgICAgYW5zLmNsZWFyKCk7CiAgICAgICAgd2hpbGUgKCFwcS5lbXB0eSgpKSB7CiAgICAgICAgICAgIGN1ciA9IHBxLnRvcCgpOwogICAgICAgICAgICBwcS5wb3AoKTsKCiAgICAgICAgICAgIGFucy5wYihjdXIpOwoKICAgICAgICAgICAgVFIgKHJhW2N1cl0sIGl0KSB7CiAgICAgICAgICAgICAgICBpblsqaXRdLS07CiAgICAgICAgICAgICAgICBpZiAoIWluWyppdF0gJiYgdmlzWyppdF0pCiAgICAgICAgICAgICAgICAgICAgcHEucHVzaCgqaXQpOwogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICBjb3V0IDw8IGFucy5zaXplKCkgPDwgZW5kbDsKICAgICAgICBUUiAoYW5zLCBpdCkKICAgICAgICAgICAgY291dCA8PCAqaXQgPDwgIiAiOwogICAgICAgIGNvdXQgPDwgZW5kbDsKICAgIH0KCiAgICByZXR1cm4gMDsKfQo=