#include <bits/stdc++.h>
using namespace std;
int n, h[200005], tot, m;
struct edge {
  int to, nxt;
} e[200005 << 2];
void add(int x, int y) {
  e[++tot].nxt = h[x];
  h[x] = tot;
  e[tot].to = y;
}
int d[200005];
void work() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) h[i] = d[i] = 0;
  tot = 0;
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    add(y, x);
  }
  vector<int> a;
  for (int x = 1; x <= n; x++) {
    for (int i = h[x]; i; i = e[i].nxt) {
      d[x] = max(d[x], d[e[i].to] + 1);
      if (d[x] == 2) {
        a.push_back(x);
        d[x] = -1;
        break;
      }
    }
  }
  printf("%d\n", (int)a.size());
  for (int i = 0; i < (int)a.size(); i++) printf("%d ", a[i]);
  putchar('\n');
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) work();
  return 0;
}
