#include <bits/stdc++.h>
int n, m;
std::vector<int> e[200005];
int bel[200005];
void test() {
  std::cin >> n >> m;
  memset(bel, 0, n + 1 << 2);
  for (int i = 1, iend = n; i <= iend; ++i) e[i].clear();
  for (int i = 1, iend = m; i <= iend; ++i) {
    int u, v;
    std::cin >> u >> v;
    e[v].push_back(u);
  }
  for (int i = 1, iend = n; i <= iend; ++i)
    if (!e[i].empty()) {
      std::sort(e[i].begin(), e[i].end());
      std::unique(e[i].begin(), e[i].end());
    }
  for (int i = 1, iend = n; i <= iend; ++i) {
    for (int to : e[i]) {
      if (bel[to] == 1) bel[i] = 2;
      if (bel[i] != 2 && bel[to] == 0) bel[i] = 1;
    }
  }
  static std::vector<int> v;
  v.clear();
  for (int i = 1, iend = n; i <= iend; ++i)
    if (bel[i] == 2) v.push_back(i);
  std::cout << (int)v.size() << '\n';
  for (int i : v) std::cout << i << ' ';
  std::cout << '\n';
}
int main() {
  std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0);
  int T;
  std::cin >> T;
  while (T--) test();
  return 0;
}
