#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  int fl = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') fl = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  x *= fl;
}
template <typename T, typename... Args>
inline void read(T &t, Args &...args) {
  read(t);
  read(args...);
}
const int N = 200005;
int T, n, m;
vector<int> edge[N];
int main() {
  for (read(T); T; T--) {
    read(n, m);
    for (int i = 1; i <= n; i++) edge[i].clear();
    vector<int> deg(n + 1), level(n + 1, 1);
    for (int i = 1; i <= m; i++) {
      int u, v;
      read(u, v);
      edge[u].emplace_back(v);
      deg[v]++;
    }
    vector<int> ans;
    for (int i = 1; i <= n; i++) {
      if (level[i] == 3) {
        ans.emplace_back(i);
        continue;
      }
      for (auto &to : edge[i]) level[to] = max(level[to], level[i] + 1);
    }
    printf("%d\n", (int)ans.size());
    for (auto &k : ans) printf("%d ", k);
    puts("");
  }
  return 0;
}
