#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
const int inf = 1 << 30;
const int maxn = 2e5 + 5;
inline void fastio() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
int n, m;
int main() {
  fastio();
  int _, u, v;
  for (cin >> _; _; _--) {
    cin >> n >> m;
    vector<int> in(n, 0), layer(n, 0), ans;
    vector<vector<int>> g(n);
    for (int i = (1); i <= (m); i++) {
      cin >> u >> v;
      u--, v--;
      g[u].push_back(v);
      in[v]++;
    }
    queue<int> q;
    for (int i = (0); i <= (n - 1); i++)
      if (!in[i]) {
        q.push(i);
        layer[i] = 1;
      }
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      if (layer[u] == 3) {
        layer[u] = 0;
        ans.push_back(u);
      }
      for (int v : g[u]) {
        in[v]--;
        layer[v] = max(layer[v], layer[u] + 1);
        if (!in[v]) {
          q.push(v);
        }
      }
    }
    cout << ans.size() << endl;
    for (int x : ans) cout << x + 1 << " ";
    cout << endl;
  }
  return 0;
}
