#include <bits/stdc++.h>
#pragma GCC optimize(3, "Ofast", "inline")
using namespace std;
const double esp = 1e-6, pi = acos(-1);
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
long long in() {
  long long first = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) first = first * 10 + ch - 48, ch = getchar();
  return first * f;
}
vector<int> v[N];
int vis[N];
void work() {
  int n = in(), m = in();
  for (int i = 1; i <= n; i++) v[i].clear(), vis[i] = false;
  for (int i = 1; i <= m; i++) {
    int first = in(), second = in();
    v[second].push_back(first);
  }
  vector<int> ans;
  for (int i = 1; i <= n; i++) {
    int flag = 0;
    for (auto first : v[i]) {
      if (vis[first]) continue;
      for (auto second : v[first]) {
        if (vis[second]) continue;
        vis[i] = 1;
        ans.push_back(i);
        flag = 1;
        break;
      }
      if (flag) break;
    }
  }
  cout << ans.size() << endl;
  for (auto first : ans) cout << first << " ";
  puts("");
  return;
}
int main() {
  int t = in();
  while (t--) work();
  return 0;
}
