#include <bits/stdc++.h>
using namespace std;
constexpr int seed = 131;
constexpr long long LLMAX = 2e18;
constexpr int MOD = 1e9 + 7;
constexpr double eps = 1e-8;
constexpr int MAXN = 1e6 + 10;
constexpr int hmod1 = 0x48E2DCE7;
constexpr int hmod2 = 0x60000005;
inline long long sqr(long long x) { return x * x; }
inline int sqr(int x) { return x * x; }
inline double sqr(double x) { return x * x; }
inline int mul(int x, int y) { return 1ll * x * y % MOD; }
inline int sub(int x, int y) { return x >= y ? x - y : MOD + x - y; }
inline int add(int x, int y) { return x + y >= MOD ? x + y - MOD : x + y; }
inline int dcmp(double x) {
  if (fabs(x) < eps) return 0;
  return (x > 0 ? 1 : -1);
}
inline long long qpow(long long a, long long n) {
  long long sum = 1;
  while (n) {
    if (n & 1) sum = sum * a % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return sum;
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long exgcd(long long a, long long b, long long &x, long long &y) {
  long long d;
  (b == 0 ? (x = 1, y = 0, d = a)
          : (d = exgcd(b, a % b, y, x), y -= a / b * x));
  return d;
}
vector<int> G[MAXN];
int col[MAXN], in[MAXN];
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    memset(in, 0, (n + 1) * sizeof(int));
    memset(col, -1, (n + 1) * sizeof(int));
    for (int i = 0; i < m; i++) {
      int u, v;
      cin >> u >> v;
      G[u].push_back(v);
      in[v]++;
    }
    queue<int> q;
    for (int i = 1; i <= n; i++)
      if (!in[i]) {
        q.push(i);
        col[i] = 0;
      }
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (int i = 0; i < (int)G[u].size(); i++) {
        int v = G[u][i];
        in[v]--;
        if (col[u] < 2)
          col[v] = max(col[v], col[u] + 1);
        else
          col[v] = max(col[v], 0);
        if (in[v] == 0) q.push(v);
      }
    }
    for (int i = 1; i <= n; i++) G[i].clear();
    vector<int> st;
    for (int i = 1; i <= n; i++)
      if (col[i] == 2) st.push_back(i);
    cout << st.size() << endl;
    for (auto i : st) cout << i << ' ';
    cout << endl;
  }
  return 0;
}
