#include <bits/stdc++.h>
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
const long double PI = acos((long double)(-1));
using namespace std;
void solve() {
  int n, m;
  scanf("%d %d", &n, &m);
  vector<vector<int>> g(n);
  vector<int> deg(n);
  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    u--;
    v--;
    g[u].emplace_back(v);
    deg[v]++;
  }
  vector<int> d(n, -1);
  vector<bool> del(n, false);
  queue<int> q;
  for (int i = 0; i < n; i++)
    if (deg[i] == 0) q.push(i), d[i] = 0;
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    if (d[cur] == 2) {
      del[cur] = true;
      for (auto nxt : g[cur]) {
        if (del[nxt]) continue;
        deg[nxt]--;
        if (deg[nxt] == 0) {
          if (d[nxt] == -1) d[nxt] = 0;
          q.emplace(nxt);
        }
      }
    } else {
      for (auto nxt : g[cur]) {
        if (del[nxt]) continue;
        chmax(d[nxt], d[cur] + 1);
        deg[nxt]--;
        if (deg[nxt] == 0) {
          q.emplace(nxt);
        }
      }
    }
  }
  vector<int> res;
  for (int i = 0; i < n; i++)
    if (del[i]) res.emplace_back(i + 1);
  printf("%d\n", (int)res.size());
  for (int i = 0; i < res.size(); i++)
    printf("%d%c", res[i], " \n"[i + 1 == res.size()]);
}
int main() {
  int kkt;
  scanf("%d", &kkt);
  while (kkt--) {
    solve();
  }
  return 0;
}
