#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
int main() {
  int t;
  bool ex[300010];
  int n, m;
  int x, y;
  int sz, sz2;
  vector<int> e[300010];
  vector<int> ans;
  scanf("%d", &t);
  for (long long tt = 0; tt < (long long)(t); tt++) {
    scanf("%d %d", &n, &m);
    for (long long i = 0; i < (long long)(n); i++) {
      e[i].clear();
      ex[i] = true;
    }
    for (long long i = 0; i < (long long)(m); i++) {
      scanf("%d %d", &x, &y);
      e[y - 1].push_back(x - 1);
    }
    for (long long i = 0; i < (long long)(n); i++) {
      sz = e[i].size();
      for (long long ii = 0; ii < (long long)(sz); ii++) {
        if (ex[e[i][ii]]) {
          sz2 = e[e[i][ii]].size();
          for (long long jj = 0; jj < (long long)(sz2); jj++) {
            if (ex[e[e[i][ii]][jj]]) ex[i] = false;
          }
        }
      }
    }
    ans.clear();
    for (long long i = 0; i < (long long)(n); i++)
      if (!ex[i]) ans.push_back(i + 1);
    sz = ans.size();
    printf("%d\n", sz);
    for (long long i = 0; i < (long long)(sz - 1); i++) printf("%d ", ans[i]);
    if (sz > 0) printf("%d\n", ans[sz - 1]);
  }
  return 0;
}
