#include <bits/stdc++.h>
using namespace std;
long long input() {
  long long x = 0, f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') f |= ch == '-', ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return f ? -x : x;
}
const int N = 2e5 + 7;
vector<int> G[N];
vector<int> Ans;
int cnt[N];
int n, m;
int main() {
  int T = input();
  while (T--) {
    n = input(), m = input();
    for (int i = 1; i <= n; i++) G[i].clear(), cnt[i] = 0;
    Ans.clear();
    for (int i = 1; i <= m; i++) {
      int u = input(), v = input();
      G[u].push_back(v);
    }
    for (int i = 1; i <= n; i++) {
      if (cnt[i] < 2) {
        for (auto v : G[i]) {
          cnt[v] = max(cnt[v], cnt[i] + 1);
        }
      } else
        Ans.push_back(i);
    }
    printf("%d\n", Ans.size());
    for (auto v : Ans) printf("%d ", v);
    printf("\n");
  }
}
