#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
constexpr int MX = 2e5 + 5;
int t[MX];
vector<int> G[MX];
int main() {
  if (fopen("in", "r")) freopen("in", "r", stdin), freopen("out", "w", stdout);
  ios_base::sync_with_stdio(0), cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int n, m;
    cin >> n >> m;
    for (int u = 1; u <= n; ++u) t[u] = 0, G[u].clear();
    for (int i = 0; i < m; ++i) {
      int x, y;
      cin >> x >> y;
      G[y].push_back(x);
    }
    int ans = 0;
    for (int u = 1; u <= n; ++u) {
      for (int v : G[u]) {
        if (t[v] == 0)
          t[u] = 1;
        else if (t[v] == 1) {
          t[u] = 2, ++ans;
          break;
        }
      }
    }
    cout << ans << '\n';
    for (int u = 1; u <= n; ++u)
      if (t[u] == 2) cout << u << ' ';
    cout << '\n';
  }
}
