#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX=2e5+5;

set< int > adj[MX];

int main(){
     int i, j, k;
     int t;
     cin >> t;
     while(t--){
          int n, m;
          cin >> n >> m;
          vector< int > indegree(n+1, 0), outdegree(n+1, 0);
          for(i=1; i<=n; ++i) adj[i].clear();
          for(i=0; i<m; ++i){
               int x, y;
               cin >> x >> y;
               adj[x].insert(y);
               indegree[y]++, outdegree[x]++;
          }
          if(n<=3){
               cout << 1 << endl << 2 << endl;
               continue;
          }
          vector< int > ans;
          for(i=1; i<=n; ++i){
               if(indegree[i] && outdegree[i]){
                    //cout << i << endl;
                    ans.push_back(i);
                    for(auto x:adj[i]){
                         indegree[x]--;
                    }
               }
          }
          random_shuffle(ans.begin(), ans.end());
          vector< int > show;
          for(i=1; i*7<=4*n && i<=ans.size(); ++i) show.push_back(ans[i-1]);
          cout << show.size() << endl;
          for(auto x:show) cout << x << " ";
          cout << endl;
     }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = (int)2e5 + 10;
int dp[N];

int n;
int gen(){
  return ((int)rng() % n + n) % n;
}
vector<int> T[N];

void solve(){
  int m;
  cin >> n >> m;
  for(int i = 1; i <= n; i ++ ){
    T[i].clear();
  }
  int u, v;
  for(int i = 1; i <= m; i ++ ){
    cin >> u >> v;
    T[u].push_back(v);
  }
  int lim = 4 * n / 7;
  vector<int> lis(n);
  for(int i = 1; i <= n; i ++ )
    lis[i-1]=i;
  int x, y;
  bool bad;
  while(1){ // lololololol
    bad=false;
    for(int it = 0 ;it < n + 10; it ++ ){
      x = gen();
      y = gen();
      swap(lis[x],lis[y]);
    }
    for(int i = 1; i <= n; i ++ ){
      dp[i] = 0;
    }
    for(int i = 0 ; i < lim ;i ++ ){
      dp[lis[i]] = -10;
    }
    for(int i = n; i >= 1; i -- ){
      if(dp[i] < 0) continue;
      for(auto x : T[i]){
        if(dp[x] < 0) continue;
        dp[i]=max(dp[i],dp[x]+1);
      }
      if(dp[i]>=2){
        bad=true;
        break;
      }
    }
    if(!bad){
      cout << lim << "\n";
      for(int i = 0 ; i < lim; i ++ )
        cout << lis[i] << " ";
      cout << "\n";
      break;
    }
  }
}

int main(){
  fastIO;
  int tc;
  cin >> tc;
  for(int t = 0; t < tc; t ++ ){
    solve();
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N = (int)2e5 + 10;
int dp[N];
vector<int> T[N];
int eli[N];

void solve(){
  int n, m;
  cin >> n >> m;
  int u, v;
  for(int i = 1; i <= n; i ++ ){
    T[i].clear();
    dp[i]=-1;
    eli[i]=false;
  }
  for(int i = 0 ; i < n; i ++ ){
    cin >> u >> v;
    T[u].push_back(v);
  }
  vector<vector<int>> chains;
  for(int i = n; i >= 1; i -- ){
    dp[i]=0;
    for(auto x : T[i]){
      dp[i]=max(dp[i],dp[x]+1);
    }
  }
  int cur;
  for(int i = 1; i <= n; i ++ ){
    cur = i;
    vector<int> psh;
    while(1){
      psh.push_back(cur);
      int id = -1;
      for(auto x : T[cur]){
        if(id == -1 || dp[x] > dp[id]){
          id = x;
        }
      }
      if(id == -1) break; // irrelevent
      if(T[cur][0] == id){
        T[cur].erase(T[cur].begin());
      }
      else{
        T[cur].pop_back();
      }
      cur = id;
    }
    if(psh.size() >= 3){
      chains.push_back(psh);
    }
  }
  sort(chains.begin(), chains.end());
  reverse(chains.begin(), chains.end());
  for(auto x : chains){
    for(int i = 0 ; i < x.size() ; i += 2){
      eli[x[i]]=true;
    }
  }
  vector<int> ans;
  for(int i = 1; i <= n; i ++ )
    if(eli[i] == false) ans.push_back(i);
  cout << ans.size() << "\n";
  for(auto x : ans) cout << x << " ";
  cout << "\n";
}

int main(){
  fastIO;
  int tc;
  cin >> tc;
  for(int t = 0; t < tc; t ++ ){
    solve();
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int N = (int)1e5 + 10;
vector<int> T[N], R[N];
bool has[N];

void solve(){
    int n, m;
    cin >> n >> m;
    vector<int> sol;
    for(int i = 1; i <= n; i ++ ){
        T[i].clear();
        R[i].clear();
        has[i]=false;
    }
    int u, v;
    for(int i = 1; i <= m; i ++ ){
        cin >> u >> v;
        T[u].push_back(v);
        R[v].push_back(u);
    }
    bool keep;
    for(int i = 1; i <= n; i ++ ){
        keep=true;
        for(auto x : R[i]){
            if(has[x]){
                keep=false;
            }
        }
        if(!keep){
            has[i]=false;
            sol.push_back(i);
        }
        else{
            for(auto x : T[i]){
                has[x]=true;
            }
        }
    }
    cout << sol.size() << "\n";
    for(auto x : sol)
        cout << x << " ";
    cout << "\n";
}

int main(){
    fastIO;
    int tc;
    cin >> tc;
    for(int t = 1; t <= tc; t ++ )
        solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N = (int)2e5 + 10;
int dp[N];
vector<int> T[N];
int eli[N];

void solve(){
  int n, m;
  cin >> n >> m;
  int u, v;
  for(int i = 1; i <= n; i ++ ){
    T[i].clear();
    dp[i]=-1;
    eli[i]=false;
  }
  for(int i = 0 ; i < m; i ++ ){
    cin >> u >> v;
    T[u].push_back(v);
  }
  vector<vector<int>> chains;
  for(int i = n; i >= 1; i -- ){
    dp[i]=0;
    for(auto x : T[i]){
      dp[i]=max(dp[i],dp[x]+1);
    }
  }
  int cur;
  for(int i = 1; i <= n; i ++ ){
    cur = i;
    vector<int> psh;
    while(1){
      psh.push_back(cur);
      int id = -1;
      for(auto x : T[cur]){
        if(id == -1 || dp[x] > dp[id]){
          id = x;
        }
      }
      if(id == -1) break; // irrelevent
      if(T[cur][0] == id){
        T[cur].erase(T[cur].begin());
      }
      else{
        T[cur].pop_back();
      }
      cur = id;
    }
    if(psh.size() >= 3){
      chains.push_back(psh);
    }
  }
  sort(chains.begin(), chains.end());
  reverse(chains.begin(), chains.end());
  for(auto x : chains){
    for(int i = 0 ; i < x.size() ; i += 2){
      eli[x[i]]=true;
    }
  }
  vector<int> ans;
  for(int i = 1; i <= n; i ++ )
    if(eli[i] == false) ans.push_back(i);
  cout << ans.size() << "\n";
  for(auto x : ans) cout << x << " ";
  cout << "\n";
}

int main(){
  fastIO;
  int tc;
  cin >> tc;
  for(int t = 0; t < tc; t ++ ){
    solve();
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = (int)2e5 + 10;
int dp[N];

int n;
int gen(){
  return ((int)rng() % n + n) % n;
}
vector<int> T[N];

bool comp(int x, int y){
  return ((int)T[x].size() < (int)T[y].size());
}

void solve(){
  int m;
  cin >> n >> m;
  for(int i = 1; i <= n; i ++ ){
    T[i].clear();
  }
  int u, v;
  for(int i = 1; i <= m; i ++ ){
    cin >> u >> v;
    T[u].push_back(v);
  }
  int lim = 4 * n / 7;
  vector<int> lis(n);
  for(int i = 1; i <= n; i ++ )
    lis[i-1]=i;
  int x, y;
  bool bad;
  sort(lis.begin(), lis.end(), comp);
  reverse(lis.begin(), lis.end());
  for(int it = 0 ;it < 40; it ++ ){
    bad=false;
    for(int i = 1; i <= n; i ++ ){
      dp[i] = 0;
    }
    for(int i = 0 ; i < lim ;i ++ ){
      dp[lis[i]] = -10;
    }
    for(int i = n; i >= 1; i -- ){
      if(dp[i] < 0) continue;
      for(auto x : T[i]){
        if(dp[x] < 0) continue;
        dp[i]=max(dp[i],dp[x]+1);
      }
    }
    if(!bad){
      cout << lim << "\n";
      for(int i = 0 ; i < lim; i ++ )
        cout << lis[i] << " ";
      cout << "\n";
      break;
    }
    for(int it = 0 ;it < n + 10; it ++ ){
      x = gen();
      y = gen();
      swap(lis[x],lis[y]);
    }
  }
}

int main(){
  fastIO;
  int tc;
  cin >> tc;
  for(int t = 0; t < tc; t ++ ){
    solve();
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = (int)2e5 + 10;
int dp[N];

int n;
int gen(){
  return ((int)rng() % n + n) % n;
}
vector<int> T[N];

bool comp(int x, int y){
  return ((int)T[x].size() < (int)T[y].size());
}

void solve(){
  int m;
  cin >> n >> m;
  for(int i = 1; i <= n; i ++ ){
    T[i].clear();
  }
  int u, v;
  for(int i = 1; i <= m; i ++ ){
    cin >> u >> v;
    T[u].push_back(v);
  }
  int lim = 4 * n / 7;
  vector<int> lis(n);
  for(int i = 1; i <= n; i ++ )
    lis[i-1]=i;
  int x, y;
  bool bad;
  sort(lis.begin(), lis.end(), comp);
  reverse(lis.begin(), lis.end());
  while(1){
    bad=false;
    for(int i = 1; i <= n; i ++ ){
      dp[i] = 0;
    }
    for(int i = 0 ; i < lim ;i ++ ){
      dp[lis[i]] = -10;
    }
    for(int i = n; i >= 1; i -- ){
      if(dp[i] < 0) continue;
      for(auto x : T[i]){
        if(dp[x] < 0) continue;
        dp[i]=max(dp[i],dp[x]+1);
      }
    }
    if(!bad){
      cout << lim << "\n";
      for(int i = 0 ; i < lim; i ++ )
        cout << lis[i] << " ";
      cout << "\n";
      break;
    }
    for(int it = 0 ;it < n + 10; it ++ ){
      x = gen();
      y = gen();
      swap(lis[x],lis[y]);
    }
  }
}

int main(){
  fastIO;
  int tc;
  cin >> tc;
  for(int t = 0; t < tc; t ++ ){
    solve();
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int N = (int)2e5 + 10;
vector<int> T[N], R[N];
bool has[N];

void solve(){
    int n, m;
    cin >> n >> m;
    vector<int> sol;
    for(int i = 1; i <= n; i ++ ){
        T[i].clear();
        R[i].clear();
        has[i]=false;
    }
    int u, v;
    for(int i = 1; i <= m; i ++ ){
        cin >> u >> v;
        T[u].push_back(v);
        R[v].push_back(u);
    }
    bool keep;
    for(int i = 1; i <= n; i ++ ){
        keep=true;
        for(auto x : R[i]){
            if(has[x]){
                keep=false;
            }
        }
        if(!keep){
            has[i]=false;
            sol.push_back(i);
        }
        else{
            for(auto x : T[i]){
                has[x]=true;
            }
        }
    }
    cout << sol.size() << "\n";
    for(auto x : sol)
        cout << x << " ";
    cout << "\n";
}

int main(){
    fastIO;
    int tc;
    cin >> tc;
    for(int t = 1; t <= tc; t ++ )
        solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int N = (int)1e5 + 10;
vector<int> T[N], R[N];
bool has[N];

void solve(){
    int n, m;
    cin >> n >> m;
    vector<int> sol;
    for(int i = 1; i <= n; i ++ ){
        T[i].clear();
        R[i].clear();
        has[i]=false;
    }
    int u, v;
    for(int i = 1; i <= m; i ++ ){
        cin >> u >> v;
        T[u].push_back(v);
        R[v].push_back(u);
    }
    bool keep;
    for(int i = 1; i <= n; i ++ ){
        keep=true;
        for(auto x : R[i]){
            if(has[x]){
                keep=false;
            }
        }
        if(!keep){
            sol.push_back(i);
        }
        else{
            for(auto x : T[i]){
                has[x]=true;
            }
        }
    }
    cout << sol.size() << "\n";
    for(auto x : sol)
        cout << x << " ";
    cout << "\n";
}

int main(){
    fastIO;
    int tc;
    cin >> tc;
    for(int t = 1; t <= tc; t ++ )
        solve();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using lint = long long;

void Solve() {
  int N, M, K;
  cin >> N >> M;

  vector<vector<int>> adj(N);
  for (int i = 0; i < M; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].emplace_back(v);
  }
  vector<int> len(N, 0);
  vector<int> ans;
  for (int i = 0; i < N; i++) {
    if (len[i] == 2) {
      ans.emplace_back(i);
    } else {
      for (auto j : adj[i]) {
        len[j] = max(len[j], len[i] + 1);
      }
    }
  }

  cout << ans.size() << "\n";
  for (auto i : ans) {
    cout << i + 1 << " ";
  }
  cout << "\n";
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int T;
  cin >> T;
  while (T--) {
    Solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
using lint = long long;

void Solve() {
  int N, M, K;
  cin >> N >> M;
  K =  (4 * N) / 7;

  vector<vector<int>> adj(N);
  for (int i = 0; i < M; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].emplace_back(v);
  }

  vector<int> block(N, 0);
  vector<int> level(N, -1);
  set<int> vis;

  function<void(int, int)> Dfs = [&](int u, int d) {
    if (block[u]) return;
    if (d > 2) return;
    vis.emplace(u);
    level[u] = max(level[u], d);
    for (auto v : adj[u]) {
      Dfs(v, d + 1);
    }
  };

  vector<int> order(N, 0);
  iota(begin(order), end(order), 0);

  vector<int> ans;
  int done = 0;

  auto Run = [&]() {
    block.assign(N, 0);
    level.assign(N, -1);
    int r = 0;
    while (r < N) {
      for (auto v : vis) {
        level[v] = -1;
      }
      vis.clear();
      Dfs(order[r], 0);
      int blocked = 0;
      int haveto = 0;
      for (auto v : vis) {
        if (level[v] == 2) {
          haveto = 1;
        }
      }
      for (auto v : vis) {
        if (level[v] == 1 && adj[v].size() == 2) {
          block[v] = 1;
          blocked = 1;
        }
      }
      if (blocked) {
        continue;
      }
      for (auto v : vis) {
        if (level[v] == 2 && adj[v].size() == 2) {
          block[v] = 1;
          blocked = 1;
        }
      }
      if (blocked) {
        continue;
      }
      for (auto v : vis) {
        if (level[v] == 2) {
          block[v] = 1;
          blocked = 1;
        }
      }
      if (!blocked) {
        r++;
      }
    }
    int cnt = 0;
    for (int i = 0; i < N; i++) {
      if (block[i]) {
        cnt++;
      }
    }
    if (cnt <= K) {
      done = 1;
      for (int i = 0; i < N; i++) {
        if (block[i]) {
          ans.emplace_back(i);
        }
      }
    }
  };
  Run();

  mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
  while (!done) {
    shuffle(begin(order), end(order), rnd);
    Run();
  }

  cout << ans.size() << "\n";
  for (auto i : ans) cout << i + 1 << " ";
  cout << "\n";
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int T;
  cin >> T;
  while (T--) {
    Solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
using lint = long long;

void Solve() {
  int N, M, K;
  cin >> N >> M;
  K =  (4 * N) / 7;

  vector<vector<int>> adj(N);
  for (int i = 0; i < M; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].emplace_back(v);
  }

  vector<int> block(N, 0);
  vector<int> level(N, -1);
  set<int> vis;

  function<void(int, int)> Dfs = [&](int u, int d) {
    if (block[u]) return;
    if (d > 2) return;
    vis.emplace(u);
    level[u] = max(level[u], d);
    for (auto v : adj[u]) {
      Dfs(v, d + 1);
    }
  };

  vector<int> order(N, 0);
  iota(begin(order), end(order), 0);

  vector<int> ans;
  int done = 0;

  auto Run = [&]() {
    int r = 0;
    while (r < N) {
      for (auto v : vis) {
        level[v] = -1;
      }
      vis.clear();
      Dfs(order[r], 0);
      int blocked = 0;
      int haveto = 0;
      for (auto v : vis) {
        if (level[v] == 2) {
          haveto = 1;
        }
      }
      for (auto v : vis) {
        if (level[v] == 1 && adj[v].size() == 2) {
          block[v] = 1;
          blocked = 1;
        }
      }
      if (blocked) {
        continue;
      }
      for (auto v : vis) {
        if (level[v] == 2 && adj[v].size() == 2) {
          block[v] = 1;
          blocked = 1;
        }
      }
      if (blocked) {
        continue;
      }
      for (auto v : vis) {
        if (level[v] == 2) {
          block[v] = 1;
          blocked = 1;
        }
      }
      if (!blocked) {
        r++;
      }
    }
    int cnt = 0;
    for (int i = 0; i < N; i++) {
      if (block[i]) {
        cnt++;
      }
    }
    if (cnt <= K) {
      done = 1;
      for (int i = 0; i < N; i++) {
        if (block[i]) {
          ans.emplace_back(i);
        }
      }
    }
  };
  Run();

  mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
  while (!done) {
    shuffle(begin(order), end(order), rnd);
    Run();
  }

  cout << ans.size() << "\n";
  for (auto i : ans) cout << i + 1 << " ";
  cout << "\n";
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int T;
  cin >> T;
  while (T--) {
    Solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
using lint = long long;

void Solve() {
  int N, M, K;
  cin >> N >> M;
  K =  (4 * N) / 7;

  vector<vector<int>> adj(N);
  for (int i = 0; i < M; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].emplace_back(v);
  }

  vector<int> block(N, 0);
  vector<int> level(N, -1);
  set<int> vis;

  function<void(int, int)> Dfs = [&](int u, int d) {
    if (block[u]) return;
    if (d > 2) return;
    vis.emplace(u);
    level[u] = max(level[u], d);
    int rep = 0;
    for (auto v : adj[u]) {
      if (u == v) {
        d++;
      }
    }
    for (auto v : adj[u]) {
      if (v != u) {
        Dfs(v, d + 1);
      }
    }
  };

  int r = 0;
  while (r < N) {
    vis.clear();
    Dfs(r, 0);
    int blocked = 0;
    for (auto v : vis) {
      if (level[v] == 2) {
        block[v] = 1;
        blocked = 1;
      }
      level[v] = -1;
    }
    if (!blocked) {
      r++;
    }
  }
  vector<int> ans;
  for (int i = 0; i < N; i++) {
    if (block[i]) {
      ans.emplace_back(i);
    }
  }

  assert(ans.size() <= K);
  cout << ans.size() << "\n";
  for (auto i : ans) cout << i + 1 << " ";
  cout << "\n";
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int T;
  cin >> T;
  while (T--) {
    Solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
using lint = long long;

void Solve() {
  int N, M, K;
  cin >> N >> M;
  K =  (4 * N) / 7;

  vector<vector<int>> adj(N);
  for (int i = 0; i < M; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].emplace_back(v);
  }

  vector<int> block(N, 0);
  vector<int> level(N, -1);
  set<int> vis;

  function<void(int, int)> Dfs = [&](int u, int d) {
    if (block[u]) return;
    if (d > 2) return;
    vis.emplace(u);
    level[u] = max(level[u], d);
    for (auto v : adj[u]) {
      Dfs(v, d + 1);
    }
  };

  int r = 0;
  while (r < N) {
    vis.clear();
    Dfs(r, 0);
    int blocked = 0;
    int haveto = 0;
    for (auto v : vis) {
      if (level[v] == 2) {
        haveto = 1;
      }
    }
    for (auto v : vis) {
      if (level[v] == 1) {
        block[v] = 1;
        blocked = 1;
      }
      level[v] = -1;
    }
    if (!blocked) {
      r++;
    }
  }
  vector<int> ans;
  for (int i = 0; i < N; i++) {
    if (block[i]) {
      ans.emplace_back(i);
    }
  }

  cout << ans.size() << "\n";
  for (auto i : ans) cout << i + 1 << " ";
  cout << "\n";
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int T;
  cin >> T;
  while (T--) {
    Solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
using lint = long long;

void Solve() {
  int N, M, K;
  cin >> N >> M;
  K =  (4 * N) / 7;

  vector<vector<int>> adj(N);
  for (int i = 0; i < M; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].emplace_back(v);
  }

  vector<int> block(N, 0);
  vector<int> level(N, -1);
  set<int> vis;

  function<void(int, int)> Dfs = [&](int u, int d) {
    if (block[u]) return;
    if (d > 2) return;
    vis.emplace(u);
    level[u] = max(level[u], d);
    for (auto v : adj[u]) {
      Dfs(v, d + 1);
    }
  };

  vector<int> order(N, 0);
  iota(begin(order), end(order), 0);

  vector<int> ans;
  int done = 0;

  auto Run = [&]() {
    block.assign(N, 0);
    level.assign(N, -1);
    int r = 0;
    while (r < N) {
      for (auto v : vis) {
        level[v] = -1;
      }
      vis.clear();
      Dfs(order[r], 0);
      int blocked = 0;
      int cnt1 = 0, cnt2 = 0;
      for (auto v : vis) {
        if (level[v] == 1) {
          cnt1++;
        }
        if (level[v] == 2) {
          cnt2++;
        }
      }
      if (cnt2 == 0) {
        r++;
        continue;
      }
      if (cnt1 + 1 < cnt2) {
        for (auto v : vis) {
          if (level[v] == 1) {
            block[v] = 1;
            blocked = 1;
          }
        }
      } else {
        for (auto v : vis) {
          if (level[v] == 2) {
            block[v] = 1;
            blocked = 1;
          }
        }
      }
    }
    int cnt = 0;
    for (int i = 0; i < N; i++) {
      if (block[i]) {
        cnt++;
      }
    }
    if (cnt <= K) {
      done = 1;
      for (int i = 0; i < N; i++) {
        if (block[i]) {
          ans.emplace_back(i);
        }
      }
    }
  };
  Run();

  mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
  while (!done) {
    shuffle(begin(order), end(order), rnd);
    Run();
  }

  cout << ans.size() << "\n";
  for (auto i : ans) cout << i + 1 << " ";
  cout << "\n";
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int T;
  cin >> T;
  while (T--) {
    Solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
using lint = long long;

void Solve() {
  int N, M, K;
  cin >> N >> M;
  K =  (4 * N) / 7;

  vector<vector<int>> adj(N);
  for (int i = 0; i < M; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].emplace_back(v);
  }

  vector<int> block(N, 0);
  vector<int> level(N, -1);
  set<int> vis;

  function<void(int, int)> Dfs = [&](int u, int d) {
    if (block[u]) return;
    if (d > 2) return;
    vis.emplace(u);
    level[u] = max(level[u], d);
    for (auto v : adj[u]) {
      Dfs(v, d + 1);
    }
  };

  int r = 0;
  while (r < N) {
    for (auto v : vis) {
      level[v] = -1;
    }
    vis.clear();
    Dfs(r, 0);
    int blocked = 0;
    int haveto = 0;
    for (auto v : vis) {
      if (level[v] == 2) {
        haveto = 1;
      }
    }
    for (auto v : vis) {
      if (level[v] == 1 && adj[v].size() == 2) {
        block[v] = 1;
        blocked = 1;
      }
    }
    if (blocked) {
      continue;
    }
    for (auto v : vis) {
      if (level[v] == 2) {
        block[v] = 1;
        blocked = 1;
      }
    }
    if (!blocked) {
      r++;
    }
  }
  vector<int> ans;
  for (int i = 0; i < N; i++) {
    if (block[i]) {
      ans.emplace_back(i);
    }
  }

  assert(ans.size() <= K);
  cout << ans.size() << "\n";
  for (auto i : ans) cout << i + 1 << " ";
  cout << "\n";
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int T;
  cin >> T;
  while (T--) {
    Solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
using lint = long long;

void Solve() {
  int N, M, K;
  cin >> N >> M;
  K =  (4 * N) / 7;

  vector<vector<int>> adj(N);
  for (int i = 0; i < M; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].emplace_back(v);
  }

  vector<int> block(N, 0);
  vector<int> level(N, -1);
  set<int> vis;

  function<void(int, int)> Dfs = [&](int u, int d) {
    if (block[u]) return;
    if (d > 2) return;
    vis.emplace(u);
    level[u] = max(level[u], d);
    for (auto v : adj[u]) {
      Dfs(v, d + 1);
    }
  };

  vector<int> order(N, 0);
  iota(begin(order), end(order), 0);

  vector<int> ans;
  int done = 0;
  vector<int> blocks(N);
  auto Run = [&]() {
    block.assign(N, 0);
    level.assign(N, -1);
    for (int i = 0; i < K; i++) {
      block[blocks[i]] = 1;
    }
    set<int> need;
    for (int i = 0; i < N; i++) {
      vis.clear();
      Dfs(i, 0);
      for (auto v : vis) {
        if (level[v] == 2) {
          need.emplace(v);
        }
        level[v] = -1;
      }
    }
    if (need.empty()) {
      done = 1;
      for (int i = 0; i < K; i++) {
        ans.emplace_back(blocks[i]);
      }
      return;
    }
    vector<int> new_blocks(begin(need), end(need));
    for (int i = 0; i < N; i++) {
      if (need.count(blocks[i]) == 0) {
        new_blocks.emplace_back(blocks[i]);
      }
    }
    blocks = new_blocks;
  };

  mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

  iota(begin(blocks), end(blocks), 0);
  while (!done) {
    Run();
  }

  cout << ans.size() << "\n";
  for (auto i : ans) cout << i + 1 << " ";
  cout << "\n";
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int T;
  cin >> T;
  while (T--) {
    Solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
using lint = long long;

void Solve() {
  int N, M, K;
  cin >> N >> M;
  K =  (4 * N) / 7;

  vector<vector<int>> adj(N);
  for (int i = 0; i < M; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].emplace_back(v);
  }

  vector<int> block(N, 0);
  vector<int> level(N, -1);
  set<int> vis;

  function<void(int, int)> Dfs = [&](int u, int d) {
    if (block[u]) return;
    if (d > 2) return;
    vis.emplace(u);
    level[u] = max(level[u], d);
    for (auto v : adj[u]) {
      Dfs(v, d + 1);
    }
  };

  int r = 0;
  while (r < N) {
    for (auto v : vis) {
      level[v] = -1;
    }
    vis.clear();
    Dfs(r, 0);
    int blocked = 0;
    int haveto = 0;
    for (auto v : vis) {
      if (level[v] == 2) {
        haveto = 1;
      }
    }
    for (auto v : vis) {
      if (level[v] == 1 && adj[v].size() == 2) {
        block[v] = 1;
        blocked = 1;
      }
    }
    if (blocked) {
      continue;
    }
    for (auto v : vis) {
      if (level[v] == 2 && adj[v].size() == 2) {
        block[v] = 1;
        blocked = 1;
      }
    }
    if (blocked) {
      continue;
    }
    for (auto v : vis) {
      if (level[v] == 2) {
        block[v] = 1;
        blocked = 1;
      }
    }
    if (!blocked) {
      r++;
    }
  }
  vector<int> ans;
  for (int i = 0; i < N; i++) {
    if (block[i]) {
      ans.emplace_back(i);
    }
  }

  assert(ans.size() <= K);
  cout << ans.size() << "\n";
  for (auto i : ans) cout << i + 1 << " ";
  cout << "\n";
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int T;
  cin >> T;
  while (T--) {
    Solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
using lint = long long;

void Solve() {
  int N, M, K;
  cin >> N >> M;
  K =  (4 * N) / 7;

  vector<vector<int>> adj(N);
  for (int i = 0; i < M; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].emplace_back(v);
  }

  vector<int> block(N, 0);
  vector<int> level(N, -1);
  set<int> vis;

  function<void(int, int)> Dfs = [&](int u, int d) {
    if (block[u]) return;
    if (d > 2) return;
    vis.emplace(u);
    int rep = 0;
    for (auto v : adj[u]) {
      if (u == v) {
        d++;
      }
    }
    level[u] = max(level[u], d);
    for (auto v : adj[u]) {
      if (v != u) {
        Dfs(v, d + 1);
      }
    }
  };

  int r = 0;
  while (r < N) {
    vis.clear();
    Dfs(r, 0);
    int blocked = 0;
    for (auto v : vis) {
      if (level[v] == 2) {
        block[v] = 1;
        blocked = 1;
      }
      level[v] = -1;
    }
    if (!blocked) {
      r++;
    }
  }
  vector<int> ans;
  for (int i = 0; i < N; i++) {
    if (block[i]) {
      ans.emplace_back(i);
    }
  }

  cout << ans.size() << "\n";
  for (auto i : ans) cout << i + 1 << " ";
  cout << "\n";
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int T;
  cin >> T;
  while (T--) {
    Solve();
  }
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define pb push_back
#define y second
#define x first

using namespace std;
using INT=long long;
using VI=vector<int>;
using pii=pair<int, int>;

#define NN 202020

VI adj[NN];
int vst[NN], runs;
int ans[NN], nn;

inline bool can(int a) {
	for(int b: adj[a]) if(vst[b]!=runs) {
		for(int c: adj[b]) if(vst[c]!=runs) {
			return false;
		}
	}
	return true;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int t; scanf("%d", &t);
	while(t--) {
		int n, m; scanf("%d %d", &n, &m);
		for(int i=1; i<=n; i++) adj[i].clear();
		while(m--) {
			int u, v; scanf("%d %d", &u, &v);
			adj[v].pb(u);
		}
		runs++, nn=0;
		for(int i=1; i<=n; i++) if(!can(i)) {
			vst[i]=runs, ans[nn++]=i;
		}
		printf("%d\n", nn);
		for(int i=0; i<nn; i++) printf("%d ", ans[i]);
		puts("");
	}
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define pb push_back
#define y second
#define x first

using namespace std;
typedef long long INT;
typedef vector<int> VI;
typedef pair<int, int> pii;

int N;
queue<int> q;

#define NN 202020

int vst[NN], indeg[NN];
VI ans, adj[NN], V;
set<int> s;

void del(int u) {
	if(N) {
		N--, vst[u]=1;
		ans.pb(u);
	}
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int t; scanf("%d", &t);
	while(t--) {
		int n, m; scanf("%d %d", &n, &m);
		for(int i=1; i<=n; i++) indeg[i]=0, vst[i]=0;
		while(m--) {
			int u, v; scanf("%d %d", &u, &v);
			adj[u].pb(v), indeg[v]++;
		}
		N=4*n/7, ans.clear();

		s.clear();
		for(int i=1; i<=n; i++) s.insert(i);

		while(N) {
			bool ok=false;
			V.clear();
			for(int a: s) {
				bool flag=true;
				for(int u: adj[a]) if(!vst[u]) {
					for(int v: adj[u]) if(!vst[v]) {
						flag=false;
						del(u), V.pb(u);
						ok=true;
						break;
					}
					if(!flag) break;
				}
				if(flag) V.pb(a);
			}
			for(int x: V) if(s.find(x)!=s.end()) {
				s.erase(x);
			}
			if(!ok) break;
		}

		printf("%d\n", ans.size());
		for(int x: ans) printf("%d ", x);
		puts("");
	}
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define pb push_back
#define y second
#define x first

using namespace std;
typedef long long INT;
typedef vector<int> VI;
typedef pair<int, int> pii;

int N;
queue<int> q;

#define NN 202020

int vst[NN], indeg[NN];
VI ans, adj[NN], V;
set<int> s;

void del(int u) {
	if(N) {
		N--, vst[u]=1;
		ans.pb(u);
	}
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int t; scanf("%d", &t);
	while(t--) {
		int n, m; scanf("%d %d", &n, &m);
		for(int i=1; i<=n; i++) indeg[i]=0, vst[i]=0;

		s.clear();
		for(int i=1; i<=n; i++) s.insert(i);
		N=4*n/7, ans.clear();

		while(m--) {
			int u, v; scanf("%d %d", &u, &v);
			if(u==v) {
				if(s.find(u)!=s.end()) s.erase(u);
				del(u);
			}
			else adj[u].pb(v), indeg[v]++;
		}

		while(N) {
			bool ok=false;
			V.clear();
			for(int a: s) {
				bool flag=true;
				for(int u: adj[a]) if(!vst[u]) {
					for(int v: adj[u]) if(!vst[v]) {
						flag=false;
						del(u), V.pb(u);
						ok=true;
						break;
					}
					if(!flag) break;
				}
				if(flag) V.pb(a);
			}
			for(int x: V) if(s.find(x)!=s.end()) {
				s.erase(x);
			}
			if(!ok) break;
		}

		printf("%d\n", ans.size());
		for(int x: ans) printf("%d ", x);
		puts("");
	}
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define pb push_back
#define y second
#define x first

using namespace std;
typedef long long INT;
typedef vector<int> VI;
typedef pair<int, int> pii;

int N;
queue<int> q;

#define NN 202020

int vst[NN], indeg[NN];
VI ans, adj[NN];

void del(int u) {
	if(N) {
		N--, vst[u]=1;
		ans.pb(u);
	}
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int t; scanf("%d", &t);
	while(t--) {
		int n, m; scanf("%d %d", &n, &m);
		for(int i=1; i<=n; i++) indeg[i]=0, vst[i]=0;
		while(m--) {
			int u, v; scanf("%d %d", &u, &v);
			adj[u].pb(v), indeg[v]++;
		}
		N=4*n/7;

		q=queue<int> ();
		for(int i=1; i<=n; i++) if(!indeg[i]) {
			vst[i]=1, q.push(i);
		}

		ans.clear();
		while(!q.empty()) {
			int u=q.front(); q.pop();
			for(int v: adj[u]) if(!vst[v]) {
				bool flag=false;
				for(int c: adj[u]) if(!vst[v]) {
					flag=true; break;
				}
				if(flag) {
					del(v);
					for(int c: adj[v]) if(!vst[v]) {
						vst[c]=1; q.push(c);
					}
				}
				if(!vst[v]) ans.pb(v);
			}
		}

		printf("%d\n", ans.size());
		for(int x: ans) printf("%d ", x);
		puts("");
	}
}


#include <bits/stdc++.h>
#define pb push_back

using namespace std;
typedef vector<int> VI;

#define NN 200010
int vst[NN];
VI adj[NN];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int T;
	scanf("%d",&T);
	while(T--){
		int n,m;
		scanf("%d%d",&n,&m);
		while(m--){
			int u,v;
			scanf("%d%d",&u,&v);
			adj[u].pb(v);
		}
		VI ans;
		for(int i=1;i<=n;i++){
			if(vst[i]==2){
				ans.pb(i);
				continue;
			}
			if(vst[i]==1){
				for(int v:adj[i]) vst[v]=2;
			}
			else{
				for(int v:adj[i]) vst[v]=1;
			}
		}
		printf("%d\n",ans.size());
		for(int i=0;i<ans.size();i++) printf("%d ",ans[i]);
		puts("");
		for(int i=1;i<=n;i++) vst[i]=0;
		for(int i=1;i<=n;i++) adj[i].clear();
	}
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
using namespace std;
const int NN=200200;
vector <int> adj[NN],ans;
bool fl[NN],vst[NN];
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int T;
	scanf("%d",&T);
	while(T--){
		int n,m;
		scanf("%d%d",&n,&m);
		while(m--){
			int u,v;
			scanf("%d%d",&u,&v);
			adj[u].pb(v);
		}
		for(int i=1;i<=n;i++){
			if(fl[i]) {ans.pb(i);continue;}
			if(vst[i]==1) for(int v:adj[i]) fl[v]=1;
			else for(int v:adj[i]) vst[v]=1;
		}
		printf("%d\n",ans.size());
		for(int i=0;i<ans.size();i++) printf("%d ",ans[i]);
		puts("");
		for(int i=1;i<=n;i++) vst[i]=fl[i]=0;
		for(int i=1;i<=n;i++) adj[i].clear();
		ans.clear();
	}
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define NN 200010

int cnt[NN];
int flag[NN];
vector<int> adj[NN];

int ok(int u) {
	for(int v : adj[u]) if(!flag[v]) {
		for(int x : adj[v]) if(!flag[x]) return 1;
	}
	return 0;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("E.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int T; cin >> T;

	while(T--) {
		int n, m; scanf("%d %d", &n, &m);
		for(int i=1; i<=n; i++) {
			adj[i].clear();
			flag[i] = 0;
		}
		for(int i=1; i<=m; i++) {
			int u, v; scanf("%d %d", &u, &v);
			adj[v].push_back(u);
		}
		vector<int> ans;
		for(int i=1; i<=n; i++) if(ok(i)) {
			flag[i] = 1; ans.push_back(i);
		}
		printf("%d\n", ans.size());
		for(int x : ans) printf("%d ", x); puts("");
	}

	return 0;
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define pb push_back
#define y second
#define x first

using namespace std;
typedef long long INT;
typedef vector<int> VI;
typedef pair<int, int> pii;

#define NN 202020

int a[NN], vst[NN];
set<int> s[20];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int n; scanf("%d", &n);
	for(int i=1; i<=n; i++) scanf("%d", a+i);
	sort(a+1, a+n+1, [](int u, int v) {return u>v;});

	for(int i=1; i<=n; i++) {
		for(int j=20; j--; ) if((a[i]>>j)&1) {
			s[j].insert(i);
		}
	}

	for(int i=1; i<=n; i++) {
		for(int j=20; j--; ) if(!((a[i]>>j)&1)) {
			while(!s[j].empty() && *s[j].begin()<=i) {
				s[j].erase(s[j].begin());
			}
			if(!s[j].empty()) {
				int u=*s[j].rbegin();
				int And=a[i]&a[u], Or=a[i]|a[u];
				if((INT)a[i]*a[i] + (INT)a[u]*a[u] < (INT)And*And + (INT)Or*Or) {
					s[j].erase(--s[j].end());
					a[i]=Or, a[u]=And;

					for(int k=20; k--; ) if((a[u]>>k)&1) {
						if(s[k].find(u)!=s[k].end()) s[k].erase(u);
					}
					for(int k=20; k--; ) if((a[u]>>k)&1) {
						if(s[k].find(u)!=s[k].end()) s[k].insert(u);
					}
				}
			}
		}
	}

	INT ans=0;
	for(int i=1; i<=n; i++) {
		ans+=(INT)a[i]*a[i];
	}
	cout<<ans;
}


#include <bits/stdc++.h>
#define NN 201000
using namespace std;

typedef long long INT;

bool vst[NN];

vector <int> ans;
vector <int> adj[NN];

bool check(int u){
	for(auto v : adj[u]) if(!vst[v]){
		for(auto k : adj[v]) if(!vst[k]) return 1;
	}
	return 0;
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int test;
	scanf("%d", &test);
	while(test--){
		int n, m;
		scanf("%d%d", &n, &m);
		ans.clear();
		for(int i = 1; i <= n; i++) {
			adj[i].clear();
			vst[i] = 0;
		}
		for(int i = 1; i <= m; i++){
			int u, v;
			scanf("%d%d", &u, &v);
//			adj[u].push_back(v);
			adj[v].push_back(u);
		}
		for(int i = 1; i <= n; i++){
			if(vst[i]) continue;
			if(check(i)) ans.push_back(i), vst[i] = 1;
		}
		printf("%d\n", ans.size());
		for(int i = 0; i < ans.size(); i++){
			if(i == ans.size() - 1) printf("%d\n", ans[i]);
			else printf("%d ", ans[i]);
		}
	}
	return 0;
}


#include<bits/stdc++.h>

#define NN 200100

using namespace std;

typedef long long INT;

vector<int>adj[NN];
int flag[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int t;
	cin>>t;
	while(t--){
		int n,m;
		cin>>n>>m;
		for(int i=1;i<=m;i++){
			int u,v;
			scanf("%d %d",&u,&v);
			adj[u].push_back(v);
		}

		vector<int>res;

		for(int u=1;u<=n;u++){
			if(flag[u]==1){
				for(int v:adj[u]){
					flag[v]=2;
				}
				continue;
			}
			if(flag[u]==2){
				res.push_back(u);
				continue;
			}
			for(int v:adj[u]){
				flag[v]=max(flag[v],1);
			}
		}

		cout<<res.size()<<endl;
		for(int v:res)printf("%d ",v);
		cout<<endl;
		for(int i=1;i<=n;i++)adj[i].clear();
		for(int i=1;i<=n;i++)flag[i]=0;
	}

	return 0;
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define pb push_back
#define y second
#define x first

using namespace std;
using INT=long long;
using VI=vector<int>;
using pii=pair<int, int>;

#define NN 202020

VI adj[NN];
int vst[NN], runs;
int ans[NN], nn;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int t; scanf("%d", &t);
	while(t--) {
		int n, m; scanf("%d %d", &n, &m);
		for(int i=1; i<=n; i++) adj[i].clear();
		while(m--) {
			int u, v; scanf("%d %d", &u, &v);
			adj[u].pb(v);
		}
		runs++, nn=0;
		for(int i=1; i<=n; i++) if(vst[i]!=runs) {
			for(int u: adj[i]) if(vst[u]!=runs) {
				for(int v: adj[u]) if(vst[v]!=runs) {
					vst[v]=runs, ans[nn++]=v;
				}
			}
		}
		printf("%d\n", nn);
		for(int i=0; i<nn; i++) printf("%d ", ans[i]);
		puts("");
	}
}


#include<bits/stdc++.h>

#define NN 200100

using namespace std;

typedef long long INT;

vector<int>adj[NN];
int flag[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int t;
	cin>>t;
	while(t--){
		int n,m;
		cin>>n>>m;
		for(int i=1;i<=m;i++){
			int u,v;
			scanf("%d %d",&u,&v);
			adj[u].push_back(v);
		}

		vector<int>res;
		for(int u=1;u<=n;u++){
			if(flag[u]==-1)continue;
			if(flag[u]){
				res.push_back(u);
				continue;
			}
			flag[u]=-1;
			for(int v:adj[u]){
				if(flag[v]==1)continue;
				flag[v]=-1;
				for(int c:adj[v]){
					flag[c]=1;
				}
			}
		}
		cout<<res.size()<<endl;
		for(int v:res)printf("%d ",v);
		cout<<endl;
		for(int i=1;i<=n;i++)adj[i].clear();
		for(int i=1;i<=n;i++)flag[i]=0;
	}

	return 0;
}


#include<bits/stdc++.h>

#define NN 200100

using namespace std;

typedef long long INT;

vector<int>adj[NN];
int flag[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int t;
	cin>>t;
	while(t--){
		int n,m;
		cin>>n>>m;
		for(int i=1;i<=m;i++){
			int u,v;
			scanf("%d %d",&u,&v);
			adj[u].push_back(v);
		}

		vector<int>res;

		for(int u=1;u<=n;u++){
			if(flag[u]==1){
				for(int v:adj[u]){
					flag[v]=2;
				}
				continue;
			}
			if(flag[u]==2){
				res.push_back(u);
				continue;
			}
			for(int v:adj[u]){
				flag[v]=1;
			}
		}
		cout<<res.size()<<endl;
		for(int v:res)printf("%d ",v);
		cout<<endl;
		for(int i=1;i<=n;i++)adj[i].clear();
		for(int i=1;i<=n;i++)flag[i]=0;
	}

	return 0;
}


#include <bits/stdc++.h>
#define pb push_back

using namespace std;
typedef vector<int> VI;
#define NN 200200
int vst[NN];
VI adj[NN];
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int T;
	scanf("%d",&T);
	while(T--){
		int n,m;
		scanf("%d%d",&n,&m);
		while(m--){
			int u,v;
			scanf("%d%d",&u,&v);
			adj[u].pb(v);
		}
		VI ans;
		for(int i=1;i<=n;i++){
			if(vst[i]==2){
				ans.pb(i);
				continue;
			}
			if(vst[i]==1){
				for(int v:adj[i]) vst[v]=2;
			}
			else{
				for(int v:adj[i]) vst[v]=max(vst[v],1);
			}
		}
		printf("%d\n",ans.size());
		for(int i=0;i<ans.size();i++) printf("%d ",ans[i]);
		puts("");
		for(int i=1;i<=n;i++) vst[i]=0;
		for(int i=1;i<=n;i++) adj[i].clear();
	}
    return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <climits>
#include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define in insert
#define vll vector<ll>
#define endl "\n"
#define pll pair<ll,ll>
#define f first
#define s second
#define int ll
#define sz(x) (ll)x.size()
#define all(x) (x.begin(),x.end())
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> os;

const ll INF = 1e18;
const ll N =(2e5+5);
void solve(){
    ll n,m;
    cin >> n >> m;
    vll adj[n+1];
    for(int i =1;i<=m;i++){
        ll u,v;
        cin >> u >> v;
        adj[u].pb(v);
        // adj[v].pb(u);
    }
    set<ll> ans;
    for(int i =1;i<=n;i++){
        if(adj[i].empty()) continue;
        for(auto j:adj[i]){
            if(adj[j].empty()) continue;
            for(auto k:adj[j]){
                //ans.in(i);
                ans.in(j);
                //ans.in(k);
            }
        }
    }
    cout << sz(ans) << endl;
    for(auto x:ans) cout << x << " ";cout<<endl;
}

signed main(){

    ios_base::sync_with_stdio(0);
    cin.tie(NULL);

     ll tt=1;
     cin >> tt;
    while(tt--){
        solve();
    }
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <climits>
#include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define in insert
#define vll vector<ll>
#define endl "\n"
#define pll pair<ll,ll>
#define f first
#define s second
#define int ll
#define sz(x) (ll)x.size()
#define all(x) (x.begin(),x.end())
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> os;

const ll INF = 1e18;
const ll N =(2e5+5);
void solve(){
    ll n,m;
    cin >> n >> m;
    vll adj[n+1];
    for(int i =1;i<=m;i++){
        ll u,v;
        cin >> u >> v;
        adj[u].pb(v);
        // adj[v].pb(u);
    }
    set<ll> ans;
    for(int i =n;i>=1;i--){
        if(adj[i].empty()) continue;
        for(auto j:adj[i]){
            if(adj[j].empty()) continue;
            for(auto k:adj[j]){
                //ans.in(i);
                ans.in(k);
                //ans.in(k);
            }
        }
    }
    cout << sz(ans) << endl;
    for(auto x:ans) cout << x << " ";cout<<endl;
}

signed main(){

    ios_base::sync_with_stdio(0);
    cin.tie(NULL);

     ll tt=1;
     cin >> tt;
    while(tt--){
        solve();
    }
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <climits>
#include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define in insert
#define vll vector<ll>
#define endl "\n"
#define pll pair<ll,ll>
#define f first
#define s second
#define int ll
#define sz(x) (ll)x.size()
#define all(x) (x.begin(),x.end())
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> os;

const ll INF = 1e18;
const ll N =(2e5+5);
void solve(){
    ll n,m;
    cin >> n >> m;
    vll adj[n+1];
    for(int i =1;i<=m;i++){
        ll u,v;
        cin >> u >> v;
        adj[u].pb(v);
        // adj[v].pb(u);
    }
    set<ll> ans;
    for(int i =1;i<=n;i++){
        if(adj[i].empty()) continue;
        for(auto j:adj[i]){
            if(adj[j].empty()) continue;
            for(auto k:adj[j]){
                //ans.in(i);
                ans.in(k);
                //ans.in(k);
            }
        }
    }
    cout << sz(ans) << endl;
    for(auto x:ans) cout << x << " ";cout<<endl;
}

signed main(){

    ios_base::sync_with_stdio(0);
    cin.tie(NULL);

     ll tt=1;
     cin >> tt;
    while(tt--){
        solve();
    }
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <climits>
#include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define in insert
#define vll vector<ll>
#define endl "\n"
#define pll pair<ll,ll>
#define f first
#define s second
#define int ll
#define sz(x) (ll)x.size()
#define all(x) (x.begin(),x.end())
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> os;

const ll INF = 1e18;
const ll N =(2e5+5);
ll path[N];
void solve(){
    ll n,m;
    cin >> n >> m;
    vll adj[n+1];
    vector<bool> vis(n+1,false);
    for(int i =1;i<=n;i++)path[i]=0;
    for(int i =1;i<=m;i++){
        ll u,v;
        cin >> u >> v;
        adj[v].pb(u);
        // adj[v].pb(u);
    }
    set<ll> ans;
    for(int i =1;i<=n;i++ ){
        for(auto j:adj[i]){
            path[i] = max(path[i],path[j]+1);
        }
        if(path[i] == 2){
            ans.in(i);
            path[i] = -1;
        }
    }
    assert(sz(ans)*7 <= 4*n);
    cout << sz(ans) << endl;
    for(auto x:ans) cout << x << " ";cout<<endl;
}

signed main(){

    ios_base::sync_with_stdio(0);
    cin.tie(NULL);

     ll tt=1;
     cin >> tt;
    while(tt--){
        solve();
    }
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <climits>
#include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define in insert
#define vll vector<ll>
#define endl "\n"
#define pll pair<ll,ll>
#define f first
#define s second
#define int ll
#define sz(x) (ll)x.size()
#define all(x) (x.begin(),x.end())
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> os;

const ll INF = 1e18;
const ll N =(2e5+5);
ll path[N];
void solve(){
    ll n,m;
    cin >> n >> m;
    vll adj[n+1];
    vector<bool> vis(n+1,false);
    for(int i =1;i<=m;i++){
        ll u,v;
        cin >> u >> v;
        adj[v].pb(u);
        // adj[v].pb(u);
    }
    set<ll> ans;
    for(int i =1;i<=n;i++){
        for(auto j:adj[i]){
            path[i] = max(path[i],path[j]+1);
        }
        if(path[i] == 2){
            ans.in(i);
            path[i] = -1;
        }
    }
    cout << sz(ans) << endl;
    for(auto x:ans) cout << x << " ";cout<<endl;
}

signed main(){

    ios_base::sync_with_stdio(0);
    cin.tie(NULL);

     ll tt=1;
     cin >> tt;
    while(tt--){
        solve();
    }
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <climits>
#include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define in insert
#define vll vector<ll>
#define endl "\n"
#define pll pair<ll,ll>
#define f first
#define s second
#define int ll
#define sz(x) (ll)x.size()
#define all(x) (x.begin(),x.end())
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> os;

const ll INF = 1e18;
const ll N =(2e5+5);
ll path[N];
void solve(){
    ll n,m;
    cin >> n >> m;
    vll adj[n+1];
    vector<bool> vis(n+1,false);
    for(int i =1;i<=m;i++){
        ll u,v;
        cin >> u >> v;
        adj[v].pb(u);
        // adj[v].pb(u);
    }
    set<ll> ans;
    for(int i =1;i<=n;i++ ){
        for(auto j:adj[i]){
            path[i] = max(path[i],path[j]+1);
        }
        if(path[i] == 2){
            vis[i]=true;
            path[i] = -1;
        }
    }
    for(int i =1;i<=n;i++)
        if(vis[i])
            ans.in(i);
    cout << sz(ans) << endl;
    for(auto x:ans) cout << x << " ";cout<<endl;
}

signed main(){

    ios_base::sync_with_stdio(0);
    cin.tie(NULL);

     ll tt=1;
     cin >> tt;
    while(tt--){
        solve();
    }
}


//#pragma GCC optimize ("O3")
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
#define all(v) (v).begin(), (v).end()

const int N = 1e5 + 7, M = 2 * N;

int n, m, arr[N];

vector<int> adj[N];
int par[N];
int cando;
int ind[N], outd[N];

int cnt;
vector<int> deleted;

bool solve(int i) {
    if (cnt > cando) {
        return 0;
    }
    if (i == n) {
        cout << cnt << "\n";
        for (auto v : deleted) {
            cout << v << " ";
        }
        cout << "\n";
        return 1;
    }
    int r = rand() % 2;
    if (r == 0) {
        if (!(ind[i] && outd[i])) {
            if (solve(i + 1)) return 1;
        }

        for (auto v : adj[i])
            --ind[v];
        --outd[par[i]];
        cnt++;
        deleted.push_back(i + 1);
        if (solve(i + 1)) return 1;
        deleted.pop_back();
        --cnt;
        for (auto v : adj[i])
            ++ind[v];
        ++outd[par[i]];
        return 0;
    }else{
        for (auto v : adj[i])
            --ind[v];
        --outd[par[i]];
        cnt++;
        deleted.push_back(i + 1);
        if (solve(i + 1)) return 1;
        deleted.pop_back();
        --cnt;
        for (auto v : adj[i])
            ++ind[v];
        ++outd[par[i]];

        if (!(ind[i] && outd[i])) {
            if (solve(i + 1)) return 1;
        }

        return 0;
    }
}

int a, b;

int main() {
    srand(time(NULL));
    ios_base::sync_with_stdio(0), cin.tie(0);
#ifdef CLION
    freopen("input.in", "rt", stdin);
#endif
    int tests;
    cin >> tests;
    while (tests--) {
        deleted.clear();
        cnt = 0;
        cin >> n >> m;
        for (int i = 0; i < n; ++i) {
            adj[i].clear();
        }
        for (int i = 0; i < m; ++i) {
            cin >> a >> b;
            --a, --b;
            ind[b]++;
            outd[a]++;
            par[b] = a;
            adj[a].push_back(b);
        }

        cando = 4 * n / 7;
        solve(0);
    }
}


//#pragma GCC optimize ("O3")
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
#define all(v) (v).begin(), (v).end()

const int N = 1e5 + 7, M = 2 * N;

int n, m, arr[N];

vector<int> adj[N];
int par[N];
int cando;
int ind[N], outd[N];

int cnt;
vector<int> deleted;

bool solve(int i) {
    if (cnt > cando) {
        return 0;
    }
    if (i == n) {
        cout << cnt << "\n";
        for (auto v : deleted) {
            cout << v << " ";
        }
        cout << "\n";
        return 1;
    }

    if (!(ind[i] && outd[i])) {
        if (solve(i + 1)) return 1;
    }

    for (auto v : adj[i])
        --ind[v];
    if(~par[i]) --outd[par[i]];
    cnt++;
    deleted.push_back(i + 1);

    if (solve(i + 1)) return 1;

    deleted.pop_back();
    --cnt;
    for (auto v : adj[i])
        ++ind[v];
    if(~par[i]) ++outd[par[i]];
    return 0;
}

int a, b;

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
#ifdef CLION
    freopen("input.in", "rt", stdin);
#endif
    int tests;
    cin >> tests;
    while (tests--) {
        deleted.clear();
        cnt = 0;
        cin >> n >> m;
        for (int i = 0; i < n; ++i) {
            adj[i].clear();
            ind[i] = outd[i] = 0;
            par[i] = -1;
        }
        for (int i = 0; i < m; ++i) {
            cin >> a >> b;
            --a, --b;
            ind[b]++;
            outd[a]++;
            par[b] = a;
            adj[a].push_back(b);
        }

        cando = 4 * n / 7;
        solve(0);
    }
}


//#pragma GCC optimize ("O3")
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
#define all(v) (v).begin(), (v).end()

const int N = 3e5 + 7, M = 2 * N;

int n, m, arr[N];

vector<int> adj[N];
int par[N];
int cando;
int ind[N], outd[N];

int cnt;
vector<int> deleted;

bool solve(int i) {
    if (cnt > cando) {
        return 0;
    }
    if (i == n) {
        cout << cnt << "\n";
        for (auto v : deleted) {
            cout << v << " ";
        }
        cout << "\n";
        return 1;
    }

    if (!(ind[i] && outd[i])) {
        if (solve(i + 1)) return 1;
    }

    for (auto v : adj[i])
        --ind[v];
    if(~par[i]) --outd[par[i]];
    cnt++;
    deleted.push_back(i + 1);

    if (solve(i + 1)) return 1;

    deleted.pop_back();
    --cnt;
    for (auto v : adj[i])
        ++ind[v];
    if(~par[i]) ++outd[par[i]];
    return 0;
}

int a, b;

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
#ifdef CLION
    freopen("input.in", "rt", stdin);
#endif
    int tests;
    cin >> tests;
    while (tests--) {
        deleted.clear();
        cnt = 0;
        cin >> n >> m;
        for (int i = 0; i < n; ++i) {
            adj[i].clear();
            ind[i] = outd[i] = 0;
            par[i] = -1;
        }
        for (int i = 0; i < m; ++i) {
            cin >> a >> b;
            --a, --b;
            ind[b]++;
            outd[a]++;
            par[b] = a;
            adj[a].push_back(b);
        }

        cando = 4 * n / 7;
        solve(0);
    }
}


//#pragma GCC optimize ("O3")
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
#define all(v) (v).begin(), (v).end()

const int N = 3e5 + 7, M = 2 * N;

int n, m, arr[N];

vector<int> adj[N];
int cando;
int ind[N], outd[N];
int removed[N];
int must[N];

int cnt;

bool solve(int i) {
    if (cnt > cando) {
        return 0;
    }
    if (i == n) {
        return 1;
    }

    if (!must[i] && !(ind[i] && outd[i])) {
        int org[2];
        for (int x = 0; x < adj[i].size(); ++x) {
            org[x] = must[adj[i][x]];
            must[adj[i][x]] = 1;
        }
        if (solve(i + 1)) return 1;

        for (int x = 0; x < adj[i].size(); ++x) {
            must[adj[i][x]] = org[x];
        }
    }

    for (auto v : adj[i])
        --ind[v];
    cnt++;
    removed[i] = 1;
    if (solve(i + 1)) return 1;

    removed[i] = 0;

    --cnt;
    for (auto v : adj[i])
        ++ind[v];
    return 0;
}

int a, b;

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
#ifdef CLION
    freopen("input.in", "rt", stdin);
#endif
    int tests;
    cin >> tests;
    while (tests--) {
        cnt = 0;
        cin >> n >> m;
        for (int i = 0; i < n; ++i) {
            adj[i].clear();
            must[i] = removed[i] = ind[i] = outd[i] = 0;

        }
        for (int i = 0; i < m; ++i) {
            cin >> a >> b;
            --a, --b;
            ind[b]++;
            outd[a]++;
            adj[a].push_back(b);
        }

        cando = 4 * n / 7;
        solve(0);
        cout << cnt << "\n";
        for (int i = 0; i < n; ++i) {
            if (removed[i])
                cout << i + 1 << " ";
        }
        cout << "\n";
    }
}


//#pragma GCC optimize ("O3")
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
#define all(v) (v).begin(), (v).end()

const int N = 3e5 + 7, M = 2 * N;

int n, m, arr[N];

vector<int> adj[N];
int cando;
int ind[N], outd[N];
int removed[N];
int must[N];

int cnt;

bool solve(int i) {
    if (cnt > cando) {
        return 0;
    }
    if (i == n) {
        return 1;
    }

    if (!must[i]) {
        if (ind[i] && outd[i]) {
            int org[2];
            for (int x = 0; x < adj[i].size(); ++x) {
                org[x] = must[adj[i][x]];
                must[adj[i][x]] = 1;
            }
            if (solve(i + 1)) return 1;

            for (int x = 0; x < adj[i].size(); ++x) {
                must[adj[i][x]] = org[x];
            }
        }else{
            if (solve(i + 1)) return 1;
        }
    }

    for (auto v : adj[i])
        --ind[v];
    cnt++;
    removed[i] = 1;
    if (solve(i + 1)) return 1;

    removed[i] = 0;

    --cnt;
    for (auto v : adj[i])
        ++ind[v];
    return 0;
}

int a, b;

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
#ifdef CLION
    freopen("input.in", "rt", stdin);
#endif
    int tests;
    cin >> tests;
    while (tests--) {
        cnt = 0;
        cin >> n >> m;
        for (int i = 0; i < n; ++i) {
            adj[i].clear();
            must[i] = removed[i] = ind[i] = outd[i] = 0;

        }
        for (int i = 0; i < m; ++i) {
            cin >> a >> b;
            --a, --b;
            ind[b]++;
            outd[a]++;
            adj[a].push_back(b);
        }

        cando = 4 * n / 7;
        solve(0);
        cout << cnt << "\n";
        for (int i = 0; i < n; ++i) {
            if (removed[i])
                cout << i + 1 << " ";
        }
        cout << "\n";
    }
}


//#pragma GCC optimize ("O3")
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
#define all(v) (v).begin(), (v).end()

const int N = 4e5 + 7, M = 2 * N;

int n, m, arr[N];

vector<int> adj[N];
int cando;
int ind[N], outd[N];
int removed[N];

int cnt;

bool solve(int i) {
    if (cnt > cando) {
        return 0;
    }
    if (i == n) {

        return 1;
    }

    int dont = 0;
    for (auto v : adj[i])
        dont += removed[v];

    if (!(ind[i] && (outd[i] - dont))) {
        if (solve(i + 1)) return 1;
    }

    for (auto v : adj[i])
        --ind[v];
    cnt++;
    removed[i] = 1;
    if (solve(i + 1)) return 1;

    removed[i] = 0;

    --cnt;
    for (auto v : adj[i])
        ++ind[v];
    return 0;
}

int a, b;

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
#ifdef CLION
    freopen("input.in", "rt", stdin);
#endif
    int tests;
    cin >> tests;
    while (tests--) {
        cnt = 0;
        cin >> n >> m;
        for (int i = 0; i < n; ++i) {
            adj[i].clear();
            removed[i] = ind[i] = outd[i] = 0;

        }
        for (int i = 0; i < m; ++i) {
            cin >> a >> b;
            --a, --b;
            ind[b]++;
            outd[a]++;
            adj[a].push_back(b);
        }

        cando = 4 * n / 7;
        solve(0);
        cout << cnt << "\n";
        for (int i = 0; i < n; ++i) {
            if (removed[i])
                cout << i + 1 << " ";
        }
        cout << "\n";
    }
}


//#pragma GCC optimize ("O3")
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
#define all(v) (v).begin(), (v).end()

const int N = 1e5 + 7, M = 2 * N;

int n, m, arr[N];

vector<int> adj[N];
int par[N];
int cando;
int ind[N], outd[N];

int cnt;
vector<int> deleted;

bool solve(int i) {
    if (cnt > cando) {
        return 0;
    }
    if (i == n) {
        cout << cnt << "\n";
        for (auto v : deleted) {
            cout << v << " ";
        }
        cout << "\n";
        return 1;
    }
    int r = rand() % 2;
    if (r == 0) {
        if (!(ind[i] && outd[i])) {
            if (solve(i + 1)) return 1;
        }

        for (auto v : adj[i])
            --ind[v];
        --outd[par[i]];
        cnt++;
        deleted.push_back(i + 1);
        if (solve(i + 1)) return 1;
        deleted.pop_back();
        --cnt;
        for (auto v : adj[i])
            ++ind[v];
        ++outd[par[i]];
        return 0;
    }else{
        for (auto v : adj[i])
            --ind[v];
        --outd[par[i]];
        cnt++;
        deleted.push_back(i + 1);
        if (solve(i + 1)) return 1;
        deleted.pop_back();
        --cnt;
        for (auto v : adj[i])
            ++ind[v];
        ++outd[par[i]];

        if (!(ind[i] && outd[i])) {
            if (solve(i + 1)) return 1;
        }

        return 0;
    }
}

int a, b;

int main() {
    srand(time(NULL));
    ios_base::sync_with_stdio(0), cin.tie(0);
#ifdef CLION
    freopen("input.in", "rt", stdin);
#endif
    int tests;
    cin >> tests;
    while (tests--) {
        deleted.clear();
        cnt = 0;
        cin >> n >> m;
        for (int i = 0; i < n; ++i) {
            adj[i].clear();
            ind[i]=outd[i]=0;
        }
        for (int i = 0; i < m; ++i) {
            cin >> a >> b;
            --a, --b;
            ind[b]++;
            outd[a]++;
            par[b] = a;
            adj[a].push_back(b);
        }

        cando = 4 * n / 7;
        solve(0);
    }
}


/*input
2
4 6
1 2
1 3
2 3
2 4
3 4
3 4
7 6
1 2
1 3
2 4
2 5
3 6
3 7
*/
#include <bits/stdc++.h>

#define ll   int
#define vi vector< ll >
#define mset(A,v,n) memset((A),(v),(n)*sizeof((A)[0]) )
#define FORi(n) for(int i=0;i<(n);i++)
#define FORj(n) for(int j=0;j<(n);j++)
#define FORk(n) for(int k=0;k<(n);k++)
#define pb push_back
#define inp(n) scanf("%lld",&(n))
#define inp2(n,m) scanf("%lld%lld",&(n),&(m));
#define F first
#define S second
#define all(v) (v).begin(),(v).end()
#define mp(a,b) make_pair((a),(b))
#define pi pair< ll , ll >
#define MAXN  200001
#define mit map<int,int>::iterator
#define mod 1000000007
#define level  20
using namespace std;
void print(vi v){FORi(v.size())cout<<v[i]+1<<" ";cout<<endl;}
ll pm(ll x,ll y){ll res = 1;while(y>0){if(y&1) res = (res*x)%mod;x= (x*x)%mod;y/=2;}return res;}
int n,m;

vi C[MAXN];
vi C2[MAXN];
vi vis(MAXN,0);
vi order;

void dfs(int root){
    vis[root]=1;
    for(int i : C[root])
        if(vis[i]==0)
            dfs(i);

    order.pb(root);
}
void solve(){
    int a,b;
   cin>>n>>m;
   order.clear();

   FORi(n)C[i].clear(),vis[i]=0,C2[i].clear();

   FORi(m)
    {
        cin>>a>>b;
        a--;b--;
        C[b].pb(a);
        C2[a].pb(b);
    }

    FORi(n){
        if(vis[i]==0)
            dfs(i);
    }

    reverse(all(order));

    vi H(n,0);
    vi ans;

    int x1;
    for(int i : order){
        x1 = 0;
        for(int j : C2[i]){
            x1 |= H[j];
        }
        //cout<<i<<" "<<x1<<endl;

        if(x1==3 || x1==2){
            ans.pb(i);
        }

        else if(x1==0){
            H[i]=1;
        }
        else{
            H[i]=2;
        }

    }

    vi C[3];
    FORi(n){
        C[H[i]%3].pb(i);
    }
    int c1=0,c2=1,c3=2;

    int mx = (4*n)/7;

    while(ans.size() > mx);
    cout<<ans.size()<<endl;
    print(ans);

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t;
    cin>>t;
    while(t--)
    solve();

    return 0;

}


/*input
2
4 6
1 2
1 3
2 3
2 4
3 4
3 4
7 6
1 2
1 3
2 4
2 5
3 6
3 7
*/
#include <bits/stdc++.h>

#define ll   int
#define vi vector< ll >
#define mset(A,v,n) memset((A),(v),(n)*sizeof((A)[0]) )
#define FORi(n) for(int i=0;i<(n);i++)
#define FORj(n) for(int j=0;j<(n);j++)
#define FORk(n) for(int k=0;k<(n);k++)
#define pb push_back
#define inp(n) scanf("%lld",&(n))
#define inp2(n,m) scanf("%lld%lld",&(n),&(m));
#define F first
#define S second
#define all(v) (v).begin(),(v).end()
#define mp(a,b) make_pair((a),(b))
#define pi pair< ll , ll >
#define MAXN  200001
#define mit map<int,int>::iterator
#define mod 1000000007
#define level  20
using namespace std;
void print(vi v){FORi(v.size())cout<<v[i]+1<<" ";cout<<endl;}
ll pm(ll x,ll y){ll res = 1;while(y>0){if(y&1) res = (res*x)%mod;x= (x*x)%mod;y/=2;}return res;}
int n,m;

vi C[MAXN];
vi C2[MAXN];
vi vis(MAXN,0);
vi order;

void dfs(int root){
    vis[root]=1;
    for(int i : C[root])
        if(vis[i]==0)
            dfs(i);

    order.pb(root);
}
void solve(){
    int a,b;
   cin>>n>>m;
   order.clear();

   FORi(n)C[i].clear(),vis[i]=0,C2[i].clear();

   FORi(m)
    {
        cin>>a>>b;
        a--;b--;
        C[b].pb(a);
        C2[a].pb(b);
    }

    FORi(n){
        if(vis[i]==0)
            dfs(i);
    }

    reverse(all(order));

    vi H(n,0);
    vi ans;

    int x1;
    for(int i : order){
        x1 = 0;
        for(int j : C2[i]){
            x1 |= H[j];
        }
        //cout<<i<<" "<<x1<<endl;

        if(x1==3 || x1==2){
            ans.pb(i);
        }

        else if(x1==0){
            H[i]=1;
        }
        else{
            H[i]=2;
        }

    }

    vi C[3];
    FORi(n){
        C[H[i]%3].pb(i);
    }
    int c1=0,c2=1,c3=2;

    int mx = (4*n)/7;
    cout<<ans.size()<<endl;
    print(ans);

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t;
    cin>>t;
    while(t--)
    solve();

    return 0;

}


/*input
2
4 6
1 2
1 3
2 3
2 4
3 4
3 4
7 6
1 2
1 3
2 4
2 5
3 6
3 7
*/
#include <bits/stdc++.h>

#define ll   int
#define vi vector< ll >
#define mset(A,v,n) memset((A),(v),(n)*sizeof((A)[0]) )
#define FORi(n) for(int i=0;i<(n);i++)
#define FORj(n) for(int j=0;j<(n);j++)
#define FORk(n) for(int k=0;k<(n);k++)
#define pb push_back
#define inp(n) scanf("%lld",&(n))
#define inp2(n,m) scanf("%lld%lld",&(n),&(m));
#define F first
#define S second
#define all(v) (v).begin(),(v).end()
#define mp(a,b) make_pair((a),(b))
#define pi pair< ll , ll >
#define MAXN  200001
#define mit map<int,int>::iterator
#define mod 1000000007
#define level  20
using namespace std;
void print(vi v){FORi(v.size())cout<<v[i]+1<<" ";cout<<endl;}
ll pm(ll x,ll y){ll res = 1;while(y>0){if(y&1) res = (res*x)%mod;x= (x*x)%mod;y/=2;}return res;}
int n,m;

vi C[MAXN];
vi vis(MAXN,0);
vi order;

void dfs(int root){
    vis[root]=1;
    for(int i : C[root])
        if(vis[i]==0)
            dfs(i);

    order.pb(root);
}
void solve(){
    int a,b;
   cin>>n>>m;
   order.clear();

   FORi(n)C[i].clear(),vis[i]=0;

   FORi(m)
    {
        cin>>a>>b;
        a--;b--;
        C[b].pb(a);
    }

    FORi(n){
        if(vis[i]==0)
            dfs(i);
    }

    reverse(all(order));

    vi H(n,0);

    for(int i : order){
        for(int j : C[i])
            H[j]=max(H[j],H[i]+1);
    }

    vi C[3];
    FORi(n){
        C[H[i]%3].pb(i);
    }
    int c1=0,c2=1,c3=2;

    int mx = (4*n)/7;

    if(C[c1].size()<C[c2].size())swap(c1,c2);
    if(C[c1].size()<C[c3].size())swap(c1,c3);
    if(C[c2].size()<C[c3].size())swap(c2,c3);

    if(C[c1].size()+C[c2].size()<=mx){

        cout<<C[c1].size()+C[c2].size()<<"\n";
        FORi(C[c2].size())cout<<C[c2][i]+1<<" ";
        print(C[c1]);
        return ;
    }

    if(C[c1].size()+C[c3].size()<=mx){

        cout<<C[c1].size()+C[c3].size()<<"\n";
        FORi(C[c3].size())cout<<C[c3][i]+1<<" ";
        print(C[c1]);
        return ;
    }

    if(C[c2].size()+C[c3].size()<=mx){

        cout<<C[c3].size()+C[c2].size()<<"\n";
        FORi(C[c3].size())cout<<C[c3][i]+1<<" ";
        print(C[c2]);
        return ;
    }

    if(C[c1].size()<=mx){

        cout<<C[c1].size()<<"\n";

        print(C[c1]);
        return ;
    }

    if(C[c2].size()<=mx){

        cout<<C[c2].size()<<"\n";

        print(C[c2]);
        return ;
    }

    if(C[c3].size()<=mx){

        cout<<C[c3].size()<<"\n";

        print(C[c3]);
        return ;
    }

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t;
    cin>>t;
    while(t--)
    solve();

    return 0;

}


/*input
2
4 6
1 2
1 3
2 3
2 4
3 4
3 4
7 6
1 2
1 3
2 4
2 5
3 6
3 7
*/
#include <bits/stdc++.h>

#define ll   int
#define vi vector< ll >
#define mset(A,v,n) memset((A),(v),(n)*sizeof((A)[0]) )
#define FORi(n) for(int i=0;i<(n);i++)
#define FORj(n) for(int j=0;j<(n);j++)
#define FORk(n) for(int k=0;k<(n);k++)
#define pb push_back
#define inp(n) scanf("%lld",&(n))
#define inp2(n,m) scanf("%lld%lld",&(n),&(m));
#define F first
#define S second
#define all(v) (v).begin(),(v).end()
#define mp(a,b) make_pair((a),(b))
#define pi pair< ll , ll >
#define MAXN  200001
#define mit map<int,int>::iterator
#define mod 1000000007
#define level  20
using namespace std;
void print(vi v){FORi(v.size())cout<<v[i]+1<<" ";cout<<endl;}
ll pm(ll x,ll y){ll res = 1;while(y>0){if(y&1) res = (res*x)%mod;x= (x*x)%mod;y/=2;}return res;}
int n,m;

vi C[MAXN];
vi C2[MAXN];
vi vis(MAXN,0);
vi order;

void dfs(int root){
    vis[root]=1;
    for(int i : C[root])
        if(vis[i]==0)
            dfs(i);

    order.pb(root);
}
void solve(){
    int a,b;
   cin>>n>>m;
   order.clear();

   FORi(n)C[i].clear(),vis[i]=0,C2[i].clear();

   FORi(m)
    {
        cin>>a>>b;
        a--;b--;
        C2[b].pb(a);
        C[a].pb(b);
    }

    FORi(n){
        if(vis[i]==0)
            dfs(i);
    }

    reverse(all(order));

    vi H(n,0);
    vi ans;

    int x1;
    for(int i : order){
        x1 = 0;
        for(int j : C2[i]){
            x1 |= H[j];
        }
        //cout<<i<<" "<<x1<<endl;

        if(x1==3 || x1==2){
            ans.pb(i);
        }

        else if(x1==0){
            H[i]=1;
        }
        else{
            H[i]=2;
        }

    }

    vi C[3];
    FORi(n){
        C[H[i]%3].pb(i);
    }
    int c1=0,c2=1,c3=2;

    int mx = (4*n)/7;

    cout<<ans.size()<<endl;
    print(ans);

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t;
    cin>>t;
    while(t--)
    solve();

    return 0;

}


/*input
2
4 6
1 2
1 3
2 3
2 4
3 4
3 4
7 6
1 2
1 3
2 4
2 5
3 6
3 7
*/
#include <bits/stdc++.h>

#define ll   int
#define vi vector< ll >
#define mset(A,v,n) memset((A),(v),(n)*sizeof((A)[0]) )
#define FORi(n) for(int i=0;i<(n);i++)
#define FORj(n) for(int j=0;j<(n);j++)
#define FORk(n) for(int k=0;k<(n);k++)
#define pb push_back
#define inp(n) scanf("%lld",&(n))
#define inp2(n,m) scanf("%lld%lld",&(n),&(m));
#define F first
#define S second
#define all(v) (v).begin(),(v).end()
#define mp(a,b) make_pair((a),(b))
#define pi pair< ll , ll >
#define MAXN  200001
#define mit map<int,int>::iterator
#define mod 1000000007
#define level  20
using namespace std;
void print(vi v){FORi(v.size())cout<<v[i]+1<<" ";cout<<endl;}
ll pm(ll x,ll y){ll res = 1;while(y>0){if(y&1) res = (res*x)%mod;x= (x*x)%mod;y/=2;}return res;}
int n,m;

vi C[MAXN];
vi vis(MAXN,0);
vi order;

void dfs(int root){
    vis[root]=1;
    for(int i : C[root])
        if(vis[i]==0)
            dfs(i);

    order.pb(root);
}
void solve(){
    int a,b;
   cin>>n>>m;
   order.clear();

   FORi(n)C[i].clear(),vis[i]=0;

   FORi(m)
    {
        cin>>a>>b;
        a--;b--;
        C[b].pb(a);
    }

    FORi(n){
        if(vis[i]==0)
            dfs(i);
    }

    reverse(all(order));

    vi H(n,0);
    vi ans;
    vi tv(n,0);
    for(int i : order){
        if(H[i]==2)
        {
            H[i]=0;
            ans.pb(i);
        }
        else
        for(int j : C[i])
            {

                H[j]=H[i]+1;
            }

    }

    vi C[3];
    FORi(n){
        C[H[i]%3].pb(i);
    }
    int c1=0,c2=1,c3=2;

    int mx = (4*n)/7;
    cout<<ans.size()<<endl;
    print(ans);

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t;
    cin>>t;
    while(t--)
    solve();

    return 0;

}


/*input
2
4 6
1 2
1 3
2 3
2 4
3 4
3 4
7 6
1 2
1 3
2 4
2 5
3 6
3 7
*/
#include <bits/stdc++.h>

#define ll   int
#define vi vector< ll >
#define mset(A,v,n) memset((A),(v),(n)*sizeof((A)[0]) )
#define FORi(n) for(int i=0;i<(n);i++)
#define FORj(n) for(int j=0;j<(n);j++)
#define FORk(n) for(int k=0;k<(n);k++)
#define pb push_back
#define inp(n) scanf("%lld",&(n))
#define inp2(n,m) scanf("%lld%lld",&(n),&(m));
#define F first
#define S second
#define all(v) (v).begin(),(v).end()
#define mp(a,b) make_pair((a),(b))
#define pi pair< ll , ll >
#define MAXN  200001
#define mit map<int,int>::iterator
#define mod 1000000007
#define level  20
using namespace std;
void print(vi v){FORi(v.size())cout<<v[i]+1<<" ";cout<<endl;}
ll pm(ll x,ll y){ll res = 1;while(y>0){if(y&1) res = (res*x)%mod;x= (x*x)%mod;y/=2;}return res;}
int n,m;

vi C[MAXN];
vi vis(MAXN,0);
vi order;

void dfs(int root){
    vis[root]=1;
    for(int i : C[root])
        if(vis[i]==0)
            dfs(i);

    order.pb(root);
}
void solve(){
    int a,b;
   cin>>n>>m;
   order.clear();

   FORi(n)C[i].clear(),vis[i]=0;

   FORi(m)
    {
        cin>>a>>b;
        a--;b--;
        C[b].pb(a);
    }

    FORi(n){
        if(vis[i]==0)
            dfs(i);
    }

    reverse(all(order));

    vi H(n,0);

    for(int i : order){
        for(int j : C[i])
            if (H[j]==0)
                H[j]=H[i]+1;
    }

    vi C[3];
    FORi(n){
        C[H[i]%3].pb(i);
    }
    int c1=0,c2=1,c3=2;

    int mx = (4*n)/7;

    if(C[c1].size()<C[c2].size())swap(c1,c2);
    if(C[c1].size()<C[c3].size())swap(c1,c3);
    if(C[c2].size()<C[c3].size())swap(c2,c3);

    if(C[c1].size()+C[c2].size()<=mx){

        cout<<C[c1].size()+C[c2].size()<<"\n";
        FORi(C[c2].size())cout<<C[c2][i]+1<<" ";
        print(C[c1]);
        return ;
    }

    if(C[c1].size()+C[c3].size()<=mx){

        cout<<C[c1].size()+C[c3].size()<<"\n";
        FORi(C[c3].size())cout<<C[c3][i]+1<<" ";
        print(C[c1]);
        return ;
    }

    if(C[c2].size()+C[c3].size()<=mx){

        cout<<C[c3].size()+C[c2].size()<<"\n";
        FORi(C[c3].size())cout<<C[c3][i]+1<<" ";
        print(C[c2]);
        return ;
    }

    if(C[c1].size()<=mx){

        cout<<C[c1].size()<<"\n";

        print(C[c1]);
        return ;
    }

    if(C[c2].size()<=mx){

        cout<<C[c2].size()<<"\n";

        print(C[c2]);
        return ;
    }

    if(C[c3].size()<=mx){

        cout<<C[c3].size()<<"\n";

        print(C[c3]);
        return ;
    }

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t;
    cin>>t;
    while(t--)
    solve();

    return 0;

}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, n) for(int (i)=0; (i)<(n); (i)++)
#define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--)

int main(int argc, char** argv) {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15);
    if (argc == 2 && atoi(argv[1]) == 123456789) freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin);

   int T;
   cin >> T;
   FOR(t, T){
       int n, m;
       cin >> n >> m;

       vector<vector<int>> ins(n+1), outs(n+1);
       vector<pii> order;
       FOR(j, m){
           int u, v;
           cin >> u >> v;
           outs[u].push_back(v);
           ins[v].push_back(u);
       }

       vector<int> sol;
       vector<int> type(n+1);
       FOR1(u, n){
           int cnt[3] = {};
           for(auto v : ins[u]) cnt[type[v]]++;

           if (cnt[0] == 0 && cnt[1] == 0){
               type[u] = 0;
           }
           else if (cnt[0] > 0 && cnt[1] == 0 && cnt[2] == 0){
               type[u] = 1;
           }
           else{
               assert(cnt[1]);
               type[u] = 2;
               sol.push_back(u);
           }
       }

       cout << sol.size() << endl;
       for(auto it : sol) cout << it << " ";
       cout << endl;
   }

    if (argc == 2 && atoi(argv[1]) == 123456789) cout << clock()*1.0/CLOCKS_PER_SEC << " sec\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, n) for(int (i)=0; (i)<(n); (i)++)
#define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--)

int main(int argc, char** argv) {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15);
    if (argc == 2 && atoi(argv[1]) == 123456789) freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin);

   int T;
   cin >> T;
   FOR(t, T){
       int n, m;
       cin >> n >> m;

       vector<vector<int>> ins(n+1), outs(n+1);
       vector<pii> order;
       FOR(j, m){
           int u, v;
           cin >> u >> v;
           outs[u].push_back(v);
           ins[v].push_back(u);
       }

       vector<int> sol;
       vector<int> type(n+1);
       FOR1(u, n){
           int cnt[4] = {};
           for(auto v : ins[u]) cnt[type[v]]++;

           if (cnt[0] == 0 && cnt[1] == 0){
               type[u] = 0;
           }
           else if (cnt[1] == 0 && cnt[2] == 0){
               type[u] = 1;
           }
           else if (cnt[0] == 0 && cnt[2] == 0){
//               assert(cnt[1]);
               type[u] = 2;
               sol.push_back(u);
           }
           else{
               type[u] = 3;
           }
       }

       cout << sol.size() << endl;
       for(auto it : sol) cout << it << " ";
       cout << endl;
   }

    if (argc == 2 && atoi(argv[1]) == 123456789) cout << clock()*1.0/CLOCKS_PER_SEC << " sec\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, n) for(int (i)=0; (i)<(n); (i)++)
#define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--)

int main(int argc, char** argv) {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15);
    if (argc == 2 && atoi(argv[1]) == 123456789) freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin);

   int T;
   cin >> T;
   FOR(t, T){
       int n, m;
       cin >> n >> m;

       vector<set<int>> ins(n+1), outs(n+1);
       FOR(j, m){
           int u, v;
           cin >> u >> v;
           outs[u].insert(v);
           ins[v].insert(u);
       }

       vector<int> sol;
       FOR1(u, n){
           if (ins[u].size() && outs[u].size()){
               for(auto v : outs[u]){
                   ins[v].erase(u);
               }
               sol.push_back(u);
           }
       }
       cout << sol.size() << endl;
       for(auto it : sol) cout << it << " ";
       cout << endl;
   }

    if (argc == 2 && atoi(argv[1]) == 123456789) cout << clock()*1.0/CLOCKS_PER_SEC << " sec\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, n) for(int (i)=0; (i)<(n); (i)++)
#define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--)

int main(int argc, char** argv) {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15);
    if (argc == 2 && atoi(argv[1]) == 123456789) freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin);

   int T;
   cin >> T;
   FOR(t, T){
       int n, m;
       cin >> n >> m;

       vector<vector<int>> ins(n+1), outs(n+1);
       vector<pii> order;
       FOR(j, m){
           int u, v;
           cin >> u >> v;
           outs[u].push_back(v);
           ins[v].push_back(u);
       }

       vector<int> sol;
       vector<int> type(n+1);
       FOR1(u, n){
           for(auto v : ins[u]) {
               type[u] = max(type[u], type[v]+1);
           }
           if (type[u] == 2){
               type[u] = -1;
               sol.push_back(u);
           }
       }

       cout << sol.size() << endl;
       for(auto it : sol) cout << it << " ";
       cout << endl;
   }

    if (argc == 2 && atoi(argv[1]) == 123456789) cout << clock()*1.0/CLOCKS_PER_SEC << " sec\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, n) for(int (i)=0; (i)<(n); (i)++)
#define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--)

int main(int argc, char** argv) {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15);
    if (argc == 2 && atoi(argv[1]) == 123456789) freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin);

   int T;
   cin >> T;
   FOR(t, T){
       int n, m;
       cin >> n >> m;

       vector<set<int>> ins(n+1), outs(n+1);
       vector<pii> order;
       FOR(j, m){
           int u, v;
           cin >> u >> v;
           outs[u].insert(v);
           ins[v].insert(u);
       }
       FOR1(u, n){
           order.push_back({-outs[u].size() * ins[u].size(), u});
       }
       sort(order.begin(), order.end());

       vector<int> sol;
       FOR(uu, n){
           int u = order[uu].second;
           if (ins[u].size() && outs[u].size()){
               for(auto v : outs[u]){
                   ins[v].erase(u);
               }
               for(auto v : ins[u]){
                   outs[v].erase(u);
               }
               sol.push_back(u);
           }
       }
       cout << sol.size() << endl;
       for(auto it : sol) cout << it << " ";
       cout << endl;
   }

    if (argc == 2 && atoi(argv[1]) == 123456789) cout << clock()*1.0/CLOCKS_PER_SEC << " sec\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, n) for(int (i)=0; (i)<(n); (i)++)
#define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--)

int main(int argc, char** argv) {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15);
    if (argc == 2 && atoi(argv[1]) == 123456789) freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin);

   int T;
   cin >> T;
   FOR(t, T){
       int n, m;
       cin >> n >> m;

       vector<set<int>> ins0(n+1), outs0(n+1);
       vector<pii> order;
       FOR(j, m){
           int u, v;
           cin >> u >> v;
           outs0[u].insert(v);
           ins0[v].insert(u);
       }
       FOR1(u, n){
           order.push_back({0, u});
       }

       vector<int> sol;
       FOR(tt, 10) {
           sol.clear();
           random_shuffle(order.begin(), order.end());
           vector<set<int>> ins = ins0;
           vector<set<int>> outs = outs0;
           FOR(uu, n) {
               int u = order[uu].second;
               if (ins[u].size() && outs[u].size()) {
                   for (auto v : outs[u]) {
                       ins[v].erase(u);
                   }
                   for (auto v : ins[u]) {
                       outs[v].erase(u);
                   }
                   sol.push_back(u);
               }
           }
           if (sol.size() * 7 <= n*4){
               break;
           }
       }
       cout << sol.size() << endl;
       for(auto it : sol) cout << it << " ";
       cout << endl;
   }

    if (argc == 2 && atoi(argv[1]) == 123456789) cout << clock()*1.0/CLOCKS_PER_SEC << " sec\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, n) for(int (i)=0; (i)<(n); (i)++)
#define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--)

int main(int argc, char** argv) {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15);
    if (argc == 2 && atoi(argv[1]) == 123456789) freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin);

   int T;
   cin >> T;
   FOR(t, T){
       int n, m;
       cin >> n >> m;

       vector<vector<int>> ins(n+1), outs(n+1);
       vector<pii> order;
       FOR(j, m){
           int u, v;
           cin >> u >> v;
           outs[u].push_back(v);
           ins[v].push_back(u);
       }

       vector<int> sol;
       vector<int> type(n+1);
       FOR1(u, n){
           int cnt[4] = {};
           for(auto v : ins[u]) cnt[type[v]]++;

           if (cnt[0] == 0 && cnt[1] == 0){
               type[u] = 0;
           }
           else if (cnt[1] == 0 && cnt[2] == 0){
               type[u] = 1;
           }
           else if (cnt[1]){
//               assert(cnt[1]);
               type[u] = 2;
               sol.push_back(u);
           }
           else{
               type[u] = 3;
           }
       }

       cout << sol.size() << endl;
       for(auto it : sol) cout << it << " ";
       cout << endl;
   }

    if (argc == 2 && atoi(argv[1]) == 123456789) cout << clock()*1.0/CLOCKS_PER_SEC << " sec\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, n) for(int (i)=0; (i)<(n); (i)++)
#define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--)

int main(int argc, char** argv) {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15);
    if (argc == 2 && atoi(argv[1]) == 123456789) freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin);

   int T;
   cin >> T;
   FOR(t, T){
       int n, m;
       cin >> n >> m;

       vector<set<int>> ins0(n+1), outs0(n+1);
       vector<pii> order;
       FOR(j, m){
           int u, v;
           cin >> u >> v;
           outs0[u].insert(v);
           ins0[v].insert(u);
       }
       FOR1(u, n){
           order.push_back({0, u});
       }

       vector<int> sol;
       bool ok = 0;
       FOR(tt, 100) {
           sol.clear();
           random_shuffle(order.begin(), order.end());
           vector<set<int>> ins = ins0;
           vector<set<int>> outs = outs0;
           FOR(uu, n) {
               int u = order[uu].second;
               if (ins[u].size() && outs[u].size()) {
                   for (auto v : outs[u]) {
                       ins[v].erase(u);
                   }
                   for (auto v : ins[u]) {
                       outs[v].erase(u);
                   }
                   sol.push_back(u);
               }
           }
           if (sol.size() * 7 <= n*4){
               ok = 1;
               break;
           }
       }
       if (!ok)
           return -1;
       cout << sol.size() << endl;
       for(auto it : sol) cout << it << " ";
       cout << endl;
   }

    if (argc == 2 && atoi(argv[1]) == 123456789) cout << clock()*1.0/CLOCKS_PER_SEC << " sec\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, n) for(int (i)=0; (i)<(n); (i)++)
#define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--)

int main(int argc, char** argv) {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15);
    if (argc == 2 && atoi(argv[1]) == 123456789) freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin);

   int T;
   cin >> T;
   FOR(t, T){
       int n, m;
       cin >> n >> m;

       vector<vector<int>> ins(n+1), outs(n+1);
       vector<pii> order;
       FOR(j, m){
           int u, v;
           cin >> u >> v;
           outs[u].push_back(v);
           ins[v].push_back(u);
       }

       vector<int> sol;
       vector<int> type(n+1);
       FOR1(u, n){
           int cnt[3] = {};
           for(auto v : ins[u]) cnt[type[v]]++;

           if (cnt[0] == 0 && cnt[1] == 0){
               type[u] = 0;
           }
           else if (cnt[0] >= 0 && cnt[1] == 0 && cnt[2] == 0){
               type[u] = 1;
           }
           else{
               type[u] = 2;
               sol.push_back(u);
           }
       }

       cout << sol.size() << endl;
       for(auto it : sol) cout << it << " ";
       cout << endl;
   }

    if (argc == 2 && atoi(argv[1]) == 123456789) cout << clock()*1.0/CLOCKS_PER_SEC << " sec\n";
    return 0;
}


#include <bits/stdc++.h>
// #include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
// using namespace std::chrono;

#define ordered_set tree<ii, null_type,less<ii>, rb_tree_tag,tree_order_statistics_node_update>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ii pair<int, int>
#define LOG(x) cout << x <<" ";
#define TRACE(x) cout << #x << " : " << x << endl
#define LOG2DArray(array, n, m) for ( int i = 1; i <= n ; i++) { for (int j = 1; j <= m; j++) { LOG(array[i][j]); } cout<<endl; } cout<<endl;
#define LOG1DArray(array, n)  for( int i = 0; i < n ; i++){ cout<<i << " : ";LOG( array[i]); } cout<<endl;
#define myFor(i, l, r) for(int i = (int) l;i< (int) r;i++)
#define _ << " : " <<
#define L(a) (int)((a).size())
#define sqr(x) ((x) * 1ll * (x))
#define vi vector<int>
#define vl vector<ll>
#define pub push_back
#define pob pop_back
#define vii vector <ii>
#define vs vector <string>
#define all(s) (s).begin(),(s).end()
#define allb(s,b) (s).begin()+b,(s).end()
#define allr(s) (s).rbegin(),(s).rend()
#define fore(i, l, r) for(auto i = l; i <= r; i++)
#define forn(i, l, r) for(auto i = l; i >= r; i--)
#define REP(i, n) for (int i = 1; i <= n; i++)
#define cinx int x; cin>>x
#define cinxl ll x; cin>>x
#define endl "\n"
#define sout cout<<endl
#define ret(x) return cout<<x<<endl , 0;

const int inf = 1e9;
const int maxn = 2e5  + 5;
const ld eps = 1e-10;
const int mod = 1e9 + 7;

int n , m;
set<int> adj[maxn];
set<int> radj[maxn];
bool visited[maxn];
vi sorted;
set<int> ans;
int cnt[maxn];
void dfs(int u){
	if (visited[u])
		return;
	visited[u] = true;
	for(auto v : radj[u]){
		dfs(v);
	}
	sorted.pub(u);
}

int main(){
	fastio;
	int testcase;
	cin>>testcase;
	while(testcase--){
		sorted.clear();
		cin>>n>>m;
		ans.clear();
		fore(i , 1, n){
			adj[i].clear();
			radj[i].clear();
			visited[i] = false;
			cnt[i] = 0;
		}
		fore(i ,1 , m){
			int u , v;
			cin>>u>>v;
			adj[u].insert(v);
			radj[v].insert(u);
		}
		fore(i ,1 , n){
			dfs(i);
		}
		// reverse(all(sorted));
		for(int u : sorted){
			if (cnt[u] == 2)
				ans.insert(u);
			for(int v : adj[u])
				cnt[v] = max(cnt[u] + 1 , cnt[v]);

		}
		cout<<ans.size()<<endl;
		for(auto u : ans){
			cout<<u<<" ";
		}
		sout;
	}
}


#include <bits/stdc++.h>
// #include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
// using namespace std::chrono;

#define ordered_set tree<ii, null_type,less<ii>, rb_tree_tag,tree_order_statistics_node_update>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ii pair<int, int>
#define LOG(x) cout << x <<" ";
#define TRACE(x) cout << #x << " : " << x << endl
#define LOG2DArray(array, n, m) for ( int i = 1; i <= n ; i++) { for (int j = 1; j <= m; j++) { LOG(array[i][j]); } cout<<endl; } cout<<endl;
#define LOG1DArray(array, n)  for( int i = 0; i < n ; i++){ cout<<i << " : ";LOG( array[i]); } cout<<endl;
#define myFor(i, l, r) for(int i = (int) l;i< (int) r;i++)
#define _ << " : " <<
#define L(a) (int)((a).size())
#define sqr(x) ((x) * 1ll * (x))
#define vi vector<int>
#define vl vector<ll>
#define pub push_back
#define pob pop_back
#define vii vector <ii>
#define vs vector <string>
#define all(s) (s).begin(),(s).end()
#define allb(s,b) (s).begin()+b,(s).end()
#define allr(s) (s).rbegin(),(s).rend()
#define fore(i, l, r) for(auto i = l; i <= r; i++)
#define forn(i, l, r) for(auto i = l; i >= r; i--)
#define REP(i, n) for (int i = 1; i <= n; i++)
#define cinx int x; cin>>x
#define cinxl ll x; cin>>x
#define endl "\n"
#define sout cout<<endl
#define ret(x) return cout<<x<<endl , 0;

const int inf = 1e9;
const int maxn = 2e5  + 5;
const ld eps = 1e-10;
const int mod = 1e9 + 7;

int n , m;
set<int> adj[maxn];
set<int> radj[maxn];
bool visited[maxn];
vi sorted;
set<int> ans;
void dfs(int u){
	if (visited[u])
		return;
	visited[u] = true;
	for(auto v : radj[u]){
		dfs(v);
	}
	sorted.pub(u);
}

int main(){
	fastio;
	int testcase;
	cin>>testcase;
	while(testcase--){
		cin>>n>>m;
		ans.clear();
		fore(i , 1, n){
			adj[i].clear();
			radj[i].clear();
			visited[i] = false;
		}
		fore(i ,1 , m){
			int u , v;
			cin>>u>>v;
			adj[u].insert(v);
			radj[v].insert(u);
		}
		fore(i ,1 , n){
			dfs(i);
		}
		for(int u : sorted){
			if (ans.count(u))
				continue;
			for(int v : adj[u])
				ans.insert(v);
		}
		cout<<ans.size()<<endl;
		for(auto u : ans){
			cout<<u<<" ";
		}
		sout;
	}
}


#include <bits/stdc++.h>
// #include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
// using namespace std::chrono;

#define ordered_set tree<ii, null_type,less<ii>, rb_tree_tag,tree_order_statistics_node_update>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ii pair<int, int>
#define LOG(x) cout << x <<" ";
#define TRACE(x) cout << #x << " : " << x << endl
#define LOG2DArray(array, n, m) for ( int i = 1; i <= n ; i++) { for (int j = 1; j <= m; j++) { LOG(array[i][j]); } cout<<endl; } cout<<endl;
#define LOG1DArray(array, n)  for( int i = 0; i < n ; i++){ cout<<i << " : ";LOG( array[i]); } cout<<endl;
#define myFor(i, l, r) for(int i = (int) l;i< (int) r;i++)
#define _ << " : " <<
#define L(a) (int)((a).size())
#define sqr(x) ((x) * 1ll * (x))
#define vi vector<int>
#define vl vector<ll>
#define pub push_back
#define pob pop_back
#define vii vector <ii>
#define vs vector <string>
#define all(s) (s).begin(),(s).end()
#define allb(s,b) (s).begin()+b,(s).end()
#define allr(s) (s).rbegin(),(s).rend()
#define fore(i, l, r) for(auto i = l; i <= r; i++)
#define forn(i, l, r) for(auto i = l; i >= r; i--)
#define REP(i, n) for (int i = 1; i <= n; i++)
#define cinx int x; cin>>x
#define cinxl ll x; cin>>x
// #define endl "\n"
#define sout cout<<endl
#define ret(x) return cout<<x<<endl , 0;

const int inf = 1e9;
const int maxn = 2e5  + 5;
const ld eps = 1e-10;
const int mod = 1e9 + 7;

int n , m;
set<int> adj[maxn];
set<int> radj[maxn];
bool visited[maxn];
vi sorted;
set<int> ans;
void dfs(int u , int p){
	if (visited[u])
		return;
	visited[u] = true;
	for(auto v : radj[u]){
		dfs(v);
	}
	sorted.pub(u);
}

int main(){
	int testcase;
	cin>>testcase;
	while(testcase--){
		cin>>n>>m;
		ans.clear();
		fore(i , 1, n){
			adj[i].clear();
			radj[i].clear();
			visited[i] = false;
		}
		fore(i ,1 , m){
			int u , v;
			cin>>u>>v;
			adj[u].insert(v);
			radj[v].insert(u);
		}
		fore(i ,1 , n){
			dfs(i);
		}
		for(int u : sorted){
			if (ans.count(u))
				continue;
			for(int v : adj[u])
				ans.insert(v);
		}
		cout<<ans.size()<<endl;
		for(auto u : ans){
			cout<<u<<" ";
		}
		sout;
	}
}