#include <bits/stdc++.h>
using namespace std;
vector<int> v[200008];
int t, n, m, x, y, z[200008];
int main() {
  for (scanf("%d", &t); t--; printf("\n")) {
    for (scanf("%d%d", &n, &m), x = n + 1; --x; z[x] = 0, v[x].clear())
      ;
    for (; m--; scanf("%d%d", &x, &y), v[x].push_back(y))
      ;
    for (x = 0; x++ < n; m += z[x] == 2)
      for (y = v[x].size(); y--; z[v[x][y]] = max(z[v[x][y]], (1 + z[x]) % 3))
        ;
    for (printf("%d\n", m + 1); --x; z[x] == 2 ? printf("%d ", x) : 0)
      ;
  }
  exit(0);
}
