#include <bits/stdc++.h>
const int N = 2e5 + 5;
const int M = N << 2;
int n, m, hd[N], E[M], V[M], cnt, out[N];
bool has[N];
inline void read(int &x) {
  x = 0;
  char Cget = getchar();
  while (!isdigit(Cget)) {
    Cget = getchar();
  }
  while (isdigit(Cget)) {
    x = x * 10 + Cget - '0';
    Cget = getchar();
  }
}
inline void EA(int x, int y) {
  E[++cnt] = hd[x];
  V[cnt] = y;
  hd[x] = cnt;
}
int main() {
  int T;
  read(T);
  while (T--) {
    read(n);
    read(m);
    for (int i = 1; i <= n; i++) {
      hd[i] = 0;
      has[i] = false;
    }
    cnt = 0;
    for (int i = 1; i <= m; i++) {
      int x, y;
      read(x);
      read(y);
      EA(y, x);
    }
    cnt = 0;
    for (int i = 1; i <= n; i++) {
      int tmp = 0;
      for (int v = hd[i]; v != 0; v = E[v]) {
        if (!has[V[v]]) {
          for (int e = hd[V[v]]; e != 0; e = E[e]) {
            if (!has[V[e]]) {
              has[i] = true;
              out[++cnt] = i;
              tmp++;
              break;
            }
          }
          if (tmp) {
            break;
          }
        }
      }
    }
    printf("%d\n", cnt);
    for (int i = 1; i <= cnt; i++) {
      printf("%d ", out[i]);
    }
    puts("");
  }
  return 0;
}
