#include <bits/stdc++.h>
namespace std {
template <typename A, typename B>
inline string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
inline string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
inline string to_string(tuple<A, B, C, D> p);
inline string to_string(const string& s) { return '"' + s + '"'; }
inline string to_string(const char* s) { return to_string((string)s); }
inline string to_string(bool b) { return (b ? "true" : "false"); }
inline string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
template <size_t N>
inline string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}
template <typename A>
inline string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto& x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
template <typename A, typename B>
inline string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
inline string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
inline string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
}  // namespace std
inline void debug_out() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
inline void debug_out(Head H, Tail... T) {
  std::cerr << " " << std::to_string(H);
  debug_out(T...);
}
using namespace std;
int main() {
  int T;
  scanf("%d", &T);
  for (int tt = 0; tt < T; tt++) {
    int n, m;
    scanf("%d%d", &n, &m);
    vector<vector<int>> e(n);
    for (int i = 0; i < m; i++) {
      int v, u;
      scanf("%d%d", &v, &u);
      v--;
      u--;
      e[v].push_back(u);
    }
    vector<int> mask(n, 0);
    vector<int> kill;
    for (int i = 0; i < n; i++) {
      if (mask[i] == 2) {
        continue;
      }
      if (mask[i] == 0) {
        for (auto v : e[i]) {
          if (mask[v] == 0) {
            mask[v] = 1;
          }
        }
      }
      if (mask[i] == 1) {
        for (auto v : e[i]) {
          mask[v] = 2;
        }
      }
    }
    for (int v = 0; v < n; v++) {
      if (mask[v] == 2) {
        kill.push_back(v);
      }
    }
    assert((int)kill.size() * 7 <= n * 4);
    cout << kill.size() << "\n";
    for (auto x : kill) {
      cout << x + 1 << " ";
    }
    puts("");
  }
  return 0;
}
