#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();
		sorted.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 : radj[u])
				if (radj[v].size())
					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){
	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();
		sorted.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 : radj[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){
	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();
		sorted.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;
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);
				continue;
			}
			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(){
	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 <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <utility>
#include <queue>
#include <set>

using namespace std;
typedef long long ll;
const int LEN = 100000;

struct fastio {
  int it, len;
  char s[LEN + 5];
  fastio() {
    it = len = 0;
  }
  char get() {
    if(it < len) return s[it++];
    it = 0, len = fread(s, 1, LEN, stdin);
    return len ? s[it++] : EOF;
  }
  bool notend() {
    char c;
    for (c = get();c == ' ' || c == '\n' || c == '\r';c = get());
    if (it) it--;
    return c != EOF;
  }
  void put(char c) {
    if(it == LEN) fwrite(s,1,LEN,stdout), it = 0;
    s[it++] = c;
  }
  void flush() {
    fwrite(s, 1, it, stdout);
  }
}buff, bufo;
inline int getint() {
  char c; int res = 0, sig = 1;
  for (c = buff.get(); c < '0' || c > '9'; c = buff.get()) if(c == '-') sig = -1;
  for (;c >= '0' && c <= '9';c = buff.get()) res = res * 10 + (c - '0');
  return sig * res;
}
inline ll getll() {
  char c; ll res = 0, sig = 1;
  for (c = buff.get(); c < '0' || c > '9'; c = buff.get()) if (c == '-') sig = -1;
  for (; c >= '0' && c <= '9'; c = buff.get()) res = res * 10 + (c - '0');
  return sig * res;
}
inline void putint(int x, char suf) {
  if (!x) bufo.put('0');
  else {
    if (x < 0) bufo.put('-'), x = -x;
    int k = 0; char s[15];
    while (x) {
      s[++k] = x % 10 + '0';
      x /= 10;
    }
    for (; k; k--) bufo.put(s[k]);
  }
  bufo.put(suf);
}
inline void putll(ll x, char suf) {
  if (!x) bufo.put('0');
  else {
    if (x < 0) bufo.put('-'), x = -x;
    int k = 0; char s[25];
    while (x) {
      s[++k] = x % 10 + '0';
      x /= 10;
    }
    for (; k; k--) bufo.put(s[k]);
  }
  bufo.put(suf);
}
inline char get_char() {
  char c;
  for (c = buff.get(); c == ' ' || c == '\n' || c == '\r'; c = buff.get());
  return c;
}
#define maxn 200005
int T, n, m;
vector<int> adj[maxn], ans;
set<int> S;
void solve() {
  n = getint(), m = getint();
  S.clear();
  ans.clear();
  for (int i = 1; i <= n; i++) {
    adj[i].clear();
    S.insert(i);
  }
  for (int i = 1; i <= m; i++) {
    int u = getint(), v = getint();
    adj[u].push_back(v);
  }
  while (!S.empty()) {
    int u = *S.begin();
    for (auto v : adj[u]) {
      if (!S.count(v)) continue;
      for (auto w : adj[v]) {
        ans.push_back(w);
        S.erase(w);
      }
      S.erase(v);
    }
    S.erase(u);
  }
  sort(ans.begin(), ans.end());
  ans.erase(unique(ans.begin(), ans.end()), ans.end());
  putint(ans.size(), '\n');
  for (auto u : ans) putint(u, ' ');
  bufo.put('\n');
}
int main() {
  T = getint();
  while (T--) solve();
  bufo.flush();
  return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <utility>
#include <queue>
#include <set>

using namespace std;
typedef long long ll;
const int LEN = 100000;

struct fastio {
  int it, len;
  char s[LEN + 5];
  fastio() {
    it = len = 0;
  }
  char get() {
    if(it < len) return s[it++];
    it = 0, len = fread(s, 1, LEN, stdin);
    return len ? s[it++] : EOF;
  }
  bool notend() {
    char c;
    for (c = get();c == ' ' || c == '\n' || c == '\r';c = get());
    if (it) it--;
    return c != EOF;
  }
  void put(char c) {
    if(it == LEN) fwrite(s,1,LEN,stdout), it = 0;
    s[it++] = c;
  }
  void flush() {
    fwrite(s, 1, it, stdout);
  }
}buff, bufo;
inline int getint() {
  char c; int res = 0, sig = 1;
  for (c = buff.get(); c < '0' || c > '9'; c = buff.get()) if(c == '-') sig = -1;
  for (;c >= '0' && c <= '9';c = buff.get()) res = res * 10 + (c - '0');
  return sig * res;
}
inline ll getll() {
  char c; ll res = 0, sig = 1;
  for (c = buff.get(); c < '0' || c > '9'; c = buff.get()) if (c == '-') sig = -1;
  for (; c >= '0' && c <= '9'; c = buff.get()) res = res * 10 + (c - '0');
  return sig * res;
}
inline void putint(int x, char suf) {
  if (!x) bufo.put('0');
  else {
    if (x < 0) bufo.put('-'), x = -x;
    int k = 0; char s[15];
    while (x) {
      s[++k] = x % 10 + '0';
      x /= 10;
    }
    for (; k; k--) bufo.put(s[k]);
  }
  bufo.put(suf);
}
inline void putll(ll x, char suf) {
  if (!x) bufo.put('0');
  else {
    if (x < 0) bufo.put('-'), x = -x;
    int k = 0; char s[25];
    while (x) {
      s[++k] = x % 10 + '0';
      x /= 10;
    }
    for (; k; k--) bufo.put(s[k]);
  }
  bufo.put(suf);
}
inline char get_char() {
  char c;
  for (c = buff.get(); c == ' ' || c == '\n' || c == '\r'; c = buff.get());
  return c;
}
#define maxn 200005
int T, n, m;
vector<int> adj[maxn];
set<int> S;
int del[maxn];
void solve() {
  n = getint(), m = getint();
  S.clear();
  for (int i = 1; i <= n; i++) {
    del[i] = 0;
    adj[i].clear();
    S.insert(i);
  }
  for (int i = 1; i <= m; i++) {
    int u = getint(), v = getint();
    adj[u].push_back(v);
  }
  while (!S.empty()) {
    int u = *S.begin();
    for (auto v : adj[u]) {
      if (!S.count(v)) continue;
      for (auto w : adj[v]) {
        if (!S.count(w) && !del[w]) {
          for (auto x : adj[w]) {
            del[x]--;
            if (!del[x]) S.insert(x);
          }
        }
        del[w]++;
        S.erase(w);
      }
      S.erase(v);
    }
    S.erase(u);
  }
  int k = 0;
  for (int i = 1; i <= n; i++) k += (del[i] > 0);
  putint(k, '\n');
  for (int i = 1; i <= n; i++) if (del[i]) putint(i, ' ');
  bufo.put('\n');
}
int main() {
  T = getint();
  while (T--) solve();
  bufo.flush();
  return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <utility>
#include <queue>
#include <set>

using namespace std;
typedef long long ll;
const int LEN = 100000;

struct fastio {
  int it, len;
  char s[LEN + 5];
  fastio() {
    it = len = 0;
  }
  char get() {
    if(it < len) return s[it++];
    it = 0, len = fread(s, 1, LEN, stdin);
    return len ? s[it++] : EOF;
  }
  bool notend() {
    char c;
    for (c = get();c == ' ' || c == '\n' || c == '\r';c = get());
    if (it) it--;
    return c != EOF;
  }
  void put(char c) {
    if(it == LEN) fwrite(s,1,LEN,stdout), it = 0;
    s[it++] = c;
  }
  void flush() {
    fwrite(s, 1, it, stdout);
  }
}buff, bufo;
inline int getint() {
  char c; int res = 0, sig = 1;
  for (c = buff.get(); c < '0' || c > '9'; c = buff.get()) if(c == '-') sig = -1;
  for (;c >= '0' && c <= '9';c = buff.get()) res = res * 10 + (c - '0');
  return sig * res;
}
inline ll getll() {
  char c; ll res = 0, sig = 1;
  for (c = buff.get(); c < '0' || c > '9'; c = buff.get()) if (c == '-') sig = -1;
  for (; c >= '0' && c <= '9'; c = buff.get()) res = res * 10 + (c - '0');
  return sig * res;
}
inline void putint(int x, char suf) {
  if (!x) bufo.put('0');
  else {
    if (x < 0) bufo.put('-'), x = -x;
    int k = 0; char s[15];
    while (x) {
      s[++k] = x % 10 + '0';
      x /= 10;
    }
    for (; k; k--) bufo.put(s[k]);
  }
  bufo.put(suf);
}
inline void putll(ll x, char suf) {
  if (!x) bufo.put('0');
  else {
    if (x < 0) bufo.put('-'), x = -x;
    int k = 0; char s[25];
    while (x) {
      s[++k] = x % 10 + '0';
      x /= 10;
    }
    for (; k; k--) bufo.put(s[k]);
  }
  bufo.put(suf);
}
inline char get_char() {
  char c;
  for (c = buff.get(); c == ' ' || c == '\n' || c == '\r'; c = buff.get());
  return c;
}
#define maxn 200005
int T, n, m;
vector<int> adj[maxn];
set<int> S;
int del[maxn];
void solve() {
  n = getint(), m = getint();
  S.clear();
  memset(del, 0, sizeof(del));
  for (int i = 1; i <= n; i++) {
    adj[i].clear();
    S.insert(i);
  }
  for (int i = 1; i <= m; i++) {
    int u = getint(), v = getint();
    adj[u].push_back(v);
  }
  while (!S.empty()) {
    int u = *S.begin();
    for (auto v : adj[u]) {
      if (!S.count(v)) continue;
      for (auto w : adj[v]) {
        if (!S.count(w) && !del[w]) {
          for (auto x : adj[w]) {
            del[x]--;
            if (!del[x]) S.insert(x);
          }
        }
        del[w]++;
        S.erase(w);
      }
      S.erase(v);
    }
    S.erase(u);
  }
  int k = 0;
  for (int i = 1; i <= n; i++) k += (del[i] > 0);
  putint(k, '\n');
  for (int i = 1; i <= n; i++) if (del[i]) putint(i, ' ');
  bufo.put('\n');
}
int main() {
  T = getint();
  while (T--) solve();
  bufo.flush();
  return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <utility>
#include <queue>
#include <set>

using namespace std;
typedef long long ll;
const int LEN = 100000;

struct fastio {
  int it, len;
  char s[LEN + 5];
  fastio() {
    it = len = 0;
  }
  char get() {
    if(it < len) return s[it++];
    it = 0, len = fread(s, 1, LEN, stdin);
    return len ? s[it++] : EOF;
  }
  bool notend() {
    char c;
    for (c = get();c == ' ' || c == '\n' || c == '\r';c = get());
    if (it) it--;
    return c != EOF;
  }
  void put(char c) {
    if(it == LEN) fwrite(s,1,LEN,stdout), it = 0;
    s[it++] = c;
  }
  void flush() {
    fwrite(s, 1, it, stdout);
  }
}buff, bufo;
inline int getint() {
  char c; int res = 0, sig = 1;
  for (c = buff.get(); c < '0' || c > '9'; c = buff.get()) if(c == '-') sig = -1;
  for (;c >= '0' && c <= '9';c = buff.get()) res = res * 10 + (c - '0');
  return sig * res;
}
inline ll getll() {
  char c; ll res = 0, sig = 1;
  for (c = buff.get(); c < '0' || c > '9'; c = buff.get()) if (c == '-') sig = -1;
  for (; c >= '0' && c <= '9'; c = buff.get()) res = res * 10 + (c - '0');
  return sig * res;
}
inline void putint(int x, char suf) {
  if (!x) bufo.put('0');
  else {
    if (x < 0) bufo.put('-'), x = -x;
    int k = 0; char s[15];
    while (x) {
      s[++k] = x % 10 + '0';
      x /= 10;
    }
    for (; k; k--) bufo.put(s[k]);
  }
  bufo.put(suf);
}
inline void putll(ll x, char suf) {
  if (!x) bufo.put('0');
  else {
    if (x < 0) bufo.put('-'), x = -x;
    int k = 0; char s[25];
    while (x) {
      s[++k] = x % 10 + '0';
      x /= 10;
    }
    for (; k; k--) bufo.put(s[k]);
  }
  bufo.put(suf);
}
inline char get_char() {
  char c;
  for (c = buff.get(); c == ' ' || c == '\n' || c == '\r'; c = buff.get());
  return c;
}
#define maxn 200005
int T, n, m;
vector<int> adj[maxn];
set<int> S, ans;
int deg[maxn];
void solve() {
  n = getint(), m = getint();
  S.clear(); ans.clear();
  for (int i = 1; i <= n; i++) {
    deg[i] = 0;
    adj[i].clear();
    S.insert(i);
  }
  for (int i = 1; i <= m; i++) {
    int u = getint(), v = getint();
    adj[u].push_back(v);
    deg[v]++;
  }
  while (!S.empty()) {
    int u = *S.begin();
    if (deg[u]) {
      for (auto v : adj[u]) {
        if (ans.count(v)) continue;
        ans.insert(v);
        S.erase(v);
        for (auto w : adj[v]) deg[w]--;
      }
    }
    S.erase(u);
  }
  putint(ans.size(), '\n');
  for (auto u : ans) putint(u, ' ');
  bufo.put('\n');
}
int main() {
  T = getint();
  while (T--) solve();
  bufo.flush();
  return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <utility>
#include <queue>
#include <set>

using namespace std;
typedef long long ll;
const int LEN = 100000;

struct fastio {
  int it, len;
  char s[LEN + 5];
  fastio() {
    it = len = 0;
  }
  char get() {
    if(it < len) return s[it++];
    it = 0, len = fread(s, 1, LEN, stdin);
    return len ? s[it++] : EOF;
  }
  bool notend() {
    char c;
    for (c = get();c == ' ' || c == '\n' || c == '\r';c = get());
    if (it) it--;
    return c != EOF;
  }
  void put(char c) {
    if(it == LEN) fwrite(s,1,LEN,stdout), it = 0;
    s[it++] = c;
  }
  void flush() {
    fwrite(s, 1, it, stdout);
  }
}buff, bufo;
inline int getint() {
  char c; int res = 0, sig = 1;
  for (c = buff.get(); c < '0' || c > '9'; c = buff.get()) if(c == '-') sig = -1;
  for (;c >= '0' && c <= '9';c = buff.get()) res = res * 10 + (c - '0');
  return sig * res;
}
inline ll getll() {
  char c; ll res = 0, sig = 1;
  for (c = buff.get(); c < '0' || c > '9'; c = buff.get()) if (c == '-') sig = -1;
  for (; c >= '0' && c <= '9'; c = buff.get()) res = res * 10 + (c - '0');
  return sig * res;
}
inline void putint(int x, char suf) {
  if (!x) bufo.put('0');
  else {
    if (x < 0) bufo.put('-'), x = -x;
    int k = 0; char s[15];
    while (x) {
      s[++k] = x % 10 + '0';
      x /= 10;
    }
    for (; k; k--) bufo.put(s[k]);
  }
  bufo.put(suf);
}
inline void putll(ll x, char suf) {
  if (!x) bufo.put('0');
  else {
    if (x < 0) bufo.put('-'), x = -x;
    int k = 0; char s[25];
    while (x) {
      s[++k] = x % 10 + '0';
      x /= 10;
    }
    for (; k; k--) bufo.put(s[k]);
  }
  bufo.put(suf);
}
inline char get_char() {
  char c;
  for (c = buff.get(); c == ' ' || c == '\n' || c == '\r'; c = buff.get());
  return c;
}
#define maxn 200005
int T, n, m;
vector<int> adj[maxn];
set<int> S, ans;
void solve() {
  n = getint(), m = getint();
  S.clear();
  ans.clear();
  for (int i = 1; i <= n; i++) {
    adj[i].clear();
    S.insert(i);
  }
  for (int i = 1; i <= m; i++) {
    int u = getint(), v = getint();
    adj[u].push_back(v);
  }
  while (!S.empty()) {
    int u = *S.begin();
    for (auto v : adj[u]) {
      if (!S.count(v)) continue;
      for (auto w : adj[v]) {
        if (!S.count(w) && !ans.count(w)) {
          for (auto x : adj[w]) {
            ans.erase(x);
            S.insert(x);
          }
        }
        ans.insert(w);
        S.erase(w);
      }
      S.erase(v);
    }
    S.erase(u);
  }
  putint(ans.size(), '\n');
  for (auto u : ans) putint(u, ' ');
  bufo.put('\n');
}
int main() {
  T = getint();
  while (T--) solve();
  bufo.flush();
  return 0;
}


#include<bits/stdc++.h>
#define ll                 long long int
#define mod                9000000000000000000
#define pi                 3.14159265358979323846264338327950288419716939937510
#define setbit(x)          __builtin_popcountll(x)
#define db                 long double
#define pp(x)              pair<x,x>
#define ff                 first
#define ss                 second
#define FIO                ios_base::sync_with_stdio(false); cin.tie(NULL);
#define tc                 ll tc; cin>>tc; for(ll number = 1 ; number <= tc ; number ++ )
#define vv(x)              vector<x>
#define pb                 push_back
#define pob                pop_back
#define pf                 push_front
#define pof                pop_front
#define endl               "\n"
#define pq                 priority_queue
#define dbg(x)             { cout<< #x << ": " << (x) << endl; }
#define dbg2(x,y)          { cout<< #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
#define bp                 ll kkk; cin >>kkk;
#define dbgArr(a,n)        cout<<"Array "<<#a<<endl; for(ll i=0 ; i<n ; i++) cout<<a[i]<<" "; cout<<endl;
#define maxE(a,n)          *max_element(a,a+n);
#define minE(a,n)          *min_element(a,a+n);
#define forn(i,s,n)        for(ll i=s ; i<n ; i++)
#define fornr(i,e,s)       for(ll i=e-1 ; i>=s ; i--)
#define arr(a,n)           for(ll i=0;i<n;i++){ cin>>a[i]; }
#define arr1(a,n)          for(ll i=1;i<=n;i++){ cin>>a[i]; }
#define file freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define print cout<<"Case #"<<number<<": "
#define all(x) x.begin(), x.end()
ll power(ll x, ll b) {ll p = 1;while (b > 0) {if(b&1) {p = p * x; p %= mod;} b >>= 1; x *= x; x %= mod;} return p % mod;}
using namespace std;
# define INF 1e15
struct lex_compare {
    bool operator() (const pair<db,ll>  p1, const pair<db,ll> p2){
        return (p1.first ==  p2.first) ? p1.ss < p2.ss : p1.first > p2.first;
    }
};

void fillOrder(ll i,vv(vv(ll)) &graph,bool visited[], stack<ll> &st){
    visited[i] = true;
    for(auto j:graph[i]){
        if(!visited[j]){
            fillOrder(j,graph,visited,st);
        }
    }
    st.push(i);
    return;
}

stack<ll> getts(vv(vv(ll)) &graph){
    stack<ll> st;
    bool visited[graph.size()+1] = {0};

    //Filling the stack acc. to their finishing time
    for(ll i =1;i<graph.size();i++){
        if(!visited[i]){
            fillOrder(i,graph,visited,st);
        }
    }
    return st;
}
void dfs2(ll src, vv(vv(ll)) &edges, ll states[],set<ll> &ans){
    ans.insert(src);
    if(states[src] == 1){
        for(auto i : edges[src]){
            states[i] = 0;
            for(auto j : edges[i]){
                 dfs2(j,edges,states, ans);
            }
        }
    }else{
        for(auto i : edges[src]){
            states[i] = 0;
            for(auto j : edges[i]){
                states[j] = 0;
                for(auto k : edges[j]){
                    dfs2(k,edges,states,ans);
                }
            }
        }
    }
    states[src] = 0;
    return;
}

ll dfs(ll src, vv(vv(ll)) &edges,ll dp[], ll states[]){
    if(dp[src] != -1) return dp[src];
    ll counts1 = 0,counts2 = 0;
    for(auto i : edges[src]){
        for(auto j : edges[i]){
            counts1 += dfs(j,edges,dp, states);
            for(auto k : edges[j]){
                counts2 += dfs(k,edges,dp,states);
            }
        }
    }
    if(counts1 == 0 && counts2 == 0){
        dp[src] = 1;
        states[src] = 1;
        return 1;
    }
    if(counts1 > counts2){
        dp[src] = counts1;
        states[src] = 1;
    }else{
        dp[src] = counts2;
        states[src] = 2;
    }
    return dp[src];
}

int main(){
    FIO;
    ll t; cin >> t;
    while(t--){
        ll n,m; cin >> n >> m;
        ll dp[n+1] = {0} ;
        ll states[n+1] = {0};
        memset(dp,-1,sizeof(dp));
        vv(vv(ll)) edges(n+1);
        forn(i,0,m){
            ll a,b; cin >> a >> b;
            edges[a].pb(b);
        }
        stack<ll> st = getts(edges);
        set<ll> ans;
        queue<ll> later;
        while(!st.empty()){
            ll x = st.top();
            st.pop();
            later.push(x);
            if(dp[x] != -1) continue;
            dfs(x,edges,dp,states);
        }
        while(!later.empty()){
            ll x = later.front();later.pop();
            if(states[x] != 0){
                dfs2(x,edges, states, ans);
            }

        }
        cout << ans.size() << endl;
        for(auto i : ans){
            cout << i << " ";
        }
        cout << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll                 long long int
#define mod                9000000000000000000
#define pi                 3.14159265358979323846264338327950288419716939937510
#define setbit(x)          __builtin_popcountll(x)
#define db                 long double
#define pp(x)              pair<x,x>
#define ff                 first
#define ss                 second
#define FIO                ios_base::sync_with_stdio(false); cin.tie(NULL);
#define tc                 ll tc; cin>>tc; for(ll number = 1 ; number <= tc ; number ++ )
#define vv(x)              vector<x>
#define pb                 push_back
#define pob                pop_back
#define pf                 push_front
#define pof                pop_front
#define endl               "\n"
#define pq                 priority_queue
#define dbg(x)             { cout<< #x << ": " << (x) << endl; }
#define dbg2(x,y)          { cout<< #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
#define bp                 ll kkk; cin >>kkk;
#define dbgArr(a,n)        cout<<"Array "<<#a<<endl; for(ll i=0 ; i<n ; i++) cout<<a[i]<<" "; cout<<endl;
#define maxE(a,n)          *max_element(a,a+n);
#define minE(a,n)          *min_element(a,a+n);
#define forn(i,s,n)        for(ll i=s ; i<n ; i++)
#define fornr(i,e,s)       for(ll i=e-1 ; i>=s ; i--)
#define arr(a,n)           for(ll i=0;i<n;i++){ cin>>a[i]; }
#define arr1(a,n)          for(ll i=1;i<=n;i++){ cin>>a[i]; }
#define file freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define print cout<<"Case #"<<number<<": "
#define all(x) x.begin(), x.end()
ll power(ll x, ll b) {ll p = 1;while (b > 0) {if(b&1) {p = p * x; p %= mod;} b >>= 1; x *= x; x %= mod;} return p % mod;}
using namespace std;
# define INF 1e15
struct lex_compare {
    bool operator() (const pair<db,ll>  p1, const pair<db,ll> p2){
        return (p1.first ==  p2.first) ? p1.ss < p2.ss : p1.first > p2.first;
    }
};

void fillOrder(ll i,vv(vv(ll)) &graph,bool visited[], stack<ll> &st){
    visited[i] = true;
    for(auto j:graph[i]){
        if(!visited[j]){
            fillOrder(j,graph,visited,st);
        }
    }
    st.push(i);
    return;
}

stack<ll> getts(vv(vv(ll)) &graph){
    stack<ll> st;
    bool visited[graph.size()+1] = {0};

    //Filling the stack acc. to their finishing time
    for(ll i =1;i<graph.size();i++){
        if(!visited[i]){
            fillOrder(i,graph,visited,st);
        }
    }
    return st;
}
void dfs2(ll src, vv(vv(ll)) &edges, ll states[],set<ll> &ans){
    ans.insert(src);
    if(states[src] == 1){
        for(auto i : edges[src]){
            states[i] = 0;
            for(auto j : edges[i]){
                 dfs2(j,edges,states, ans);
            }
        }
    }else{
        for(auto i : edges[src]){
            states[i] = 0;
            for(auto j : edges[i]){
                states[j] = 0;
                for(auto k : edges[j]){
                    dfs2(k,edges,states,ans);
                }
            }
        }
    }
    states[src] = 0;
    return;
}

ll dfs(ll src, vv(vv(ll)) &edges,ll dp[], ll states[]){
    if(dp[src] != -1) return dp[src];
    ll counts1 = 0,counts2 = 0;
    for(auto i : edges[src]){
        for(auto j : edges[i]){
            counts1 += dfs(j,edges,dp, states);
            for(auto k : edges[j]){
                counts2 += dfs(k,edges,dp,states);
            }
        }
    }
    if(counts1 == 0 && counts2 == 0){
        dp[src] = 1;
        states[src] = 1;
        return 1;
    }
    if(counts1 > counts2){
        dp[src] = counts1;
        states[src] = 1;
    }else{
        dp[src] = counts2;
        states[src] = 2;
    }
    return dp[src];
}

int main(){
    FIO;
    ll t; cin >> t;
    while(t--){
        ll n,m; cin >> n >> m;
        vv(vv(ll)) edges(n+1);
        forn(i,0,m){
            ll a,b; cin >> a >> b;
            edges[a].pb(b);
        }
        vv(ll) ans;
        bool visone[n+1] = {0};
        bool vis2[n+1] = {0};
        forn(i,1,n+1){
            if(vis2[i]){
                ans.pb(i); continue;
            }else{
                for(auto j : edges[i]){
                    if(visone[i]) vis2[j] = 2;
                    else visone[j] = 1;
                }
            }
        }
        cout << ans.size() << endl;
        for(auto i : ans){
            cout << i << " ";
        }
        cout << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll                 long long int
#define mod                9000000000000000000
#define pi                 3.14159265358979323846264338327950288419716939937510
#define setbit(x)          __builtin_popcountll(x)
#define db                 long double
#define pp(x)              pair<x,x>
#define ff                 first
#define ss                 second
#define FIO                ios_base::sync_with_stdio(false); cin.tie(NULL);
#define tc                 ll tc; cin>>tc; for(ll number = 1 ; number <= tc ; number ++ )
#define vv(x)              vector<x>
#define pb                 push_back
#define pob                pop_back
#define pf                 push_front
#define pof                pop_front
#define endl               "\n"
#define pq                 priority_queue
#define dbg(x)             { cout<< #x << ": " << (x) << endl; }
#define dbg2(x,y)          { cout<< #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
#define bp                 ll kkk; cin >>kkk;
#define dbgArr(a,n)        cout<<"Array "<<#a<<endl; for(ll i=0 ; i<n ; i++) cout<<a[i]<<" "; cout<<endl;
#define maxE(a,n)          *max_element(a,a+n);
#define minE(a,n)          *min_element(a,a+n);
#define forn(i,s,n)        for(ll i=s ; i<n ; i++)
#define fornr(i,e,s)       for(ll i=e-1 ; i>=s ; i--)
#define arr(a,n)           for(ll i=0;i<n;i++){ cin>>a[i]; }
#define arr1(a,n)          for(ll i=1;i<=n;i++){ cin>>a[i]; }
#define file freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define print cout<<"Case #"<<number<<": "
#define all(x) x.begin(), x.end()
ll power(ll x, ll b) {ll p = 1;while (b > 0) {if(b&1) {p = p * x; p %= mod;} b >>= 1; x *= x; x %= mod;} return p % mod;}
using namespace std;
# define INF 1e15
struct lex_compare {
    bool operator() (const pair<db,ll>  p1, const pair<db,ll> p2){
        return (p1.first ==  p2.first) ? p1.ss < p2.ss : p1.first > p2.first;
    }
};

void fillOrder(ll i,vv(vv(ll)) &graph,bool visited[], stack<ll> &st){
    visited[i] = true;
    for(auto j:graph[i]){
        if(!visited[j]){
            fillOrder(j,graph,visited,st);
        }
    }
    st.push(i);
    return;
}

stack<ll> getts(vv(vv(ll)) &graph){
    stack<ll> st;
    bool visited[graph.size()+1] = {0};

    //Filling the stack acc. to their finishing time
    for(ll i =1;i<graph.size();i++){
        if(!visited[i]){
            fillOrder(i,graph,visited,st);
        }
    }
    return st;
}
void dfs2(ll src, vv(vv(ll)) &edges, ll states[],set<ll> &ans){
    ans.insert(src);
    if(states[src] == 1){
        for(auto i : edges[src]){
            states[i] = 0;
            for(auto j : edges[i]){
                 dfs2(j,edges,states, ans);
            }
        }
    }else{
        for(auto i : edges[src]){
            states[i] = 0;
            for(auto j : edges[i]){
                states[j] = 0;
                for(auto k : edges[j]){
                    dfs2(k,edges,states,ans);
                }
            }
        }
    }
    states[src] = 0;
    return;
}

ll dfs(ll src, vv(vv(ll)) &edges,ll dp[], ll states[]){
    if(dp[src] != -1) return dp[src];
    ll counts1 = 0,counts2 = 0;
    for(auto i : edges[src]){
        for(auto j : edges[i]){
            counts1 += dfs(j,edges,dp, states);
            for(auto k : edges[j]){
                counts2 += dfs(k,edges,dp,states);
            }
        }
    }
    if(counts1 == 0 && counts2 == 0){
        dp[src] = 1;
        states[src] = 1;
        return 1;
    }
    if(counts1 <= counts2){
        dp[src] = counts1;
        states[src] = 1;
    }else{
        dp[src] = counts2;
        states[src] = 2;
    }
    return dp[src];
}

int main(){
    FIO;
    ll t; cin >> t;
    while(t--){
        ll n,m; cin >> n >> m;
        ll dp[n+1] = {0} ;
        ll states[n+1] = {0};
        memset(dp,-1,sizeof(dp));
        vv(vv(ll)) edges(n+1);
        forn(i,0,m){
            ll a,b; cin >> a >> b;
            edges[a].pb(b);
        }
        stack<ll> st = getts(edges);
        set<ll> ans;
        queue<ll> later;
        while(!st.empty()){
            ll x = st.top();
            st.pop();
            later.push(x);
            if(dp[x] != -1) continue;
            dfs(x,edges,dp,states);
        }
        while(!later.empty()){
            ll x = later.front();later.pop();
            if(states[x] != 0){
                dfs2(x,edges, states, ans);
            }

        }
        cout << ans.size() << endl;
        for(auto i : ans){
            cout << i << " ";
        }
        cout << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll                 long long int
#define mod                9000000000000000000
#define pi                 3.14159265358979323846264338327950288419716939937510
#define setbit(x)          __builtin_popcountll(x)
#define db                 long double
#define pp(x)              pair<x,x>
#define ff                 first
#define ss                 second
#define FIO                ios_base::sync_with_stdio(false); cin.tie(NULL);
#define tc                 ll tc; cin>>tc; for(ll number = 1 ; number <= tc ; number ++ )
#define vv(x)              vector<x>
#define pb                 push_back
#define pob                pop_back
#define pf                 push_front
#define pof                pop_front
#define endl               "\n"
#define pq                 priority_queue
#define dbg(x)             { cout<< #x << ": " << (x) << endl; }
#define dbg2(x,y)          { cout<< #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
#define bp                 ll kkk; cin >>kkk;
#define dbgArr(a,n)        cout<<"Array "<<#a<<endl; for(ll i=0 ; i<n ; i++) cout<<a[i]<<" "; cout<<endl;
#define maxE(a,n)          *max_element(a,a+n);
#define minE(a,n)          *min_element(a,a+n);
#define forn(i,s,n)        for(ll i=s ; i<n ; i++)
#define fornr(i,e,s)       for(ll i=e-1 ; i>=s ; i--)
#define arr(a,n)           for(ll i=0;i<n;i++){ cin>>a[i]; }
#define arr1(a,n)          for(ll i=1;i<=n;i++){ cin>>a[i]; }
#define file freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define print cout<<"Case #"<<number<<": "
#define all(x) x.begin(), x.end()
ll power(ll x, ll b) {ll p = 1;while (b > 0) {if(b&1) {p = p * x; p %= mod;} b >>= 1; x *= x; x %= mod;} return p % mod;}
using namespace std;
# define INF 1e15
struct lex_compare {
    bool operator() (const pair<db,ll>  p1, const pair<db,ll> p2){
        return (p1.first ==  p2.first) ? p1.ss < p2.ss : p1.first > p2.first;
    }
};

void fillOrder(ll i,vv(vv(ll)) &graph,bool visited[], stack<ll> &st){
    visited[i] = true;
    for(auto j:graph[i]){
        if(!visited[j]){
            fillOrder(j,graph,visited,st);
        }
    }
    st.push(i);
    return;
}

stack<ll> getts(vv(vv(ll)) &graph){
    stack<ll> st;
    bool visited[graph.size()+1] = {0};

    //Filling the stack acc. to their finishing time
    for(ll i =1;i<graph.size();i++){
        if(!visited[i]){
            fillOrder(i,graph,visited,st);
        }
    }
    return st;
}

ll dfs(ll src, vv(vv(ll)) &edges, bool vis[], bool removed[], vv(ll) &ans){
    vis[src] = 0;
    ll tracks = 0;
    for(auto i : edges[src]){
        if(!removed[i]){
            tracks += dfs(i,edges,vis,removed, ans);
        }
    }
    //dbg2(src,tracks);
    if(tracks <= 1) return 1;
    if(tracks > 1) {
        //dbg(src);
        removed[src] = 1;
        ans.pb(src);
        return 0;
    }

}

int main(){
    FIO;
    ll t; cin >> t;
    while(t--){
        ll n,m; cin >> n >> m;
        vv(vv(ll)) edges(n+1);
        forn(i,0,m){
            ll a,b; cin >> a >> b;
            edges[a].pb(b);
        }
        stack<ll> st = getts(edges);
        bool vis[n+1] = {0};
        bool removed[n+1] = {0};
        vv(ll) ans;
        while(!st.empty()){
            ll x = st.top();
            st.pop();
            if(removed[x]) continue;
            dfs(x,edges,vis,removed,ans);
        }
        cout << ans.size() << endl;
        for(auto i : ans){
            cout << i << " ";
        }
        cout << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll                 long long int
#define mod                9000000000000000000
#define pi                 3.14159265358979323846264338327950288419716939937510
#define setbit(x)          __builtin_popcountll(x)
#define db                 long double
#define pp(x)              pair<x,x>
#define ff                 first
#define ss                 second
#define FIO                ios_base::sync_with_stdio(false); cin.tie(NULL);
#define tc                 ll tc; cin>>tc; for(ll number = 1 ; number <= tc ; number ++ )
#define vv(x)              vector<x>
#define pb                 push_back
#define pob                pop_back
#define pf                 push_front
#define pof                pop_front
#define endl               "\n"
#define pq                 priority_queue
#define dbg(x)             { cout<< #x << ": " << (x) << endl; }
#define dbg2(x,y)          { cout<< #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
#define bp                 ll kkk; cin >>kkk;
#define dbgArr(a,n)        cout<<"Array "<<#a<<endl; for(ll i=0 ; i<n ; i++) cout<<a[i]<<" "; cout<<endl;
#define maxE(a,n)          *max_element(a,a+n);
#define minE(a,n)          *min_element(a,a+n);
#define forn(i,s,n)        for(ll i=s ; i<n ; i++)
#define fornr(i,e,s)       for(ll i=e-1 ; i>=s ; i--)
#define arr(a,n)           for(ll i=0;i<n;i++){ cin>>a[i]; }
#define arr1(a,n)          for(ll i=1;i<=n;i++){ cin>>a[i]; }
#define file freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define print cout<<"Case #"<<number<<": "
#define all(x) x.begin(), x.end()
ll power(ll x, ll b) {ll p = 1;while (b > 0) {if(b&1) {p = p * x; p %= mod;} b >>= 1; x *= x; x %= mod;} return p % mod;}
using namespace std;
# define INF 1e15
struct lex_compare {
    bool operator() (const pair<db,ll>  p1, const pair<db,ll> p2){
        return (p1.first ==  p2.first) ? p1.ss < p2.ss : p1.first > p2.first;
    }
};

void fillOrder(ll i,vv(vv(ll)) &graph,bool visited[], stack<ll> &st){
    visited[i] = true;
    for(auto j:graph[i]){
        if(!visited[j]){
            fillOrder(j,graph,visited,st);
        }
    }
    st.push(i);
    return;
}

stack<ll> getts(vv(vv(ll)) &graph){
    stack<ll> st;
    bool visited[graph.size()+1] = {0};

    //Filling the stack acc. to their finishing time
    for(ll i =1;i<graph.size();i++){
        if(!visited[i]){
            fillOrder(i,graph,visited,st);
        }
    }
    return st;
}
void dfs2(ll src, vv(vv(ll)) &edges, ll states[],set<ll> &ans){
    ans.insert(src);
    if(states[src] == 1){
        for(auto i : edges[src]){
            states[i] = 0;
            for(auto j : edges[i]){
                 dfs2(j,edges,states, ans);
            }
        }
    }else{
        for(auto i : edges[src]){
            states[i] = 0;
            for(auto j : edges[i]){
                states[j] = 0;
                for(auto k : edges[j]){
                    dfs2(k,edges,states,ans);
                }
            }
        }
    }
    states[src] = 0;
    return;
}

ll dfs(ll src, vv(vv(ll)) &edges,ll dp[], ll states[]){
    if(dp[src] != -1) return dp[src];
    ll counts1 = 0,counts2 = 0;
    for(auto i : edges[src]){
        for(auto j : edges[i]){
            counts1 += dfs(j,edges,dp, states);
            for(auto k : edges[j]){
                counts2 += dfs(k,edges,dp,states);
            }
        }
    }
    if(counts1 == 0 && counts2 == 0){
        dp[src] = 1;
        states[src] = 1;
        return 1;
    }
    if(counts1 > counts2){
        dp[src] = counts1;
        states[src] = 1;
    }else{
        dp[src] = counts2;
        states[src] = 2;
    }
    return dp[src];
}

int main(){
    FIO;
    ll t; cin >> t;
    while(t--){
        ll n,m; cin >> n >> m;
        ll dp[n+1] = {0} ;
        ll states[n+1] = {0};
        vv(vv(ll)) edges(n+1);
        forn(i,0,m){
            ll a,b; cin >> a >> b;
            edges[a].pb(b);
        }
        vv(ll) ans;
        forn(i,1,n+1){
            if(states[i] >= 2){
                ans.pb(i); continue;
            }else{
                for(auto j : edges[i]){
                    if(states[i]) states[j] = 2;
                    else states[j] = 1;
                }
            }
        }
        cout << ans.size() << endl;
        for(auto i : ans){
            cout << i << " ";
        }
        cout << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll                 long long int
#define mod                9000000000000000000
#define pi                 3.14159265358979323846264338327950288419716939937510
#define setbit(x)          __builtin_popcountll(x)
#define db                 long double
#define pp(x)              pair<x,x>
#define ff                 first
#define ss                 second
#define FIO                ios_base::sync_with_stdio(false); cin.tie(NULL);
#define tc                 ll tc; cin>>tc; for(ll number = 1 ; number <= tc ; number ++ )
#define vv(x)              vector<x>
#define pb                 push_back
#define pob                pop_back
#define pf                 push_front
#define pof                pop_front
#define endl               "\n"
#define pq                 priority_queue
#define dbg(x)             { cout<< #x << ": " << (x) << endl; }
#define dbg2(x,y)          { cout<< #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
#define bp                 ll kkk; cin >>kkk;
#define dbgArr(a,n)        cout<<"Array "<<#a<<endl; for(ll i=0 ; i<n ; i++) cout<<a[i]<<" "; cout<<endl;
#define maxE(a,n)          *max_element(a,a+n);
#define minE(a,n)          *min_element(a,a+n);
#define forn(i,s,n)        for(ll i=s ; i<n ; i++)
#define fornr(i,e,s)       for(ll i=e-1 ; i>=s ; i--)
#define arr(a,n)           for(ll i=0;i<n;i++){ cin>>a[i]; }
#define arr1(a,n)          for(ll i=1;i<=n;i++){ cin>>a[i]; }
#define file freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define print cout<<"Case #"<<number<<": "
#define all(x) x.begin(), x.end()
ll power(ll x, ll b) {ll p = 1;while (b > 0) {if(b&1) {p = p * x; p %= mod;} b >>= 1; x *= x; x %= mod;} return p % mod;}
using namespace std;
# define INF 1e15
struct lex_compare {
    bool operator() (const pair<db,ll>  p1, const pair<db,ll> p2){
        return (p1.first ==  p2.first) ? p1.ss < p2.ss : p1.first > p2.first;
    }
};

void fillOrder(ll i,vv(vv(ll)) &graph,bool visited[], stack<ll> &st){
    visited[i] = true;
    for(auto j:graph[i]){
        if(!visited[j]){
            fillOrder(j,graph,visited,st);
        }
    }
    st.push(i);
    return;
}

stack<ll> getts(vv(vv(ll)) &graph){
    stack<ll> st;
    bool visited[graph.size()+1] = {0};

    //Filling the stack acc. to their finishing time
    for(ll i =1;i<graph.size();i++){
        if(!visited[i]){
            fillOrder(i,graph,visited,st);
        }
    }
    return st;
}
void dfs2(ll src, vv(vv(ll)) &edges, ll states[],set<ll> &ans){
    ans.insert(src);
    if(states[src] == 1){
        for(auto i : edges[src]){
            states[i] = 0;
            for(auto j : edges[i]){
                 dfs2(j,edges,states, ans);
            }
        }
    }else{
        for(auto i : edges[src]){
            states[i] = 0;
            for(auto j : edges[i]){
                states[j] = 0;
                for(auto k : edges[j]){
                    dfs2(k,edges,states,ans);
                }
            }
        }
    }
    states[src] = 0;
    return;
}

ll dfs(ll src, vv(vv(ll)) &edges,ll dp[], ll states[]){
    if(dp[src] != -1) return dp[src];
    ll counts1 = 0,counts2 = 0;
    for(auto i : edges[src]){
        for(auto j : edges[i]){
            counts1 += dfs(j,edges,dp, states);
            for(auto k : edges[j]){
                counts2 += dfs(k,edges,dp,states);
            }
        }
    }
    if(counts1 == 0 && counts2 == 0){
        dp[src] = 1;
        states[src] = 1;
        return 1;
    }
    if(counts1 > counts2){
        dp[src] = counts1;
        states[src] = 1;
    }else{
        dp[src] = counts2;
        states[src] = 2;
    }
    return dp[src];
}

int main(){
    FIO;
    ll t; cin >> t;
    while(t--){
        ll n,m; cin >> n >> m;
        ll dp[n+1] = {0} ;
        ll states[n+1] = {0};
        vv(vv(ll)) edges(n+1);
        forn(i,0,m){
            ll a,b; cin >> a >> b;
            edges[a].pb(b);
        }
        vv(ll) ans;
        forn(i,1,n+1){
            if(states[i] >= 2){
                ans.pb(i); continue;
            }else{
                for(auto j : edges[i]){
                    if(states[i] == 1) states[j] = 2;
                    else states[j] = 1;
                }
            }
        }
        cout << ans.size() << endl;
        for(auto i : ans){
            cout << i << " ";
        }
        cout << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define FAST ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int M=2e5+10;
int t,n,m,din[M],dout[M];
set<int>g[M],rg[M];
vector<int>ans;
set<pair<pair<int,int>,int> >s;
bool del[M];
void clearing()
{
  for(int i=1;i<=n;i++)del[i]=din[i]=dout[i]=0,g[i].clear(),rg[i].clear();
  ans.clear();
  s.clear();
}
int32_t main()
{
  FAST;
  cin>>t;
  while(t--)
    {
      cin>>n>>m;
      for(int i=1;i<=m;i++)
	{
	  int u,v;
	  cin>>u>>v;
	  if(g[u].find(v)==g[u].end())
	    din[v]++,dout[u]++;
	  g[u].insert(v);
	  rg[v].insert(u);
	}
      for(int i=1;i<=n;i++)
	s.insert({{min(din[i],dout[i]),din[i]},i});
      for(int i=1;i<=4*n/7;i++)
	{
	  auto p=*s.rbegin();
	  s.erase(p);
	  //cout<<p.second<<endl;
	  ans.push_back(p.second);
	  del[p.second]=true;
	  for(auto u:g[p.second])
	    if(!del[u])
	      {
		s.erase({{min(din[u],dout[u]),din[u]},u});
		din[u]--;
		s.insert({{min(din[u],dout[u]),din[u]},u});
	      }
	  for(auto u:rg[p.second])
	    if(!del[u])
	      {
		s.erase({{min(din[u],dout[u]),din[u]},u});
		dout[u]--;
		s.insert({{min(din[u],dout[u]),din[u]},u});
	      }
	}
      cout<<ans.size()<<endl;
      for(auto x:ans)
	cout<<x<<" ";
      cout<<endl;
      clearing();
    }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
const int M=2e5+10;
int a[M],n,m;
vector<int>g[M],ans;
void clearing()
{
  for(int i=1;i<=n;i++)g[i].clear(),a[i]=0;
  ans.clear();
}
int32_t main()
{
    int t;
    cin>>t;
    while(t--)
      {
	cin>>n>>m;
        for(int i=1;i<=m;i++)
	  {
	    int u,v;
	    cin>>u>>v;
	    g[u].push_back(v);
	  }
        for(int i=1;i<=n;i++)
	  {
            if(a[i]==2)
	      ans.push_back(i);
            else
	      for(auto u:g[i])
		a[u]=max(a[u],a[i]+1);
	  }
        cout<<ans.size()<<endl;
        for(auto x:ans)
	  cout<<x<<" ";
	cout<<endl;
	clearing();
      }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define FAST ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int M=2e5+10;
int t,n,m,din[M],dout[M];
vector<int>ans,g[M],rg[M];
set<pair<int,int> >s;
bool del[M];
void clearing()
{
  for(int i=1;i<=n;i++)del[i]=din[i]=dout[i]=0,g[i].clear(),rg[i].clear();
  ans.clear();
  s.clear();
}
int32_t main()
{
  FAST;
  cin>>t;
  while(t--)
    {
      cin>>n>>m;
      for(int i=1;i<=m;i++)
	{
	  int u,v;
	  cin>>u>>v;
	  g[u].push_back(v);
	  rg[v].push_back(u);
	  din[v]++;
	  dout[u]++;
	}
      for(int i=1;i<=n;i++)
	s.insert({min(din[i],dout[i]),i});
      for(int i=1;i<=4*n/7;i++)
	{
	  auto p=*s.rbegin();
	  s.erase(p);
	  //cout<<p.second<<endl;
	  ans.push_back(p.second);
	  del[p.second]=true;
	  for(auto u:g[p.second])
	    if(!del[u])
	      {
		s.erase({min(din[u],dout[u]),u});
		din[u]--;
		s.insert({min(din[u],dout[u]),u});
	      }
	  for(auto u:rg[p.second])
	    if(!del[u])
	      {
		s.erase({min(din[u],dout[u]),u});
		dout[u]--;
		s.insert({min(din[u],dout[u]),u});
	      }
	}
      cout<<ans.size()<<endl;
      for(auto x:ans)
	cout<<x<<" ";
      cout<<endl;
      clearing();
    }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define FAST ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int M=2e5+10;
int t,n,m,din[M],dout[M];
set<int>g[M],rg[M];
vector<int>ans;
set<pair<pair<int,int>,int> >s;
bool del[M];
void clearing()
{
  for(int i=1;i<=n;i++)del[i]=din[i]=dout[i]=0,g[i].clear(),rg[i].clear();
  ans.clear();
  s.clear();
}
int32_t main()
{
  FAST;
  cin>>t;
  while(t--)
    {
      cin>>n>>m;
      for(int i=1;i<=m;i++)
	{
	  int u,v;
	  cin>>u>>v;
	  if(g[u].find(v)==g[u].end())
	    din[v]++,dout[u]++;
	  g[u].insert(v);
	  rg[v].insert(u);
	}
      for(int i=1;i<=n;i++)
	s.insert({{min(din[i],dout[i]),din[i]+dout[i]},i});
      for(int i=1;i<=4*n/7;i++)
	{
	  auto p=*s.rbegin();
	  s.erase(p);
	  //cout<<p.second<<endl;
	  ans.push_back(p.second);
	  del[p.second]=true;
	  for(auto u:g[p.second])
	    if(!del[u])
	      {
		s.erase({{min(din[u],dout[u]),din[u]+dout[u]},u});
		din[u]--;
		s.insert({{min(din[u],dout[u]),din[u]+dout[u]},u});
	      }
	  for(auto u:rg[p.second])
	    if(!del[u])
	      {
		s.erase({{min(din[u],dout[u]),din[u]+dout[u]},u});
		dout[u]--;
		s.insert({{min(din[u],dout[u]),din[u]+dout[u]},u});
	      }
	}
      cout<<ans.size()<<endl;
      for(auto x:ans)
	cout<<x<<" ";
      cout<<endl;
      clearing();
    }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int ans,len1,len2,n,o,e;
vector<int>v;
string s,t;
int main()
{
  cin>>n>>s>>t;
  for(int i=0;i<n;i++)
    if(s[i]=='1' && t[i]=='0')
      v.push_back(1),o++;
    else if(s[i]=='0' && t[i]=='1')
      v.push_back(2),e++;
  if(o!=e)return cout<<-1,0;
  n=v.size();
  for(auto x:v)
    {
      if(x==1)
	len1++,len2--;
      else
	len1--,len2++;
      len1=max(len1,0);
      len2=max(len2,0);
      ans=max(ans,max(len1,len2));
    }
  cout<<ans<<endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define FAST ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int M=2e5+10;
int t,n,m,din[M],dout[M];
set<int>g[M],rg[M];
vector<int>ans;
set<pair<pair<int,int>,int> >s;
bool del[M];
void clearing()
{
  for(int i=1;i<=n;i++)del[i]=din[i]=dout[i]=0,g[i].clear(),rg[i].clear();
  ans.clear();
  s.clear();
}
int32_t main()
{
  FAST;
  cin>>t;
  while(t--)
    {
      cin>>n>>m;
      for(int i=1;i<=m;i++)
	{
	  int u,v;
	  cin>>u>>v;
	  g[u].insert(v);
	  rg[v].insert(u);
	  din[v]++;
	  dout[u]++;
	}
      for(int i=1;i<=n;i++)
	s.insert({{min(din[i],dout[i]),din[i]+dout[i]},i});
      for(int i=1;i<=4*n/7;i++)
	{
	  auto p=*s.rbegin();
	  s.erase(p);
	  //cout<<p.second<<endl;
	  ans.push_back(p.second);
	  del[p.second]=true;
	  for(auto u:g[p.second])
	    if(!del[u])
	      {
		s.erase({{min(din[u],dout[u]),din[u]+dout[u]},u});
		din[u]--;
		s.insert({{min(din[u],dout[u]),din[u]+dout[u]},u});
	      }
	  for(auto u:rg[p.second])
	    if(!del[u])
	      {
		s.erase({{min(din[u],dout[u]),din[u]+dout[u]},u});
		dout[u]--;
		s.insert({{min(din[u],dout[u]),din[u]+dout[u]},u});
	      }
	}
      cout<<ans.size()<<endl;
      for(auto x:ans)
	cout<<x<<" ";
      cout<<endl;
      clearing();
    }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define FAST ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int M=2e5+10;
int t,n,m,din[M],dout[M];
set<int>g[M],rg[M];
vector<int>ans;
set<pair<int,int> >s;
bool del[M];
void clearing()
{
  for(int i=1;i<=n;i++)del[i]=din[i]=dout[i]=0,g[i].clear(),rg[i].clear();
  ans.clear();
  s.clear();
}
int32_t main()
{
  FAST;
  cin>>t;
  while(t--)
    {
      cin>>n>>m;
      for(int i=1;i<=m;i++)
	{
	  int u,v;
	  cin>>u>>v;
	  g[u].insert(v);
	  rg[v].insert(u);
	  din[v]++;
	  dout[u]++;
	}
      for(int i=1;i<=n;i++)
	s.insert({min(din[i],dout[i]),i});
      for(int i=1;i<=4*n/7;i++)
	{
	  auto p=*s.rbegin();
	  s.erase(p);
	  //cout<<p.second<<endl;
	  ans.push_back(p.second);
	  del[p.second]=true;
	  for(auto u:g[p.second])
	    if(!del[u])
	      {
		s.erase({min(din[u],dout[u]),u});
		din[u]--;
		s.insert({min(din[u],dout[u]),u});
	      }
	  for(auto u:rg[p.second])
	    if(!del[u])
	      {
		s.erase({min(din[u],dout[u]),u});
		dout[u]--;
		s.insert({min(din[u],dout[u]),u});
	      }
	}
      cout<<ans.size()<<endl;
      for(auto x:ans)
	cout<<x<<" ";
      cout<<endl;
      clearing();
    }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define FAST ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int M=2e5+10;
int t,n,m,din[M],dout[M];
set<int>g[M],rg[M];
vector<int>ans;
set<pair<pair<int,pair<int,int> >,int > >s;
bool del[M];
void clearing()
{
  for(int i=1;i<=n;i++)del[i]=din[i]=dout[i]=0,g[i].clear(),rg[i].clear();
  ans.clear();
  s.clear();
}
int32_t main()
{
  FAST;
  cin>>t;
  while(t--)
    {
      cin>>n>>m;
      for(int i=1;i<=m;i++)
	{
	  int u,v;
	  cin>>u>>v;
	  if(g[u].find(v)==g[u].end())
	    din[v]++,dout[u]++;
	  g[u].insert(v);
	  rg[v].insert(u);
	}
      for(int i=1;i<=n;i++)
	s.insert({{min(din[i],dout[i]),{dout[i],din[i]}},i});
      for(int i=1;i<=4*n/7;i++)
	{
	  auto p=*s.rbegin();
	  s.erase(p);
	  //cout<<p.second<<endl;
	  ans.push_back(p.second);
	  del[p.second]=true;
	  for(auto u:g[p.second])
	    if(!del[u])
	      {
		s.erase({{min(din[u],dout[u]),{dout[i],din[i]}},u});
		din[u]--;
		s.insert({{min(din[u],dout[u]),{dout[i],din[i]}},u});
	      }
	  for(auto u:rg[p.second])
	    if(!del[u])
	      {
		s.erase({{min(din[u],dout[u]),{dout[i],din[i]}},u});
		dout[u]--;
		s.insert({{min(din[u],dout[u]),{dout[i],din[i]}},u});
	      }
	}
      cout<<ans.size()<<endl;
      for(auto x:ans)
	cout<<x<<" ";
      cout<<endl;
      clearing();
    }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define FAST ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int M=2e5+10;
int t,n,m,din[M],dout[M];
set<int>g[M],rg[M];
vector<int>ans;
set<pair<pair<int,int>,int> >s;
bool del[M];
void clearing()
{
  for(int i=1;i<=n;i++)del[i]=din[i]=dout[i]=0,g[i].clear(),rg[i].clear();
  ans.clear();
  s.clear();
}
int32_t main()
{
  FAST;
  cin>>t;
  while(t--)
    {
      cin>>n>>m;
      for(int i=1;i<=m;i++)
	{
	  int u,v;
	  cin>>u>>v;
	  g[u].insert(v);
	  rg[v].insert(u);
	  din[v]++;
	  dout[u]++;
	}
      for(int i=1;i<=n;i++)
	s.insert({{min(din[i],dout[i]),din[i]},i});
      for(int i=1;i<=4*n/7;i++)
	{
	  auto p=*s.rbegin();
	  s.erase(p);
	  //cout<<p.second<<endl;
	  ans.push_back(p.second);
	  del[p.second]=true;
	  for(auto u:g[p.second])
	    if(!del[u])
	      {
		s.erase({{min(din[u],dout[u]),din[u]},u});
		din[u]--;
		s.insert({{min(din[u],dout[u]),din[u]},u});
	      }
	  for(auto u:rg[p.second])
	    if(!del[u])
	      {
		s.erase({{min(din[u],dout[u]),din[u]},u});
		dout[u]--;
		s.insert({{min(din[u],dout[u]),din[u]},u});
	      }
	}
      cout<<ans.size()<<endl;
      for(auto x:ans)
	cout<<x<<" ";
      cout<<endl;
      clearing();
    }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define FAST ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int M=2e5+10;
int t,n,m,din[M],dout[M];
set<int>g[M],rg[M];
vector<int>ans;
set<pair<pair<int,int>,int> >s;
bool del[M];
void clearing()
{
  for(int i=1;i<=n;i++)del[i]=din[i]=dout[i]=0,g[i].clear(),rg[i].clear();
  ans.clear();
  s.clear();
}
int32_t main()
{
  FAST;
  cin>>t;
  while(t--)
    {
      cin>>n>>m;
      for(int i=1;i<=m;i++)
	{
	  int u,v;
	  cin>>u>>v;
	  if(g[u].find(v)==g[u].end())
	    din[v]++,dout[u]++;
	  g[u].insert(v);
	  rg[v].insert(u);
	}
      for(int i=1;i<=n;i++)
	s.insert({{min(din[i],dout[i]),dout[i]},i});
      for(int i=1;i<=4*n/7;i++)
	{
	  auto p=*s.rbegin();
	  s.erase(p);
	  //cout<<p.second<<endl;
	  ans.push_back(p.second);
	  del[p.second]=true;
	  for(auto u:g[p.second])
	    if(!del[u])
	      {
		s.erase({{min(din[u],dout[u]),dout[u]},u});
		din[u]--;
		s.insert({{min(din[u],dout[u]),dout[u]},u});
	      }
	  for(auto u:rg[p.second])
	    if(!del[u])
	      {
		s.erase({{min(din[u],dout[u]),dout[u]},u});
		dout[u]--;
		s.insert({{min(din[u],dout[u]),dout[u]},u});
	      }
	}
      cout<<ans.size()<<endl;
      for(auto x:ans)
	cout<<x<<" ";
      cout<<endl;
      clearing();
    }
  return 0;
}


n = int(raw_input())
ns = map(int, raw_input().split())
l, r = 0, n - 1

res = []
pre = -1
while l <= r:
    if ns[l] < ns[r] and ns[l] > pre:
        pre = ns[l]
        res.append('L')
        l += 1
    elif ns[r] > pre:
        pre = ns[r]
        res.append('R')
        r -= 1
    else:
        break

print len(res)
print ''.join(res)


n = int(raw_input())
ns = map(int, raw_input().split())
l, r = 0, n - 1

res = []
pre = -1
while l <= r:
    if min(ns[l], ns[r]) > pre:
        if ns[l] < ns[r]:
            pre = ns[l]
            res.append('L')
            l += 1
        else:
            pre = ns[r]
            res.append('R')
            r -= 1
    elif ns[l] > pre:
        pre = ns[l]
        res.append('L')
        l += 1
    elif ns[r] > pre:
        pre = ns[r]
        res.append('R')
        r -= 1
    else:
        break

print len(res)
print ''.join(res)


###########################################
#  Author : Uranux
#  Created On : Wed Apr 24 2019
#  File : C.py
###########################################

from math import sqrt

def input_s():
    return raw_input()

def input_i():
    return int(raw_input())

def input_2_i():
    arr = map(int, raw_input().split())
    return arr[0], arr[1]

def input_3_i():
    arr = map(int, raw_input().split())
    return arr[0], arr[1], arr[2]

def input_i_array():
    return map(int, raw_input().split())

def get_presum(n, arr):
    presum = [arr[0]]
    for i in range(1, n):
        presum.append(presum[i - 1] + arr[i])
    return presum

def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)

def lcm(a, b):
    return a * b / gcd(a, b)

def calc(n, arr):
    l_pos = 0
    r_pos = n - 1
    ans = []
    cnt_val = 0
    while l_pos <= r_pos:
        if cnt_val < arr[l_pos] and cnt_val < arr[r_pos]:
            if arr[l_pos] < arr[r_pos]:
                ans.append('L')
                cnt_val = arr[l_pos]
                l_pos += 1
            elif arr[l_pos] > arr[r_pos]:
                ans.append('R')
                cnt_val = arr[r_pos]
                r_pos -= 1
            else:
                cnt_ans1 = 0
                for i in range(l_pos, r_pos):
                    if arr[i] >= arr[i + 1]:
                        break
                    cnt_ans1 += 1
                cnt_ans2 = 0
                for i in range(r_pos, l_pos + 1, -1):
                    if arr[i] >= arr[i - 1]:
                        break
                    cnt_ans2 += 1

                if cnt_ans1 > cnt_ans2:
                    ans.append('L' * (cnt_ans1 + 1))
                else:
                    ans.append('R' * (cnt_ans2 + 1))

                break

        elif cnt_val < arr[l_pos]:
            ans.append('L')
            cnt_val = arr[l_pos]
            l_pos += 1
        elif cnt_val < arr[r_pos]:
            ans.append('R')
            cnt_val = arr[r_pos]
            r_pos -= 1
        else:
            break

    return ''.join(ans)

"""
5
1 2 4 3 2
"""

n = input_i()
arr = input_i_array()

s = calc(n, arr)
print len(s)
print s


#include <stdio.h>
#include <stdlib.h>

int stoi(){
        char s[11];
        int cnt = 0;
        while((s[cnt++]=getchar())!='\n' &&s[cnt-1] != ' ');
        s[cnt] = '\0';
        return atoi(s);
}
int main(void){
        int n = stoi();
        int *a = (int *)malloc(sizeof(int)*n);
        for(int k=0;k<n;k++) a[k] = stoi();
        int f=0,l=n-1;
        int m=-1;
        char *s = (char *)malloc(sizeof(char)*(n+1));
        int cnt=0;
        while(f<=l){
                if(m < a[f] && m < a[l]){
                        if(a[f]>a[l]){
                                m = a[l--];
                                s[cnt++] = 'R';
                        }else{
                                m = a[f++];
                                s[cnt++] = 'L';
                        }
                        continue;
                }else if(m < a[f]){
                        m = a[f++];
                        s[cnt++] = 'L';
                        continue;
                }else if(m < a[l]){
                        m = a[l--];
                        continue;
                }
                break;
        }
        s[cnt] = '\0';
        printf("%d\n%s\n",cnt,s);
        return 0;
}


#include <stdio.h>
#include <stdlib.h>

int stoi(){
        char s[11];
        int cnt = 0;
        while((s[cnt++]=getchar())!='\n' &&s[cnt-1] != ' ');
        s[cnt] = '\0';
        return atoi(s);
}
int main(void){
        int n = stoi();
        int *a = (int *)malloc(sizeof(int)*n);
        for(int k=0;k<n;k++) a[k] = stoi();
        int f=0,l=n-1;
        int m=-1;
        char *s = (char *)malloc(sizeof(char)*(n+1));
        int cnt=0;
        while(f<=l){
                if(m < a[f] && m < a[l]){
                        if(a[f]>a[l]){
                                m = a[l--];
                                s[cnt++] = 'R';
                        }else{
                                m = a[f++];
                                s[cnt++] = 'L';
                        }
                        continue;
                }else if(m < a[f]){
                        m = a[f++];
                        s[cnt++] = 'L';
                        continue;
                }else if(m < a[l]){
                        m = a[l--];
                        s[cnt++] = 'R';
                        continue;
                }
                break;
        }
        s[cnt] = '\0';
        printf("%d\n%s\n",cnt,s);
        return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int incres(int *a,int f,int l,char *s,int m){
        int cnt =0;
        while(f<=l){
                if(m < a[f] && m < a[l]){
                        if(a[f]>a[l]){
                                m = a[l--];
                                s[cnt++] = 'R';
                        }else if(a[f]<a[l] || f==l){
                                m = a[f++];
                                s[cnt++] = 'L';
                        }else{
                                char *ss1 = (char *)malloc(sizeof(char)*(l-f));
                                char *ss2 = (char *)malloc(sizeof(char)*(l-f));
                                int le = incres(a+f+1,0,l-f-1,ss1,a[f]);
                                int ri = incres(a+f,0,l-f-1,ss2,a[l]);
                                if(le > ri){
                                        s[cnt++] = 'L';
                                        memcpy(s+cnt,ss1,sizeof(char)*le);
                                        cnt +=le;
                                }else{
                                        s[cnt++] = 'R';
                                        memcpy(s+cnt,ss2,sizeof(char)*ri);
                                        cnt +=ri;
                                }
                                break;
                        }
                        continue;
                }else if(m < a[f]){
                        m = a[f++];
                        s[cnt++] = 'L';
                        continue;
                }else if(m < a[l]){
                        m = a[l--];
                        s[cnt++] = 'R';
                        continue;
                }
                break;
        }
        s[cnt] = '\0';
        return cnt;
}
int stoi(){
        char s[11];
        int cnt = 0;
        while((s[cnt++]=getchar())!='\n' &&s[cnt-1] != ' ');
        s[cnt] = '\0';
        return atoi(s);
}
int main(void){
        int n = stoi();
        int *a = (int *)malloc(sizeof(int)*n);
        for(int k=0;k<n;k++) a[k] = stoi();
        int f=0,l=n-1;
        char *s = (char *)malloc(sizeof(char)*(n+1));
        int cnt = incres(a,f,l,s,-1);
        printf("%d\n%s\n",cnt,s);
        return 0;
}


#include<stdlib.h>
#include<stdio.h>
#include<string.h>

typedef long long int lli;
typedef struct bst bst;

#define TRUE         (1==1)
#define FALSE        (!(TRUE))
#define in32(x)      scanf("%d", &(x))
#define in64(x)      scanf("%I64d", &(x))
#define instr(x)     scanf("%s", (x))
#define out32(x)     printf("%d\n", (x))
#define out64(x)     printf("%I64d\n", (x))
#define outstr(x)    printf("%s\n", (x))
#define sigNUM(x)    (((x)==0)?0:(((x)<0)?-1:1))
#define max(a,b)     ((a)<(b))?(b):(a)
#define min(a,b)     ((a)>(b))?(b):(a)
#define abs(a)       ((a)<0)?(-(a)):(a)
#define fo3(i,a,b)   for(i=(int)(a);i<=(int)(b);i++)
#define of3(i,a,b)   for(i=(int)(a);i>=(int)(b);i--)

int comp(lli * f, lli * s) {
    if (sigNUM(f[0]) != sigNUM(s[0]))
        return sigNUM(f[0]) - sigNUM(s[0]);
    if (sigNUM(f[1]) != sigNUM(s[1]))
        return sigNUM(f[1]) - sigNUM(s[1]);
    if (f[0]*s[1] != s[0]*f[1])
        return sigNUM(f[0]*s[1] - f[0]*s[1]);
    return f[2] - s[2];
}

void sort(lli ** arr, int size) { // CHANGE THIS
    int i;
    if (size < 6) {
        int nSize = 0;
        while (size != 0) {
            nSize = 0;
            for (i = 0; i < size - 1; i++)
                if (comp(arr[i], arr[i + 1]) > 0) { // CHANGE THIS
                    nSize = i + 1;
                    lli * tmp = arr[i];
                    arr[i] = arr[i + 1];
                    arr[i+1] = tmp;
                }
            size = nSize;
        }
        return;
    }
    lli ** tmp = (lli **)calloc(size, sizeof(lli *)); // CHANGE THIS
    int lSize = (size >> 1);
    int rSize = size - lSize;
    sort(arr, lSize); sort(arr + lSize, rSize);
    int lPtr = 0, rPtr = 0;
    int ptr = 0;
    while (ptr != size) {
        if (lPtr == lSize)
            tmp[ptr] = arr[lSize + (rPtr++)];
        else if (rPtr == rSize || comp(arr[lPtr], arr[lSize + rPtr]) <= 0) // CHANGE THIS
            tmp[ptr] = arr[lPtr++];
        else
            tmp[ptr] = arr[lSize + (rPtr++)];
        ptr++;
    }
    for (i = 0; i < size; i++)
        arr[i] = tmp[i];
    free(tmp);
}

#define MOD    998244353
#define MAX    200010
#define SENT   -1
int arr[MAX];
int n;

int doLeft() {
    int fptr = 0;
    int bptr = n-1;
    int last = -1;
    int ret = 0;
    while (fptr <= bptr) {
        if (last < arr[fptr] && last < arr[bptr] && arr[fptr] != arr[bptr]) {
            if (arr[fptr] < arr[bptr])
                last = arr[fptr++];
            else
                last = arr[bptr--];
        } else if (last < arr[fptr] && last < arr[bptr])
            last = arr[fptr++]; // TIE
        else if (last < arr[bptr])
            last = arr[bptr--];
        else if (last < arr[fptr])
            last = arr[fptr++];
        else
            return ret;
        ret++;
    }
    return ret;
}
int doRight() {
    int fptr = 0;
    int bptr = n-1;
    int last = -1;
    int ret = 0;
    while (fptr <= bptr) {
        if (last < arr[fptr] && last < arr[bptr] && arr[fptr] != arr[bptr]) {
            if (arr[fptr] < arr[bptr])
                last = arr[fptr++];
            else
                last = arr[bptr--];
        } else if (last < arr[fptr] && last < arr[bptr])
            last = arr[bptr--]; // TIE
        else if (last < arr[bptr])
            last = arr[bptr--];
        else if (last < arr[fptr])
            last = arr[fptr++];
        else
            return ret;
        ret++;
    }
    return ret;
}
int printLeft() {
    int fptr = 0;
    int bptr = n-1;
    int last = -1;
    int ret = 0;
    while (fptr <= bptr) {
        if (last < arr[fptr] && last < arr[bptr] && arr[fptr] != arr[bptr]) {
            if (arr[fptr] < arr[bptr]) {
                last = arr[fptr++];
                printf("L");
            } else {
                last = arr[bptr--];
                printf("R");
            }
        } else if (last < arr[fptr] && last < arr[bptr]) {
            last = arr[fptr++]; // TIE
            printf("L");
        } else if (last < arr[fptr]) {
            last = arr[fptr++];
            printf("L");
        } else if (last < arr[bptr]) {
            last = arr[bptr--];
            printf("R");
        } else
            return ret;
        ret++;
    }
    return ret;
}
int printRight() {
    int fptr = 0;
    int bptr = n-1;
    int last = -1;
    int ret = 0;
    while (fptr <= bptr) {
        if (last < arr[fptr] && last < arr[bptr] && arr[fptr] != arr[bptr]) {
            if (arr[fptr] < arr[bptr]) {
                last = arr[fptr++];
                printf("L");
            } else {
                last = arr[bptr--];
                printf("R");
            }
        } else if (last < arr[fptr] && last < arr[bptr]) {
            last = arr[bptr--]; // TIE
            printf("R");
        } else if (last < arr[fptr]) {
            last = arr[fptr++];
            printf("L");
        } else if (last < arr[bptr]) {
            last = arr[bptr--];
            printf("R");
        } else
            return ret;
        ret++;
    }
    return ret;
}

int main(void) {
    int i;
    in32(n);
    fo3(i,0,n-1)
        in32(arr[i]);
    int left = doLeft();
    int right = doRight();
    if (left > right) {
        out32(left);
        printLeft();
    } else {
        out32(right);
        printRight();
    }
    outstr("");
    return 0;
}


#include<stdio.h>
int main()
{
    int k,i,last_element=0,j=0;
    scanf("%d",&k);
    int a[k];
    for(i=0;i<k;i++)
    {
        scanf("%d",&a[i]);
    }
    char seq[k];
    int l=0,r=k-1;
    for(i=0;i<k;i++)
    {
        if(l==r)
        {
            if(a[l]>last_element)
            {
                seq[i]='R';
                last_element=a[l];
                j++;
            }
            else
            {
                break;
            }
        }
        else if(a[l]<a[r])
        {
            if(a[l]>last_element)
            {
                last_element=a[l];
                seq[i]='L';
                l++;
                j++;
            }
            else if(a[r]>last_element)
            {
                last_element=a[r];
                seq[i]='R';
                r--;
                j++;
            }
            else
            {
                break;
            }
        }
        else
        {
            if(a[r]>last_element)
            {
                last_element=a[r];
                seq[i]='R';
                r--;
                j++;
            }
            else if(a[l]>last_element)
            {
                last_element=a[l];
                seq[i]='L';
                l++;
                j++;
            }
            else
            {
                break;
            }
        }
    }
    printf("%d\n",j);
    for(i=0;i<j;i++)
    {
        printf("%c",seq[i]);
    }
    return 0;
}


#include<stdio.h>
int main()
{
    int k,i,last_element=0,j=0;
    scanf("%d",&k);
    int a[k];
    for(i=0;i<k;i++)
    {
        scanf("%d",&a[i]);
    }
    char seq[k];
    int l=0,r=k-1;
    for(i=0;i<k;i++)
    {
        if(l==r)
        {
            seq[i]='R';
            last_element=a[l];
            j++;
        }
        else if(a[l]<a[r])
        {
            if(a[l]>last_element)
            {
                last_element=a[l];
                seq[i]='L';
                l++;
                j++;
            }
            else if(a[r]>last_element)
            {
                last_element=a[r];
                seq[i]='R';
                r--;
                j++;
            }
            else
            {
                break;
            }
        }
        else
        {
            if(a[r]>last_element)
            {
                last_element=a[r];
                seq[i]='R';
                r--;
                j++;
            }
            else if(a[l]>last_element)
            {
                last_element=a[l];
                seq[i]='L';
                l++;
                j++;
            }
            else
            {
                break;
            }
        }
    }
    printf("%d\n",j);
    for(i=0;i<j;i++)
    {
        printf("%c",seq[i]);
    }
    return 0;
}


#include<stdio.h>
int main(){
    int ar[200100];
    char lr[200100];
    int n;
    scanf("%d",&n);
    int i,lol,count=0;
    for(i=0;i<n;i++){
        scanf("%d",&ar[i]);
    }
    int j=n-1;
    lol=0;
   int m;
    for(m=0,i=0;j>=i;count++,m++){
    //	printf("%d\t%d\n%d\t%d\n",i,ar[i],j,ar[j]);
         if(ar[i]<ar[j] && ar[i]>lol){
        lol=ar[i];
     //   printf("%d\t",lol);
        i++;
        lr[m]='L';
    }
    else if(ar[j]<ar[i] && ar[j]>lol){
        lol=ar[j];
    //    printf("%d\t",lol);
        j--;
        lr[m]='R';
    }
    else if(ar[i]>lol){
    	lol=ar[i];
     //   printf("%d\t",lol);
        i++;
        lr[m]='L';
    }

    else if(ar[j]>lol){
    	lol=ar[j];
     //   printf("%d\t",lol);
        j--;
        lr[m]='R';
    }

    else {
    //	printf("bal\n");
    	break;
    }

    }
 //   printf("%d\t%d\n%d\t%d\n",i,ar[i],j,ar[j]);
    /*if(count>200000){
    	count=200000;
    	lr[m]='\0';
    }*/
    lr[m+1]='\0';
    printf("%d\n%s\n",count,lr);
    return 0;
}


#include<stdio.h>
int main(){
    int ar[200100];
    char lr[200100];
    int n;
    scanf("%d",&n);
    int i,lol,count=0;
    for(i=0;i<n;i++){
        scanf("%d",&ar[i]);
    }
    int j=n-1;
    lol=0;
   int m;
    for(m=0,i=0;j>i;count++,m++){
    //	printf("%d\t%d\n%d\t%d\n",i,ar[i],j,ar[j]);
         if(ar[i]<ar[j] && ar[i]>lol){
        lol=ar[i];
     //   printf("%d\t",lol);
        i++;
        lr[m]='L';
    }
    else if(ar[j]<ar[i] && ar[j]>lol){
        lol=ar[j];
    //    printf("%d\t",lol);
        j--;
        lr[m]='R';
    }
    else if(ar[i]>lol){
    	lol=ar[i];
     //   printf("%d\t",lol);
        i++;
        lr[m]='L';
    }

    else if(ar[j]>lol){
    	lol=ar[j];
     //   printf("%d\t",lol);
        j--;
        lr[m]='R';
    }

    else {
    //	printf("bal\n");
    	break;
    }

    }
 //   printf("%d\t%d\n%d\t%d\n",i,ar[i],j,ar[j]);
    /*if(count>200000){
    	count=200000;
    	lr[m]='\0';
    }*/
    lr[m+1]='\0';
    printf("%d\n%s\n",count,lr);
    return 0;
}


#include<stdio.h>
int main(){
    int ar[200100];
    char lr[200100];
    int n;
    scanf("%d",&n);
    int i,lol,count=0;
    for(i=0;i<n;i++){
        scanf("%d",&ar[i]);
    }
    int j=n-1;
    lol=0;
   int m;
    for(m=0,i=0;;count++,m++){
    //	printf("%d\t%d\n%d\t%d\n",i,ar[i],j,ar[j]);
         if(ar[i]<ar[j] && ar[i]>lol){
        lol=ar[i];
     //   printf("%d\t",lol);
        i++;
        lr[m]='L';
    }
    else if(ar[j]<ar[i] && ar[j]>lol){
        lol=ar[j];
    //    printf("%d\t",lol);
        j--;
        lr[m]='R';
    }
    else if(ar[i]>lol){
    	lol=ar[i];
     //   printf("%d\t",lol);
        i++;
        lr[m]='L';
    }

    else if(ar[j]>lol){
    	lol=ar[j];
     //   printf("%d\t",lol);
        j--;
        lr[m]='R';
    }

    else {
    //	printf("bal\n");
    	break;
    }

    }
 //   printf("%d\t%d\n%d\t%d\n",i,ar[i],j,ar[j]);
    if(count>200000){
    	count=200000;
    }
    lr[m+1]='\0';
    printf("%d\n%s\n",count,lr);
    return 0;
}


#include<stdio.h>
int main(){
    int ar[200100];
    char lr[200100];
    int n;
    scanf("%d",&n);
    int i,lol,count=0;
    for(i=0;i<n;i++){
        scanf("%d",&ar[i]);
    }
    int j=n-1;
    lol=0;
   int m;
    for(m=0,i=0;;count++,m++){
    //	printf("%d\t%d\n%d\t%d\n",i,ar[i],j,ar[j]);
         if(ar[i]<ar[j] && ar[i]>lol){
        lol=ar[i];
     //   printf("%d\t",lol);
        i++;
        lr[m]='L';
    }
    else if(ar[j]<ar[i] && ar[j]>lol){
        lol=ar[j];
    //    printf("%d\t",lol);
        j--;
        lr[m]='R';
    }
    else if(ar[i]>lol){
    	lol=ar[i];
     //   printf("%d\t",lol);
        i++;
        lr[m]='L';
    }

    else if(ar[j]>lol){
    	lol=ar[j];
     //   printf("%d\t",lol);
        j--;
        lr[m]='R';
    }

    else {
    //	printf("bal\n");
    	break;
    }

    }
 //   printf("%d\t%d\n%d\t%d\n",i,ar[i],j,ar[j]);

    lr[m+1]='\0';
    printf("%d\n%s\n",count,lr);
    return 0;
}


#include<stdio.h>
int main(){
    int ar[200100];
    char lr[200100];
    int n;
    scanf("%d",&n);
    int i,lol,count=0;
    for(i=0;i<n;i++){
        scanf("%d",&ar[i]);
    }
    int j=n-1;
    lol=0;
   int m;
    for(m=0,i=0;;count++,m++){
    //	printf("%d\t%d\n%d\t%d\n",i,ar[i],j,ar[j]);
         if(ar[i]<ar[j] && ar[i]>lol){
        lol=ar[i];
     //   printf("%d\t",lol);
        i++;
        lr[m]='L';
    }
    else if(ar[j]<ar[i] && ar[j]>lol){
        lol=ar[j];
    //    printf("%d\t",lol);
        j--;
        lr[m]='R';
    }
    else if(ar[i]>lol){
    	lol=ar[i];
     //   printf("%d\t",lol);
        i++;
        lr[m]='L';
    }

    else if(ar[j]>lol){
    	lol=ar[j];
     //   printf("%d\t",lol);
        j--;
        lr[m]='R';
    }

    else {
    //	printf("bal\n");
    	break;
    }

    }
 //   printf("%d\t%d\n%d\t%d\n",i,ar[i],j,ar[j]);
    if(count>200000){
    	count=200000;
    	lr[m]='\0';
    }
    lr[m+1]='\0';
    printf("%d\n%s\n",count,lr);
    return 0;
}


#include <stdio.h>

void helpme(int size, int ar[200001]){
    char a[200001];
    int begin = 0, end = size-1, prev, i = 0;
    if(ar[begin] <= ar[end]){
        prev = ar[begin];
        *(a+i) = 'L';
        i++;
        begin++;
    }
    else{
        prev = ar[end];
        *(a+i) = 'R';
        i++;
        end--;
    }
    while(begin <= end){
        if(begin == end){
            if(ar[begin] > prev){
                if(prev == ar[begin-1]){
                    *(a+i) = 'R';
                    i++;
                    break;
                }
                else{
                    *(a+i) = 'L';
                    i++;
                    break;
                }
            }else
            break;
        }
        if(ar[begin] > prev){
            if(ar[begin] > ar[end] && ar[end] > prev){
                prev = ar[end];
                *(a+i) = 'R';
                i++;
                end--;
            }else if(ar[begin] > ar[end] && ar[end] < prev){
                prev = ar[begin];
                *(a+i) = 'L';
                i++;
                begin++;
            }
            else if(ar[begin]<ar[end]){
                prev = ar[begin];
                *(a+i) = 'L';
                i++;
                begin++;
            }
        }
        else if(ar[end] > prev){
                prev = ar[end];
                *(a+i) = 'R';
                i++;
                end--;
        }
        else
            break;
    }
    *(a+i) = '\0';
    printf("%d\n", i);
    puts(a);
}

int main(){

    int n, i, ar[200001];
    scanf("%d", &n);
    for(i = 0; i<n; i++)
        scanf("%d", &ar[i]);
    helpme(n, ar);

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;
    scanf("%d",&n);
    int a[n];
    for(int i=0; i<n; i++)
        scanf("%d",&a[i]);

    int t=0,now_i=0;
    char x[n];
    int l_item=0,r_item=0;
    int left=0,right=n-1;

    while(left+l_item <= right-r_item)
    {

        if(l_item==0 && r_item==0)
        {

            if(a[left+l_item] < a[right-r_item])
            {
                if(a[left+l_item] > now_i)
                {
                    x[t]='L';
                    now_i=a[left+l_item];
                    t++;
                    left++;
                    //printf("%d L %d/%d/%d\n",t,left,l_item,now_i);
                }
                else
                {
                    l_item++;
                }
            }
            else
            {
                if(a[right-r_item] > now_i)
                {
                    x[t]='R';
                    now_i=a[right-r_item];
                    t++;
                    right--;
                    //printf("%d R %d/%d/%d\n",t,right,r_item,now_i);
                }
                else
                {
                    r_item++;
                }
            }
        }
        else if(l_item!=0 && r_item!=0)
            break;
        else
        {
            if(l_item == 0)
            {
                if(a[left+l_item] > now_i)
                {
                    x[t]='L';
                    now_i=a[left+l_item];
                    t++;
                    left++;
                    //printf("%d L %d/%d/%d\n",t,left,l_item,now_i);
                }
                else
                {
                    l_item++;
                }
            }
            else if(r_item == 0)
            {
                if(a[right-r_item] > now_i)
                {
                    x[t]='R';
                    now_i=a[right-r_item];
                    t++;
                    right--;
                    //printf("%d R %d/%d/%d\n",t,right,r_item,now_i);
                }
                else
                {
                    r_item++;
                }
            }
        }
    }

    printf("%d\n",t);
    for(int i=0; i<t; i++)
    {
        printf("%c",x[i]);
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;
    scanf("%d",&n);
    int a[n];
    for(int i=0; i<n; i++)
        scanf("%d",&a[i]);

    int t=0,nti=0;
    char x[n];
    int lti=0,rti=n-1;

    while(lti <= rti)
    {
        if(a[lti] > a[rti])
        {
            if(a[rti] > nti)
            {
                x[t]='R';
                nti=a[rti];
                t++;
            }
            else
            {

            }

            rti--;
        }
        else
        {
            if(a[lti] > nti)
            {
                x[t]='L';
                nti=a[lti];
                t++;
            }
            else
            {

            }
            lti++;
        }
    }

    printf("%d\n",t);
    for(int i=0; i<t; i++)
    {
        printf("%c",x[i]);
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;
    scanf("%d",&n);
    int a[n];
    for(int i=0; i<n; i++)
        scanf("%d",&a[i]);

    int t=0,now_i=0;
    char x[n];
    int l_item=0,r_item=0;
    int left=0,right=n-1;

    while(left+l_item <= right-r_item)
    {
        if(a[left+l_item] < a[right-r_item])
        {
            if(a[left+l_item] > now_i)
            {
                x[t]='L';
                now_i=a[left+l_item];
                t++;
                left++;
                //printf("%d L %d/%d/%d\n",t,left,l_item,now_i);
            }
            else
            {
                l_item++;
            }
        }
        else if(a[right-r_item] < a[left+l_item])
        {
            if(a[right-r_item] > now_i)
            {
                x[t]='R';
                now_i=a[right-r_item];
                t++;
                right--;
                //printf("%d R %d/%d/%d\n",t,right,r_item,now_i);
            }
            else
            {
                r_item++;
            }
        }
        else
        {
            if(l_item < r_item)
            {
                x[t]='L';
                now_i=a[left+l_item];
                t++;
                left++;
            }
            else
            {
                x[t]='R';
                now_i=a[right-r_item];
                t++;
                right--;
            }
        }

        if(l_item!=0 && r_item!=0)
            break;
    }

    printf("%d\n",t);
    for(int i=0; i<t; i++)
    {
        printf("%c",x[i]);
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;
    scanf("%d",&n);
    int a[n];
    for(int i=0; i<n; i++)
        scanf("%d",&a[i]);

    int t=0,now_i=0;
    char x[n];
    int l_item=0,r_item=0;
    int left=0,right=n-1;

    while(left+l_item <= right-r_item)
    {
        if(a[left+l_item] < a[right-r_item])
        {
            if(a[left+l_item] > now_i)
            {
                x[t]='L';
                now_i=a[left+l_item];
                t++;
                left++;
                //printf("%d L %d/%d/%d\n",t,left,l_item,now_i);
            }
            else
            {
                l_item++;
            }
        }
        else if(a[right-r_item] < a[left+l_item])
        {
            if(a[right-r_item] > now_i)
            {
                x[t]='R';
                now_i=a[right-r_item];
                t++;
                right--;
                //printf("%d R %d/%d/%d\n",t,right,r_item,now_i);
            }
            else
            {
                r_item++;
            }
        }
        else
        {
            if(l_item < r_item)
            {
                x[t]='L';
                now_i=a[left+l_item];
                t++;
                left++;
            }
            else
            {
                x[t]='R';
                now_i=a[right-r_item];
                t++;
                right--;
            }
        }
    }

    printf("%d\n",t);
    for(int i=0; i<t; i++)
    {
        printf("%c",x[i]);
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;
    scanf("%d",&n);
    int a[n];
    for(int i=0; i<n; i++)
        scanf("%d",&a[i]);

    int t=0,nti=0;
    char x[n];
    int lti=0,rti=n-1;

    while(lti <= rti)
    {
        if(a[lti] < a[rti])
        {
            if(a[lti] > nti)
            {
                x[t]='L';
                nti=a[lti];
                t++;
            }
            else
            {

            }

            lti++;
        }
        else
        {
            if(a[rti] > nti)
            {
                x[t]='R';
                nti=a[rti];
                t++;
            }
            else
            {

            }
            rti--;
        }
    }

    printf("%d\n",t);
    for(int i=0; i<t; i++)
    {
        printf("%c",x[i]);
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

typedef long long int LL;

int a[200005];

int main(){

	int n; scanf("%d", &n);

	for(int i = 0; i < n; ++i){
		scanf("%d", a+i);
	}

	char* ans = (char*)malloc(n * sizeof(char));

	int ind = 0;

	int last = -1;
	int l = 0, r = n-1;
	while(l <= r){
		if(a[l] < last && a[r] < last) break;

		else if(a[l] > last && a[l] < a[r]){
			// choose a[l]
			last = a[l];
			ans[ind++] = 'L';
			l++;
		}
		else{
			last = a[r];
			ans[ind++] = 'R';
			r--;
		}
	}
	ans[ind] = '\0';
	printf("%d\n%s", ind, ans);

	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

typedef long long int LL;

int a[200005];

int main(){

	int n; scanf("%d", &n);

	for(int i = 0; i < n; ++i){
		scanf("%d", a+i);
	}

	char* ans = (char*)malloc((n+3) * sizeof(char));

	int ind = 0;

	int last = -1;
	int l = 0, r = n-1;
	while(l <= r){
		if(a[l] < last && a[r] < last) break;

		else if((a[l] > last && a[l] < a[r] ) || (a[r] < last)){
			// choose a[l]
			last = a[l];
			ans[ind++] = 'L';
			l++;
		}
		else{
			last = a[r];
			ans[ind++] = 'R';
			r--;
		}
	}
	ans[ind] = '\0';
	printf("%d\n%s", ind, ans);

	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

typedef long long int LL;

int a[200005];

int main(){

	int n; scanf("%d", &n);

	for(int i = 0; i < n; ++i){
		scanf("%d", a+i);
	}

	char* ans = (char*)malloc((n+3) * sizeof(char));

	int ind = 0;

	int last = -1;
	int l = 0, r = n-1;
	while(l <= r){
		if(a[l] < last && a[r] < last) break;

		else if(a[l] > last && a[l] < a[r]){
			// choose a[l]
			last = a[l];
			ans[ind++] = 'L';
			l++;
		}
		else{
			last = a[r];
			ans[ind++] = 'R';
			r--;
		}
	}
	ans[ind] = '\0';
	printf("%d\n%s", ind, ans);

	return 0;
}


#include<stdio.h>
long long int a[200005]={0,};
char opr[200005]={0,};
int main(void)
{
	long long int n=0;
	scanf("%I64d",&n);
	long long int ans=0;
	long long int pre=0;
	long long int i=0;
	long long int j=n-1;
	long long int k=0;
	while(i<n)
	{
		scanf("%I64d",&a[i]);
		i++;
	}
	i=0;
	int flag=-1;
	while(i<=j)
	{
		if(a[i]<pre)
		{
			flag=0;
			break;
		}
		if(a[j]<pre)
		{
			flag=1;
			break;
		}
		if(a[i]<a[j])
		{
			pre=a[i++];
			opr[k++]='L';
		}
		else
		{
			pre=a[j--];
			opr[k++]='R';
		}
	}
	if(flag==0)
	{
		while(i<=j&&a[j]>pre)
		{
			pre=a[j--];
			opr[k++]='R';
		}
	}
	if(flag==1)
	{
		while(i<=j&&a[i]>pre)
		{
			pre=a[i++];
			opr[k++]='L';
		}
	}
	printf("%I64d\n",k);
	i=0;
	while(i<k)
	{
		printf("%c",opr[i++]);
	}
}


#include <stdio.h>
#include <stdlib.h>
#define N 200000

int main() {
    int n;
    int aa[N];
    scanf("%d", &n);
    int i;
    i =-1;
    while (++i < n)
        scanf("%d",&aa[i]);
    char *ans;
    ans = (char *) malloc(sizeof(char)*(n+1));
    int k,l,r, curr;
    curr = l= k = 0;
    r= n-1;
    n;
    while (n){
        if (aa[r] > curr && (aa[r] <= aa[l] || aa[l] <= curr)){
            ans[k++]= 'R';
            curr = aa[r--];
            n--;
        } else if (aa[l] > curr && (aa[l] <= aa[r] || aa[r] <= curr)) {
            ans[k++] = 'L';
            curr = aa[l++];
            n--;
        } else
            break;
    }
    printf("%d\n", k);
    ans[k] = '\0';
    printf("%s\n", ans);
    return 0;
}


#include <stdio.h>

int main(void)
{
    int i;
    int n;
    int c;

    scanf("%d", &n);

    int a[n];
    int ans=0;
    int ansDir[n];

    for(i=0; i<n; i++)
    {
        scanf("%d", &a[i]);
    }

    int l=0, r=n-1;
    c=0;
    for(i=0; i<n; i++)
    {
        if(a[l]>c)
        {
            if(a[l]<=a[r])
            {
                ansDir[ans]=0;
                c=a[l];
                l++;
                ans++;
            }
            else if(a[r]>c)
            {
                ansDir[ans]=1;
                c=a[r];
                r--;
                ans++;
            }
            else
            {
                ansDir[ans]=0;
                c=a[l];
                l++;
                ans++;
            }
        }
        else if(a[r]>c)
        {
            ansDir[ans]=1;
            c=a[r];
            r--;
            ans++;
        }
        else
        {
            break;
        }
    }
    printf("%d\n", ans);
    for(i=0; i<ans; i++)
    {
        if(ansDir[i]==0)
        {
            printf("L");
        }
        else
        {
            printf("R");
        }
    }
    return 0;
}


#include<stdio.h>
#include<stdlib.h>

int main()
{
    int n,*a;
    int i;

    scanf("%d",&n);
    a=(int*)malloc(n*sizeof(int));
    for(i=0;i<n;i++)
        scanf("%d",a+i);

    char *s;
    s=(char*)malloc((n+1)*sizeof(char));

    int x=0,j=0;

    for(i=0;i<n;i++)
    {
        if(a[i]>a[n-1-i]&&a[n-i-1]>x)
        {
            x=a[n-i-1];
            s[i]='R';
            j++;
            continue;
        }
        if(a[i]<a[n-1-i]&&x<a[i])
        {
            x=a[i];
            s[i]='L';
            j++;
            continue;
        }
        break;
    }

    printf("%d",j);
    s[j+1]='\0';

    printf("%s",s);
    return 0;
}


#include<stdio.h>
#include<stdlib.h>

int main()
{
    int n,*a;
    int i;

    scanf("%d",&n);
    a=(int*)malloc(n*sizeof(int));
    for(i=0;i<n;i++)
        scanf("%d",a+i);

    char *s;
    s=(char*)malloc((n+1)*sizeof(char));

    int x=0,j=n-1,k=0;

    i=0;
    int l=1;
    for(;i<n;i++)
    {
        for(;j>=0;j--)
        {
            if(k==n)
            {
                l=1;
                break;
            }
            if(x<a[j]||x<a[i])
            {
                if(a[j]>x&&x>a[i])
                {
                    l=0;
                    x=a[j];
                    s[k]='R';
                    k++;
                    continue;
                }
                if(a[i]>x&&x>a[j])
                {
                    l=0;
                    x=a[i];
                    s[k]='L';
                    k++;
                    break;
                }
                if(a[j]>a[i])
                {
                    l=0;
                    x=a[i];
                    s[k]='L';
                    k++;
                    break;
                }
                if(a[i]>a[j])
                {
                    l=0;
                    x=a[j];
                    s[k]='R';
                    k++;
                    continue;
                }
            }
            l=1;
            break;
        }
        if(l==1)
            break;
    }

    printf("%d\n",k);
    s[k]='\0';
    printf("%s",s);
    return 0;
}