#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
vector<vi> adj;
vi inc;
vector<int> dangerous_ending;
int main(int argc, char* argv[]) {
  std::ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.precision(10);
  int _testsvariableXXX;
  cin >> _testsvariableXXX;
  while (_testsvariableXXX--) {
    int n, m;
    cin >> n >> m;
    adj.assign(n, {});
    inc.assign(n, {});
    dangerous_ending.assign(n, {});
    for (int i = (0); i < (m); ++i) {
      int u, v;
      cin >> u >> v;
      u--, v--;
      adj[u].push_back(v);
      inc[v]++;
    }
    for (int i = (0); i < (n); ++i) {
      for (int j : adj[i])
        for (int k : adj[j]) dangerous_ending[k]++;
    }
    for (int u = (0); u < (n); ++u) {
      if (dangerous_ending[u]) {
        for (auto n : adj[u]) {
          dangerous_ending[n] -= inc[u];
          inc[n]--;
          for (auto m : adj[n]) dangerous_ending[m]--;
        }
      }
    }
    vi out;
    for (int i = (0); i < (n); ++i)
      if (dangerous_ending[i]) out.push_back(i + 1);
    cout << out.size() << '\n';
    copy((out).begin(), (out).end(), ostream_iterator<int>(cout, " "));
    cout << '\n';
  }
  cout.flush();
  return 0;
}
