#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
class ESkiAccidents {
 public:
  void calc(istream& cin, ostream& cout) {
    int n, m;
    cin >> n >> m;
    vector<int> g[n];
    vector<int> dp(n);
    vector<int> ret;
    for (int i = 0; i < (int)(m); i++) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      g[a].push_back(b);
    }
    for (int i = 0; i < (int)(n); i++) {
      if (dp[i] == 0) {
        for (auto e : g[i]) {
          chmax(dp[e], 1);
        }
      } else if (dp[i] == 1) {
        for (auto e : g[i]) {
          chmax(dp[e], 2);
        }
      } else {
        ret.push_back(i);
      }
    }
    cout << ret.size() << '\n';
    assert(ret.size() * 7 <= n * 4);
    for (auto e : ret) {
      cout << e + 1 << ' ';
    }
    cout << '\n';
  }
  void solve(istream& cin, ostream& cout) {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
      calc(cin, cout);
    }
  }
};
signed main() {
  ESkiAccidents solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}
