#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
template <class T, class U>
void chmin(T &t, const U &u) {
  if (t > u) t = u;
}
template <class T, class U>
void chmax(T &t, const U &u) {
  if (t < u) t = u;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  for (int i = 0; i < (v.size()); i++) {
    if (i) os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int tc;
  cin >> tc;
  while (tc--) {
    int n, m;
    cin >> n >> m;
    V<int> ans;
    VV<int> g(n);
    for (int i = 0; i < (m); i++) {
      int a, b;
      cin >> a >> b;
      --a, --b;
      g[a].push_back(b);
    }
    VV<int> st(n, V<int>(3));
    for (int i = 0; i < (n); i++) {
      int c;
      if (st[i][1]) {
        c = 2;
      } else if (st[i][0]) {
        c = 1;
      } else {
        c = 0;
      }
      if (c == 2) ans.push_back(i);
      for (int to : g[i]) {
        st[to][c] = 1;
      }
    }
    cout << ans.size() << '\n';
    for (auto v : ans) cout << v + 1 << ' ';
    cout << '\n';
  }
  return 0;
}
