#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 30;
const long long linf = 1LL << 62;
const int MAX = 510000;
long long dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
long long dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const double pi = 3.14159265358979;
const double eps = 1e-7;
template <typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2>
inline void print2(T1 a, T2 b) {
  cout << a << " " << b << "\n";
}
template <typename T1, typename T2, typename T3>
inline void print3(T1 a, T2 b, T3 c) {
  cout << a << " " << b << " " << c << "\n";
}
const int mod = 998244353;
void solve() {
  long long n, m;
  cin >> n >> m;
  vector<vector<long long> > G(n);
  for (long long i = 0; i < (m); i++) {
    long long s, t;
    cin >> s >> t;
    s--;
    t--;
    G[s].push_back(t);
  }
  vector<bool> era(n, 0);
  set<long long> ans;
  for (long long i = 0; i < (n); i++) {
    if (ans.find(i) != ans.end()) continue;
    if (era[i]) {
      for (auto j : G[i]) {
        ans.insert(j);
      }
      continue;
    }
    for (auto j : G[i]) {
      era[j] = true;
    }
  }
  cout << ans.size() << "\n";
  for (auto i : ans) cout << i + 1 << " ";
  cout << "\n";
}
int main() {
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
}
