#include <bits/stdc++.h>
const int N = 3e5;
int t, n, m, h[N], val[N];
std::set<int> adj[N], jda[N], res;
std::map<int, std::set<int>> map;
void reset() {
  for (int i = 1; i <= n; ++i) {
    adj[i].clear();
    jda[i].clear();
    h[i] = 0;
    val[i] = 0;
  }
  map.clear();
  res.clear();
}
void dfs(int u) {
  if (h[u]) return;
  for (int v : adj[u]) {
    dfs(v);
    h[u] = std::max(h[u], h[v]);
  }
  map[-++h[u]].insert(u);
}
void turn_off(int u) {
  val[u] = n;
  for (int v : adj[u]) --val[v];
}
void turn_on(int u) {
  --val[u];
  for (int v : adj[u])
    if (!++val[v]) turn_off(v);
}
int greedy(int u) {
  if (!val[u]) {
    for (int v : jda[u]) assert(val[v] > 0);
    --val[u];
    for (int v : adj[u])
      if (!val[v])
        turn_on(v);
      else if (~val[v])
        val[v] = n;
  }
  return val[u];
}
void solve() {
  scanf("%d%d", &n, &m);
  reset();
  for (int i = 0; i < m; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    adj[u].insert(v);
    jda[v].insert(u);
  }
  for (int i = 1; i <= n; ++i) dfs(i);
  for (auto pset : map)
    for (int x : pset.second)
      if (greedy(x) > 0) res.insert(x);
  printf("%u\n", res.size());
  for (int x : res) printf("%d ", x);
  printf("\n");
}
int main() {
  scanf("%d", &t);
  for (int i = 0; i < t; ++i) solve();
}
