#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>

using namespace std;

stack<int>a;
int b[11000],symbol[11000]={0};
string s[5010];

int main()
{     freopen("STL.in","r",stdin);

      int i,j=0,n,m,x,y,z,k=0;
      cin>>n;
	  /*getchar();
      getline(cin,s);
	  m=s.length();*/
	  //cout<<m<<endl;
      //for(i=0;i<m;i++)cout<<s[i]<<endl;
      int p=1;
      while(cin>>s[p]){
      	p++;
      }
      //cout<<p-1<<endl;

      for(i=1;i<p;i++){
      	//cout<<"+1"<<endl;
      	//cout<<i<<endl;
      	//bool f=false;
      	//cout<<s[i]<<s[i+1]<<endl;
      	if(s[i]=="pair")a.push(1),b[++k]=1;
      	if(s[i]=="int")a.push(2),b[++k]=2;
      	//cout<<a.top()<<endl;
      	//cout<<"size:"<<a.size()<<endl;
      	/*if(!f){
		  //cout<<">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"<<endl;
		  continue;
		  }*/
      	if(a.size()<=2){
		  //cout<<"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"<<endl;
		  continue;
		  }
      	//cout<<"size:"<<a.size()<<endl;
        x=a.top();
		//cout<<x<<endl;
		a.pop();
		//for(j=0;j<a.size();j++)cout<<a.top()<<' ',a.pop();
		//cout<<endl;
      	if(x==2){
      		y=a.top();
			//cout<<y<<endl;
			a.pop();
      		if(y==2){
      			z=a.top();
				//cout<<z<<endl;
				a.pop();
      			if(z==1){
				  a.push(2);
				  symbol[i]++;
				  //cout<<"YES"<<endl;
		  	            }
      			  else{
					 a.push(z);//,cout<<"NOZZZZ"<<endl;
					 a.push(y);
					 a.push(x);
					 continue;
				     }
					}
      		  else{
				 a.push(y);//,cout<<"NOYYY"<<endl;
      		     a.push(x);
      		     continue;
      		     }
				}
      	  else {
			a.push(x);//,cout<<"NOXX"<<endl;
			continue;
		    }
      while(a.size()>2){
	    x=a.top();
		  //cout<<x<<endl;
		  a.pop();
		  //for(j=0;j<a.size();j++)cout<<a.top()<<' ',a.pop();
		  //cout<<endl;
      	  if(x==2){
      		  y=a.top();
			  //cout<<y<<endl;
			  a.pop();
      		  if(y==2){
      			  z=a.top();
				  //cout<<z<<endl;
				  a.pop();
      			  if(z==1){
				    a.push(2);
				    symbol[k]++;
				    //cout<<symbol[k]<<endl;
				    //cout<<"YES"<<endl;
		  	        }
      			    else{
					   a.push(z);//,cout<<"NOZZZZ"<<endl;
					   a.push(y);
					   a.push(x);
					   break;
				       }
					  }
      		    else{
				   a.push(y);//,cout<<"NOYYY"<<endl;
      		       a.push(x);
      		       break;
      		       }
				  }
      	    else {
			  a.push(x);//,cout<<"NOXX"<<endl;
			  break;
		      }
      	}
      //cout<<endl<<"............................................."<<endl;
	  //for(j=0;j<a.size();j++)cout<<a.top()<<' ',a.pop();
	  /*int a1,a2,a3;
	  a1=a.top();
	  cout<<a1<<' ';
	  a.pop();
	  a2=a.top();
	  cout<<a2<<' ';
	  a.pop();
	  a3=a.top();
	  cout<<a3<<' ';
	  a.pop();
	  a.push(a3);
	  a.push(a2);
	  a.push(a1);*/
	  }
	  //cout<<a.size()<<endl;

	  while(a.size()>2){
	    x=a.top();
		  //cout<<x<<endl;
		  a.pop();
		  //for(j=0;j<a.size();j++)cout<<a.top()<<' ',a.pop();
		  //cout<<endl;
      	  if(x==2){
      		  y=a.top();
			  //cout<<y<<endl;
			  a.pop();
      		  if(y==2){
      			  z=a.top();
				  //cout<<z<<endl;
				  a.pop();
      			  if(z==1){
				    a.push(2);
				    symbol[k]++;
				    //cout<<symbol[k]<<endl;
				    //cout<<"YES"<<endl;
		  	        }
      			    else{
					   a.push(z);//,cout<<"NOZZZZ"<<endl;
					   a.push(y);
					   a.push(x);
					   break;
				       }
					  }
      		    else{
				   a.push(y);//,cout<<"NOYYY"<<endl;
      		       a.push(x);
      		       break;
      		       }
				  }
      	    else {
			  a.push(x);//,cout<<"NOXX"<<endl;
			  break;
		      }
      	}

      //for(i=1;i<=k;i++)cout<<b[i]<<endl;
      //cout<<"size:"<<a.size()<<endl;
      //for(j=0;j<a.size();j++)cout<<a.top()<<' ',a.pop();
      //cout<<endl;
      /*for(i=1;i<=3;i++){
      	cout<<a.top()<<endl;
      	a.pop();
        }*/
	  //int sum=0;
	  //cout<<symbol[k]<<endl;

      if(a.size()==1&&a.top()==2){
      	for(i=1;i<=k;i++){
      	   if(b[i]==1&&b[i-1]!=2)cout<<"pair<";
      	     else if(b[i]==1&&b[i-1]==2)cout<<",pair<";
      	     else if(n>=2){
			     if(b[i-1]==1)cout<<"int";
			       else cout<<",int";
		   while(symbol[i]>0){
		   	cout<<">";
		   	symbol[i]--;
		     }
		  }
	     }
		cout<<endl;
	    }
		else cout<<"Error occurred"<<endl;

	  return 0;
}


#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>
#include <algorithm>
#define MAXN 100010

using namespace std;

char str[15];
int represent[MAXN];
vector < string > K;
bool noSolution;
string resp;

int solve( int pos, int qt )
{
    if(noSolution)
        return -1;
    //printf(" - Pos: %d String: %s -", pos, K[pos].c_str());
    if( qt == 0 && K[pos] == "int")
    {
        if(K.size() == 1)
        {
            printf("int\n");
            return -1;
        }
        if(noSolution)
            return -1;
        printf("Error occurred\n");
        noSolution = true;
        return -1;
    }
    if( pos == K.size()-1 && K[pos] == "pair")
    {
        if(noSolution)
            return -1;
        printf("Error occurred\n");
        noSolution = true;
        return -1;
    }
    if( pos > K.size()-1 )
    {
        if(noSolution)
            return -1;
        printf("Error occurred\n");
        noSolution = true;
        return -1;
    }
    resp += K[pos];
    if(K[pos] == "pair")
    {
        resp += "<";
        int newpos = solve(pos+1,1);
        resp += ",";
        int k = solve(newpos,2);
        if( noSolution )
            return -1;
        resp += ">";
        return k;
    }
    else
        return pos+1;
}

int main()
{
    int n;
    scanf("%d ", &n);
    int ct = 0;
    vector < int > Saida;
    char c;
    while((c = getchar()) && c != '\n')
    {
        string aux = "";
        do
        {
            aux += c;
        }while( (c = getchar()) != ' ' && c != '\n');

        K.push_back(aux);
        if( c == '\n' )
            break;

        if(aux == "pair")
            Saida.push_back(ct);
        ct++;
    }
    noSolution = false;
    resp = "";
    int kout = solve(0,0);
    if( kout != -1 )
        printf("%s\n", resp.c_str());

    return 0;
}


#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>
#include <algorithm>
#define MAXN 100010

using namespace std;

char str[15];
int represent[MAXN];
vector < string > K;
bool noSolution;
string resp;

int solve( int pos, int qt )
{
    if(noSolution)
        return -1;
    //printf(" - Pos: %d String: %s -", pos, K[pos].c_str());
    if( qt == 0 && K[pos] == "int")
    {
        if(noSolution)
            return -1;
        printf("Error occurred\n");
        noSolution = true;
        return -1;
    }
    if( pos == K.size()-1 && K[pos] == "pair")
    {
        if(noSolution)
            return -1;
        printf("Error occurred\n");
        noSolution = true;
        return -1;
    }
    if( pos > K.size()-1 )
    {
        if(noSolution)
            return -1;
        printf("Error occurred\n");
        noSolution = true;
        return -1;
    }
    resp += K[pos];
    if(K[pos] == "pair")
    {
        resp += "<";
        int newpos = solve(pos+1,1);
        resp += ",";
        int k = solve(newpos,2);
        if( noSolution )
            return -1;
        resp += ">";
        return k;
    }
    else
        return pos+1;
}

int main()
{
    int n;
    scanf("%d ", &n);
    int ct = 0;
    vector < int > Saida;
    char c;
    while((c = getchar()) && c != '\n')
    {
        string aux = "";
        do
        {
            aux += c;
        }while( (c = getchar()) != ' ' && c != '\n');

        K.push_back(aux);
        if( c == '\n' )
            break;

        if(aux == "pair")
            Saida.push_back(ct);
        ct++;
    }
    noSolution = false;
    resp = "";
    int kout = solve(0,0);
    if( kout != -1 )
        printf("%s\n", resp.c_str());

    return 0;
}


#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>
#include <algorithm>
#define MAXN 100010

using namespace std;

char str[15];
int represent[MAXN];
vector < string > K;
bool noSolution;
string resp;

int solve( int pos, int qt )
{
    if(noSolution)
        return -1;
    //printf(" - Pos: %d String: %s -", pos, K[pos].c_str());
    if( qt == 0 && K[pos] == "int")
    {
        if(K.size() == 1)
        {
            resp = "int";
            return 2;
        }
        if(noSolution)
            return -1;
        noSolution = true;
        return -1;
    }
    if( pos == K.size()-1 && K[pos] == "pair")
    {
        if(noSolution)
            return -1;
        noSolution = true;
        return -1;
    }
    if( pos > K.size()-1 )
    {
        if(noSolution)
            return -1;
        noSolution = true;
        return -1;
    }
    resp += K[pos];
    if(K[pos] == "pair")
    {
        resp += "<";
        int newpos = solve(pos+1,1);
        resp += ",";
        int k = solve(newpos,2);
        if( noSolution )
            return -1;
        resp += ">";
        return k;
    }
    else
        return pos+1;
}

int main()
{
    int n;
    scanf("%d ", &n);
    int ct = 0;
    vector < int > Saida;
    char c;
    while((c = getchar()) && c != '\n')
    {
        string aux = "";
        do
        {
            aux += c;
        }while( (c = getchar()) != ' ' && c != '\n');

        K.push_back(aux);
        if( c == '\n' )
            break;
        ct++;
    }
    noSolution = false;
    resp = "";
    int kout = solve(0,0);
    //printf("Kout: %d Ct: %d\n", kout,ct);
    if( kout-1 ==  ct )
        printf("%s\n", resp.c_str());
    else
        printf("Error occurred\n");

    return 0;
}


#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>
#include <algorithm>
#define MAXN 100010

using namespace std;

char str[15];
int represent[MAXN];
vector < string > K;
bool noSolution;
string resp;

int solve( int pos, int qt )
{
    if(noSolution)
        return -1;
    //printf(" - Pos: %d String: %s -", pos, K[pos].c_str());
    if( qt == 0 && K[pos] == "int")
    {
        if(K.size() == 1)
        {
            resp = "int";
            return 1;
        }
        if(noSolution)
            return -1;
        noSolution = true;
        return -1;
    }
    if( pos == K.size()-1 && K[pos] == "pair")
    {
        if(noSolution)
            return -1;
        noSolution = true;
        return -1;
    }
    if( pos > K.size()-1 )
    {
        if(noSolution)
            return -1;
        noSolution = true;
        return -1;
    }
    resp += K[pos];
    if(K[pos] == "pair")
    {
        resp += "<";
        int newpos = solve(pos+1,1);
        resp += ",";
        int k = solve(newpos,2);
        if( noSolution )
            return -1;
        resp += ">";
        return k;
    }
    else
        return pos+1;
}

int main()
{
    int n;
    scanf("%d ", &n);
    int ct = 0;
    vector < int > Saida;
    char c;
    while((c = getchar()) && c != '\n')
    {
        string aux = "";
        do
        {
            aux += c;
        }while( (c = getchar()) != ' ' && c != '\n');

        K.push_back(aux);
        if( c == '\n' )
            break;

    }
    noSolution = false;
    resp = "";
    int kout = solve(0,0);
    printf("Kout: %d Ct: %d\n", kout,K.size());
    if( kout ==  K.size() )
        printf("%s\n", resp.c_str());
    else
        printf("Error occurred\n");

    return 0;
}


#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>
#include <algorithm>
#define MAXN 100010

using namespace std;

char str[15];
int represent[MAXN];
vector < string > K;
bool noSolution;
string resp;

int solve( int pos, int qt )
{
    if(noSolution)
        return -1;
    //printf(" - Pos: %d String: %s -", pos, K[pos].c_str());
    if( qt == 0 && K[pos] == "int")
    {
        if(K.size() == 1)
        {
            printf("int\n");
            return -1;
        }
        if(noSolution)
            return -1;
        printf("Error occurred\n");
        noSolution = true;
        return -1;
    }
    if( pos == K.size()-1 && K[pos] == "pair")
    {
        if(noSolution)
            return -1;
        printf("Error occurred\n");
        noSolution = true;
        return -1;
    }
    if( pos > K.size()-1 )
    {
        if(noSolution)
            return -1;
        printf("Error occurred\n");
        noSolution = true;
        return -1;
    }
    resp += K[pos];
    if(K[pos] == "pair")
    {
        resp += "<";
        int newpos = solve(pos+1,1);
        resp += ",";
        int k = solve(newpos,2);
        if( noSolution )
            return -1;
        resp += ">";
        if(K[k] == "int" && K[pos+1] == "int" && K[newpos] == "int" && qt == 0)
        {
            noSolution = true;
            printf("Error occurred\n");
            return -1;
        }
        return k;
    }
    else
        return pos+1;
}

int main()
{
    int n;
    scanf("%d ", &n);
    int ct = 0;
    vector < int > Saida;
    char c;
    while((c = getchar()) && c != '\n')
    {
        string aux = "";
        do
        {
            aux += c;
        }while( (c = getchar()) != ' ' && c != '\n');

        K.push_back(aux);
        if( c == '\n' )
            break;

        if(aux == "pair")
            Saida.push_back(ct);
        ct++;
    }
    noSolution = false;
    resp = "";
    int kout = solve(0,0);
    if( kout != -1 )
        printf("%s\n", resp.c_str());

    return 0;
}


#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>
#include <algorithm>
#define MAXN 100010

using namespace std;

char str[15];
int represent[MAXN];
vector < string > K;
bool noSolution;
string resp;

int solve( int pos, int qt )
{
    if(noSolution)
        return -1;
    //printf(" - Pos: %d String: %s -", pos, K[pos].c_str());
    if( qt == 0 && K[pos] == "int")
    {
        if(K.size() == 1)
        {
            printf("int\n");
            return -1;
        }
        if(noSolution)
            return -1;
        printf("Error occurred\n");
        noSolution = true;
        return -1;
    }
    if( pos == K.size()-1 && K[pos] == "pair")
    {
        if(noSolution)
            return -1;
        printf("Error occurred\n");
        noSolution = true;
        return -1;
    }
    if( pos > K.size()-1 )
    {
        if(noSolution)
            return -1;
        printf("Error occurred\n");
        noSolution = true;
        return -1;
    }
    resp += K[pos];
    if(K[pos] == "pair")
    {
        resp += "<";
        int newpos = solve(pos+1,1);
        resp += ",";
        int k = solve(newpos,2);
        if( noSolution )
            return -1;
        resp += ">";
        if(K[k] == "int" && K[pos+1] == "int" && K[newpos] == "int")
        {
            noSolution = true;
            printf("Error occurred\n");
            return -1;
        }
        return k;
    }
    else
        return pos+1;
}

int main()
{
    int n;
    scanf("%d ", &n);
    int ct = 0;
    vector < int > Saida;
    char c;
    while((c = getchar()) && c != '\n')
    {
        string aux = "";
        do
        {
            aux += c;
        }while( (c = getchar()) != ' ' && c != '\n');

        K.push_back(aux);
        if( c == '\n' )
            break;

        if(aux == "pair")
            Saida.push_back(ct);
        ct++;
    }
    noSolution = false;
    resp = "";
    int kout = solve(0,0);
    if( kout != -1 )
        printf("%s\n", resp.c_str());

    return 0;
}


#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>
#include <algorithm>
#define MAXN 100010

using namespace std;

char str[15];
int represent[MAXN];
vector < string > K;
bool noSolution;
string resp;

int solve( int pos, int qt )
{
    if(noSolution)
        return -1;
    //printf(" - Pos: %d String: %s -", pos, K[pos].c_str());
    if( qt == 0 && K[pos] == "int")
    {
        if(K.size() == 1)
        {
            printf("int\n");
            return -1;
        }
        if(noSolution)
            return -1;
        printf("Error occurred\n");
        noSolution = true;
        return -1;
    }
    if( pos == K.size()-1 && K[pos] == "pair")
    {
        if(noSolution)
            return -1;
        printf("Error occurred\n");
        noSolution = true;
        return -1;
    }
    if( pos > K.size()-1 )
    {
        if(noSolution)
            return -1;
        printf("Error occurred\n");
        noSolution = true;
        return -1;
    }
    resp += K[pos];
    if(K[pos] == "pair")
    {
        resp += "<";
        int newpos = solve(pos+1,1);
        resp += ",";
        int k = solve(newpos,2);
        if( noSolution )
            return -1;
        resp += ">";
        if(K[k] == "int" && K[pos+1] == "int" && K[newpos] == "int" && qt != 0)
        {
            noSolution = true;
            printf("Error occurred\n");
            return -1;
        }
        return k;
    }
    else
        return pos+1;
}

int main()
{
    int n;
    scanf("%d ", &n);
    int ct = 0;
    vector < int > Saida;
    char c;
    while((c = getchar()) && c != '\n')
    {
        string aux = "";
        do
        {
            aux += c;
        }while( (c = getchar()) != ' ' && c != '\n');

        K.push_back(aux);
        if( c == '\n' )
            break;

        if(aux == "pair")
            Saida.push_back(ct);
        ct++;
    }
    noSolution = false;
    resp = "";
    int kout = solve(0,0);
    if( kout != -1 )
        printf("%s\n", resp.c_str());

    return 0;
}


#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>
#include <algorithm>
#define MAXN 100010

using namespace std;

char str[15];
int represent[MAXN];
vector < string > K;
bool noSolution;
string resp;

int solve( int pos, int qt )
{
    if(noSolution)
        return -1;
    //printf(" - Pos: %d String: %s -", pos, K[pos].c_str());
    if( qt == 0 && K[pos] == "int")
    {
        if(K.size() == 1)
        {
            resp = "int";
            return 1;
        }
        if(noSolution)
            return -1;
        noSolution = true;
        return -1;
    }
    if( pos == K.size()-1 && K[pos] == "pair")
    {
        if(noSolution)
            return -1;
        noSolution = true;
        return -1;
    }
    if( pos > K.size()-1 )
    {
        if(noSolution)
            return -1;
        noSolution = true;
        return -1;
    }
    resp += K[pos];
    if(K[pos] == "pair")
    {
        resp += "<";
        int newpos = solve(pos+1,1);
        resp += ",";
        int k = solve(newpos,2);
        if( noSolution )
            return -1;
        resp += ">";
        return k;
    }
    else
        return pos+1;
}

int main()
{
    int n;
    scanf("%d ", &n);
    int ct = 0;
    vector < int > Saida;
    char c;
    while((c = getchar()) && c != '\n')
    {
        string aux = "";
        do
        {
            aux += c;
        }while( (c = getchar()) != ' ' && c != '\n');

        K.push_back(aux);
        if( c == '\n' )
            break;

    }
    noSolution = false;
    resp = "";
    int kout = solve(0,0);
    //printf("Kout: %d Ct: %d\n", kout,K.size());
    if( kout ==  K.size() )
        printf("%s\n", resp.c_str());
    else
        printf("Error occurred\n");

    return 0;
}


#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>
#include <algorithm>
#define MAXN 100010

using namespace std;

char str[15];
int represent[MAXN];
vector < string > K;
bool noSolution;
string resp;

int solve( int pos, int qt )
{
    if(noSolution)
        return -1;
    ///printf(" - Pos: %d String: %s -", pos, K[pos].c_str());
    if( qt == 0 && K[pos] == "int")
    {
        if(K.size() == 1)
        {
            printf("int\n");
            return -1;
        }
        if(noSolution)
            return -1;
        printf("Error occurred\n");
        noSolution = true;
        return -1;
    }
    if( pos == K.size()-1 && K[pos] == "pair")
    {
        if(noSolution)
            return -1;
        printf("Error occurred\n");
        noSolution = true;
        return -1;
    }
    if( pos > K.size()-1 )
    {
        if(noSolution)
            return -1;
        printf("Error occurred\n");
        noSolution = true;
        return -1;
    }
    resp += K[pos];
    if(K[pos] == "pair")
    {
        resp += "<";
        int newpos = solve(pos+1,1);
        resp += ",";
        int k = solve(newpos,2);
        if( noSolution )
            return -1;
        resp += ">";
        if(K[k] == "int" && K[pos+1] == "int" && K[newpos] == "int" && qt == 0)
        {
            noSolution = true;
            printf("Error occurred\n");
            return -1;
        }
        return k;
    }
    else
        return pos+1;
}

int main()
{
    int n;
    scanf("%d ", &n);
    int ct = 0;
    vector < int > Saida;
    char c;
    while((c = getchar()) && c != '\n')
    {
        string aux = "";
        do
        {
            aux += c;
        }while( (c = getchar()) != ' ' && c != '\n');

        K.push_back(aux);
        if( c == '\n' )
            break;

        if(aux == "pair")
            Saida.push_back(ct);
        ct++;
    }
    noSolution = false;
    resp = "";
    int kout = solve(0,0);
    if( kout != -1 )
        printf("%s\n", resp.c_str());

    return 0;
}


#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>
#include <algorithm>
#define MAXN 100010

using namespace std;

char str[15];
int represent[MAXN];
vector < string > K;
bool noSolution;
string resp;

int solve( int pos, int qt )
{
    if(noSolution)
        return -1;
    //printf(" - Pos: %d String: %s -", pos, K[pos].c_str());
    if( qt == 0 && K[pos] == "int")
    {
        if(K.size() == 1)
        {
            printf("int\n");
            return -1;
        }
        if(noSolution)
            return -1;
        printf("Error occurred\n");
        noSolution = true;
        return -1;
    }
    if( pos == K.size()-1 && K[pos] == "pair")
    {
        if(noSolution)
            return -1;
        printf("Error occurred\n");
        noSolution = true;
        return -1;
    }
    if( pos > K.size()-1 )
    {
        if(noSolution)
            return -1;
        printf("Error occurred\n");
        noSolution = true;
        return -1;
    }
    resp += K[pos];
    if(K[pos] == "pair")
    {
        resp += "<";
        int newpos = solve(pos+1,1);
        resp += ",";
        int k = solve(newpos,2);
        if( noSolution )
            return -1;
        resp += ">";
        if(K[k] == "int")
        {
            noSolution = true;
            printf("Error occurred\n");
            return -1;
        }
        return k;
    }
    else
        return pos+1;
}

int main()
{
    int n;
    scanf("%d ", &n);
    int ct = 0;
    vector < int > Saida;
    char c;
    while((c = getchar()) && c != '\n')
    {
        string aux = "";
        do
        {
            aux += c;
        }while( (c = getchar()) != ' ' && c != '\n');

        K.push_back(aux);
        if( c == '\n' )
            break;

        if(aux == "pair")
            Saida.push_back(ct);
        ct++;
    }
    noSolution = false;
    resp = "";
    int kout = solve(0,0);
    if( kout != -1 )
        printf("%s\n", resp.c_str());

    return 0;
}


#include <iostream>
#include <string>
#include <cstdio>
using namespace std;

string s;
char ans[400000];
char *p = "pair";
char *Int = "int";
int n;
int k = 0;
int i = 0;
int m;

bool isint(int q)
{
    if (m - q < 3) return false;
    bool res = true;
    for (int j = 0; j < 3; ++j)
    {
        if (s[q + j] != Int[j]) res = false;
    }
    return res;
}

bool ispair(int q)
{
    if (m - q < 4) return false;
    bool res = true;
    for (int j = 0; j < 4; ++j)
    {
        if (s[q + j] != p[j]) res = false;
    }
    return res;
}

bool getPair()
{
    if (k > 200000) return false;
    if (ispair(i))
    {
        i += 5;
        for (int j = 0; j < 4; ++j) ans[k + j] = p[j];
        k += 4;
        ans[k] = '<';
        k++;
        if (!getPair()) return false;
        ans[k] = ',';
        k++;
        if (!getPair()) return false;
        ans[k] = '>';
        k++;
        return true;
    }
    if (isint(i))
    {
        for (int j = 0; j < 3; ++j) ans[k + j] = Int[j];
        k += 3;
        i += 4;
        n--;
        return true;
    }
    return false;
}
int main()
{
    cin >> n;
    getchar();
    getline(cin, s);
    m = s.length();
    if ((getPair()) && (!n))
    {
        ans[k] = '\n';
        cout << ans;
    }
    else
    {
        cout << "Error occurred" << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstdio>
using namespace std;

string s;
char ans[200000];
char *p = "pair";
char *in = "int";
int n;
int k = 0;
int i = 0;
int m;

bool isint(int q)
{
    if (m - q < 3) return false;
    bool res = true;
    for (int j = 0; j < 3; ++j)
    {
        if (s[q + j] != in[j]) res = false;
    }
    return res;
}

bool ispair(int q)
{
    if (m - q < 4) return false;
    bool res = true;
    for (int j = 0; j < 4; ++j)
    {
        if (s[q + j] != p[j]) res = false;
    }
    return res;
}

bool getPair()
{
    if (ispair(i))
    {
        i += 5;
        for (int j = 0; j < 4; ++j) ans[k + j] = p[j];
        k += 4;
        ans[k] = '<';
        k++;
        if (!getPair()) return false;
        ans[k] = ',';
        k++;
        if (!getPair()) return false;
        ans[k] = '>';
        k++;
        return true;
    }
    if (isint(i))
    {
        for (int j = 0; j < 3; ++j) ans[k + j] = in[j];
        k += 3;
        i += 4;
        n--;
        return true;
    }
    return false;
}
int main()
{
    cin >> n;
    getchar();
    getline(cin, s);
    m = s.length();
    if ((getPair()) && (!n))
    {
        ans[k] = '\n';
        cout << ans;
    }
    else
    {
        cout << "Error occurred" << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstdio>
using namespace std;

string s;
char ans[400000];
char *p = "pair";
char *Int = "int";
int n;
int k = 0;
int i = 0;
int m;

bool isint(int q)
{
    if (m - q < 3) return false;
    bool res = true;
    for (int j = 0; j < 3; ++j)
    {
        if (s[q + j] != Int[j]) res = false;
    }
    return res;
}

bool ispair(int q)
{
    if (m - q < 4) return false;
    bool res = true;
    for (int j = 0; j < 4; ++j)
    {
        if (s[q + j] != p[j]) res = false;
    }
    return res;
}

bool getPair()
{
    if (i >= m - 2) return false;
    if (ispair(i))
    {
        i += 5;
        for (int j = 0; j < 4; ++j) ans[k + j] = p[j];
        k += 4;
        ans[k] = '<';
        k++;
        if (!getPair()) return false;
        ans[k] = ',';
        k++;
        if (!getPair()) return false;
        ans[k] = '>';
        k++;
        return true;
    }
    if (isint(i))
    {
        for (int j = 0; j < 3; ++j) ans[k + j] = Int[j];
        k += 3;
        i += 4;
        n--;
        return true;
    }
    return false;
}
int main()
{
    cin >> n;
    getchar();
    getline(cin, s);
    m = s.length();
    if ((getPair()) && (!n))
    {
        ans[k] = '\n';
        cout << ans;
    }
    else
    {
        cout << "Error occurred" << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstdio>
using namespace std;

string s;
char ans[500000];
char *p = "pair";
char *Int = "int";
int n;
int k = 0;
int i = 0;
int m;

bool isint(int q)
{
    if (m - q < 3) return false;
    bool res = true;
    for (int j = 0; j < 3; ++j)
    {
        if (s[q + j] != Int[j]) res = false;
    }
    return res;
}

bool ispair(int q)
{
    if (m - q < 4) return false;
    bool res = true;
    for (int j = 0; j < 4; ++j)
    {
        if (s[q + j] != p[j]) res = false;
    }
    return res;
}

bool getPair()
{
    if (i >= m) return false;
    if (ispair(i))
    {
        i += 5;
        for (int j = 0; j < 4; ++j) ans[k + j] = p[j];
        k += 4;
        ans[k] = '<';
        k++;
        if (!getPair()) return false;
        ans[k] = ',';
        k++;
        if (!getPair()) return false;
        ans[k] = '>';
        k++;
        return true;
    }
    if (isint(i))
    {
        for (int j = 0; j < 3; ++j) ans[k + j] = Int[j];
        k += 3;
        i += 4;
        n--;
        return true;
    }
    return false;
}
int main()
{
    cin >> n;
    getchar();
    getline(cin, s);
    m = s.length();
    if ((getPair()) && (!n))
    {
        ans[k] = '\n';
        cout << ans;
    }
    else
    {
        cout << "Error occurred" << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstdio>
using namespace std;

string s;
char ans[500000];
char *p = "pair";
char *Int = "int";
int n;
int k = 0;
int i = 0;
int m;

bool isint(int q)
{
    if (m - q < 3) return false;
    bool res = true;
    for (int j = 0; j < 3; ++j)
    {
        if (s[q + j] != Int[j]) res = false;
    }
    return res;
}

bool ispair(int q)
{
    if (m - q < 4) return false;
    bool res = true;
    for (int j = 0; j < 4; ++j)
    {
        if (s[q + j] != p[j]) res = false;
    }
    return res;
}

bool getPair()
{
    if (i >= m) return false;
    if (ispair(i))
    {
        i += 5;
        for (int j = 0; j < 4; ++j) ans[k + j] = p[j];
        k += 4;
        ans[k] = '<';
        k++;
        if (!getPair()) return false;
        ans[k] = ',';
        k++;
        if (!getPair()) return false;
        ans[k] = '>';
        k++;
        return true;
    }
    if (isint(i))
    {
        for (int j = 0; j < 3; ++j) ans[k + j] = Int[j];
        k += 3;
        i += 4;
        n--;
        return true;
    }
    return false;
}
int main()
{
    cin >> n;
    getchar();
    getline(cin, s);
    m = s.length();
    if ((getPair()) && (!n) && (m == i))
    {
        ans[k] = '\n';
        cout << ans;
    }
    else
    {
        cout << "Error occurred" << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstdio>
using namespace std;

string s;
char ans[600000];
char *p = "pair";
char *Int = "int";
int n;
int k = 0;
int i = 0;
int m;

bool isint(int q)
{
    if (m - q < 3) return false;
    bool res = true;
    for (int j = 0; j < 3; ++j)
    {
        if (s[q + j] != Int[j]) res = false;
    }
    return res;
}

bool ispair(int q)
{
    if (m - q < 4) return false;
    bool res = true;
    for (int j = 0; j < 4; ++j)
    {
        if (s[q + j] != p[j]) res = false;
    }
    return res;
}

bool getPair()
{
    if (i >= m - 1) return false;
    if (ispair(i))
    {
        i += 5;
        for (int j = 0; j < 4; ++j) ans[k + j] = p[j];
        k += 4;
        ans[k] = '<';
        k++;
        if (!getPair()) return false;
        ans[k] = ',';
        k++;
        if (!getPair()) return false;
        ans[k] = '>';
        k++;
        return true;
    }
    if (isint(i))
    {
        for (int j = 0; j < 3; ++j) ans[k + j] = Int[j];
        k += 3;
        i += 4;
        n--;
        return true;
    }
    return false;
}
int main()
{
    cin >> n;
    getchar();
    getline(cin, s);
    m = s.length();
    if ((getPair()) && (!n))
    {
        ans[k] = '\n';
        cout << ans;
    }
    else
    {
        cout << "Error occurred" << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstdio>
using namespace std;

string s;
char ans[400000];
char *p = "pair";
char *Int = "int";
int n;
int k = 0;
int i = 0;
int m;

bool isint(int q)
{
    if (m - q < 3) return false;
    bool res = true;
    for (int j = 0; j < 3; ++j)
    {
        if (s[q + j] != Int[j]) res = false;
    }
    return res;
}

bool ispair(int q)
{
    if (m - q < 4) return false;
    bool res = true;
    for (int j = 0; j < 4; ++j)
    {
        if (s[q + j] != p[j]) res = false;
    }
    return res;
}

bool getPair()
{
    if (i >= m - 1) return false;
    if (ispair(i))
    {
        i += 5;
        for (int j = 0; j < 4; ++j) ans[k + j] = p[j];
        k += 4;
        ans[k] = '<';
        k++;
        if (!getPair()) return false;
        ans[k] = ',';
        k++;
        if (!getPair()) return false;
        ans[k] = '>';
        k++;
        return true;
    }
    if (isint(i))
    {
        for (int j = 0; j < 3; ++j) ans[k + j] = Int[j];
        k += 3;
        i += 4;
        n--;
        return true;
    }
    return false;
}
int main()
{
    cin >> n;
    getchar();
    getline(cin, s);
    m = s.length();
    if ((getPair()) && (!n))
    {
        ans[k] = '\n';
        if (k > 400000) cout << "JJK";
        cout << ans;
    }
    else
    {
        cout << "Error occurred" << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstdio>
using namespace std;

string s;
char ans[200000];
char *p = "pair";
char *Int = "int";
int n;
int k = 0;
int i = 0;
int m;

bool isint(int q)
{
    if (m - q < 3) return false;
    bool res = true;
    for (int j = 0; j < 3; ++j)
    {
        if (s[q + j] != Int[j]) res = false;
    }
    return res;
}

bool ispair(int q)
{
    if (m - q < 4) return false;
    bool res = true;
    for (int j = 0; j < 4; ++j)
    {
        if (s[q + j] != p[j]) res = false;
    }
    return res;
}

bool getPair()
{
    if (ispair(i))
    {
        i += 5;
        for (int j = 0; j < 4; ++j) ans[k + j] = p[j];
        k += 4;
        ans[k] = '<';
        k++;
        if (!getPair()) return false;
        ans[k] = ',';
        k++;
        if (!getPair()) return false;
        ans[k] = '>';
        k++;
        return true;
    }
    if (isint(i))
    {
        for (int j = 0; j < 3; ++j) ans[k + j] = Int[j];
        k += 3;
        i += 4;
        n--;
        return true;
    }
    return false;
}
int main()
{
    cin >> n;
    getchar();
    getline(cin, s);
    m = s.length();
    if ((getPair()) && (!n))
    {
        ans[k] = '\n';
        cout << ans;
    }
    else
    {
        cout << "Error occurred" << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstdio>
using namespace std;

string s;
char ans[1000000];
char *p = "pair";
char *Int = "int";
int n;
int k = 0;
int i = 0;
int m;

bool isint(int q)
{
    if (m - q < 3) return false;
    bool res = true;
    for (int j = 0; j < 3; ++j)
    {
        if (s[q + j] != Int[j]) res = false;
    }
    return res;
}

bool ispair(int q)
{
    if (m - q < 4) return false;
    bool res = true;
    for (int j = 0; j < 4; ++j)
    {
        if (s[q + j] != p[j]) res = false;
    }
    return res;
}

bool getPair()
{
    if (i >= m - 1) return false;
    if (ispair(i))
    {
        i += 5;
        for (int j = 0; j < 4; ++j) ans[k + j] = p[j];
        k += 4;
        ans[k] = '<';
        k++;
        if (!getPair()) return false;
        ans[k] = ',';
        k++;
        if (!getPair()) return false;
        ans[k] = '>';
        k++;
        return true;
    }
    if (isint(i))
    {
        for (int j = 0; j < 3; ++j) ans[k + j] = Int[j];
        k += 3;
        i += 4;
        n--;
        return true;
    }
    return false;
}
int main()
{
    cin >> n;
    getchar();
    getline(cin, s);
    m = s.length();
    if ((getPair()) && (!n))
    {
        ans[k] = '\n';
        if (k > 400000) cout << "JJK";
        cout << ans;
    }
    else
    {
        cout << "Error occurred" << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstdio>
using namespace std;

string s;
char ans[500000];
char *p = "pair";
char *Int = "int";
int n;
int k = 0;
int i = 0;
int m;

bool isint(int q)
{
    if (m - q < 3) return false;
    bool res = true;
    for (int j = 0; j < 3; ++j)
    {
        if (s[q + j] != Int[j]) res = false;
    }
    return res;
}

bool ispair(int q)
{
    if (m - q < 4) return false;
    bool res = true;
    for (int j = 0; j < 4; ++j)
    {
        if (s[q + j] != p[j]) res = false;
    }
    return res;
}

bool getPair()
{
    if (i >= m) return false;
    if (ispair(i))
    {
        i += 5;
        for (int j = 0; j < 4; ++j) ans[k + j] = p[j];
        k += 4;
        ans[k] = '<';
        k++;
        if (!getPair()) return false;
        ans[k] = ',';
        k++;
        if (!getPair()) return false;
        ans[k] = '>';
        k++;
        return true;
    }
    if (isint(i))
    {
        for (int j = 0; j < 3; ++j) ans[k + j] = Int[j];
        k += 3;
        i += 4;
        n--;
        return true;
    }
    return false;
}
int main()
{
    cin >> n;
    getchar();
    getline(cin, s);
    m = s.length();
    if ((getPair()) && (!n) && (m + 1 == i))
    {
        ans[k] = '\n';
        cout << ans;
    }
    else
    {
        cout << "Error occurred" << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstdio>
using namespace std;

string s;
char ans[500000];
char *p = "pair";
char *Int = "int";
int n;
int k = 0;
int i = 0;
int m;

bool isint(int q)
{
    if (m - q < 3) return false;
    bool res = true;
    for (int j = 0; j < 3; ++j)
    {
        if (s[q + j] != Int[j]) res = false;
    }
    return res;
}

bool ispair(int q)
{
    if (m - q < 4) return false;
    bool res = true;
    for (int j = 0; j < 4; ++j)
    {
        if (s[q + j] != p[j]) res = false;
    }
    return res;
}

bool getPair()
{
    if (i > m) return false;
    if (ispair(i))
    {
        i += 5;
        for (int j = 0; j < 4; ++j) ans[k + j] = p[j];
        k += 4;
        ans[k] = '<';
        k++;
        if (!getPair()) return false;
        ans[k] = ',';
        k++;
        if (!getPair()) return false;
        ans[k] = '>';
        k++;
        return true;
    }
    if (isint(i))
    {
        for (int j = 0; j < 3; ++j) ans[k + j] = Int[j];
        k += 3;
        i += 4;
        n--;
        return true;
    }
    return false;
}
int main()
{
    cin >> n;
    getchar();
    getline(cin, s);
    m = s.length();
    if ((getPair()) && (!n))
    {
        ans[k] = '\n';
        cout << ans;
    }
    else
    {
        cout << "Error occurred" << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstdio>
using namespace std;

string s;
char ans[400000];
char *p = "pair";
char *Int = "int";
int n;
int k = 0;
int i = 0;
int m;

bool isint(int q)
{
    if (m - q < 3) return false;
    bool res = true;
    for (int j = 0; j < 3; ++j)
    {
        if (s[q + j] != Int[j]) res = false;
    }
    return res;
}

bool ispair(int q)
{
    if (m - q < 4) return false;
    bool res = true;
    for (int j = 0; j < 4; ++j)
    {
        if (s[q + j] != p[j]) res = false;
    }
    return res;
}

bool getPair()
{
    if (i >= m) return false;
    if (ispair(i))
    {
        i += 5;
        for (int j = 0; j < 4; ++j) ans[k + j] = p[j];
        k += 4;
        ans[k] = '<';
        k++;
        if (!getPair()) return false;
        ans[k] = ',';
        k++;
        if (!getPair()) return false;
        ans[k] = '>';
        k++;
        return true;
    }
    if (isint(i))
    {
        for (int j = 0; j < 3; ++j) ans[k + j] = Int[j];
        k += 3;
        i += 4;
        n--;
        return true;
    }
    return false;
}
int main()
{
    cin >> n;
    getchar();
    getline(cin, s);
    m = s.length();
    if ((getPair()) && (!n))
    {
        ans[k] = '\n';
        if (k > 400000) cout << "JJK";
        cout << ans;
    }
    else
    {
        cout << "Error occurred" << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstdio>
using namespace std;

string s;
char ans[400000];
char *p = "pair";
char *Int = "int";
int n;
int k = 0;
int i = 0;
int m;

bool isint(int q)
{
    if (m - q < 3) return false;
    bool res = true;
    for (int j = 0; j < 3; ++j)
    {
        if (s[q + j] != Int[j]) res = false;
    }
    return res;
}

bool ispair(int q)
{
    if (m - q < 4) return false;
    bool res = true;
    for (int j = 0; j < 4; ++j)
    {
        if (s[q + j] != p[j]) res = false;
    }
    return res;
}

bool getPair()
{
    if (ispair(i))
    {
        i += 5;
        for (int j = 0; j < 4; ++j) ans[k + j] = p[j];
        k += 4;
        ans[k] = '<';
        k++;
        if (!getPair()) return false;
        ans[k] = ',';
        k++;
        if (!getPair()) return false;
        ans[k] = '>';
        k++;
        return true;
    }
    if (isint(i))
    {
        for (int j = 0; j < 3; ++j) ans[k + j] = Int[j];
        k += 3;
        i += 4;
        n--;
        return true;
    }
    return false;
}
int main()
{
    cin >> n;
    getchar();
    getline(cin, s);
    m = s.length();
    if ((getPair()) && (!n))
    {
        ans[k] = '\n';
        cout << ans;
    }
    else
    {
        cout << "Error occurred" << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstdio>
using namespace std;

string s;
char ans[200000];
char *p = "pair";
char *in = "int";
int n;
int k = 0;
int i = 0;
int m;

bool isint(int i)
{
    if (m - i < 3) return false;
    bool res = true;
    for (int j = i; j < i + 3; ++j)
    {
        if (s[j] != in[j - i]) res = false;
    }
    return res;
}

bool ispair(int i)
{
    if (m - i < 4) return false;
    bool res = true;
    for (int j = i; j < i + 4; ++j)
    {
        if (s[j] != p[j - i]) res = false;
    }
    return res;
}

bool getPair()
{
    if (ispair(i))
    {
        i += 5;
        for (int j = 0; j < 4; j++) ans[k + j] = p[j];
        k += 4;
        ans[k] = '<';
        k++;
        if (!getPair()) return false;
        ans[k] = ',';
        k++;
        if (!getPair()) return false;
        ans[k] = '>';
        k++;
        return true;
    }
    if (isint(i))
    {
        for (int j = 0; j < 3; j++) ans[k + j] = in[j];
        k += 3;
        i += 4;
        n--;
        return true;
    }
    return false;
}
int main()
{
    cin >> n;
    getchar();
    getline(cin, s);
    m = s.length();
    if ((getPair()) && (!n))
    {
        ans[k] = '\n';
        cout << ans;
    }
    else
    {
        cout << "Error occurred" << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstdio>
using namespace std;

string s;
char ans[400000];
char *p = "pair";
char *Int = "int";
int n;
int k = 0;
int i = 0;
int m;

bool isint(int q)
{
    if (m - q < 3) return false;
    bool res = true;
    for (int j = 0; j < 3; ++j)
    {
        if (s[q + j] != Int[j]) res = false;
    }
    return res;
}

bool ispair(int q)
{
    if (m - q < 4) return false;
    bool res = true;
    for (int j = 0; j < 4; ++j)
    {
        if (s[q + j] != p[j]) res = false;
    }
    return res;
}

bool getPair()
{
    if (i >= m - 2) return false;
    if (ispair(i))
    {
        i += 5;
        for (int j = 0; j < 4; ++j) ans[k + j] = p[j];
        k += 4;
        ans[k] = '<';
        k++;
        if (!getPair()) return false;
        ans[k] = ',';
        k++;
        if (!getPair()) return false;
        ans[k] = '>';
        k++;
        return true;
    }
    if (isint(i))
    {
        for (int j = 0; j < 3; ++j) ans[k + j] = Int[j];
        k += 3;
        i += 4;
        n--;
        return true;
    }
    return false;
}
int main()
{
    cin >> n;
    getchar();
    getline(cin, s);
    m = s.length();
    if ((getPair()) && (!n))
    {
        ans[k] = '\n';
        if (k > 400000) cout << "JJK";
        cout << ans;
    }
    else
    {
        cout << "Error occurred" << endl;
    }
    return 0;
}


#include <stack>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

typedef long long ll;
const int Maxn = 10;

int N, M;
vector<int> G[Maxn + 5];

int dfn[Maxn + 5], low[Maxn + 5], dcnt;
int col[Maxn + 5], ccnt;
bool vis[Maxn + 5];
int stk[Maxn + 5], tp;

void Tarjan_scc(int u) {
	dfn[u] = ++dcnt, low[u] = dcnt, vis[u] = true;
	stk[++tp] = u;
	for(int i = 0; i < (int)G[u].size(); i++) {
		int v = G[u][i];
		if(!dfn[v]) {
			Tarjan_scc(v);
			low[u] = min(low[u], low[v]);
		} else if(vis[v]) low[u] = min(low[u], low[v]);
	}
	if(dfn[u] == low[u]) {
		col[u] = ++ccnt, vis[u] = false;
		while(stk[tp] != u) {
			col[stk[tp]] = ccnt;
			vis[stk[tp--]] = false;
		}
		tp--;
	}
}

int main() {
#ifdef LOACL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	int T;
	scanf("%d", &T);
	while(T--) {
		scanf("%d %d", &N, &M);
		for(int i = 1; i <= M; i++) {
			int u, v;
			scanf("%d %d", &u, &v);
			if(u == v) continue;
			G[u].push_back(v);
		}
		ccnt = 0;
		for(int i = 1; i <= N; i++)
			dfn[i] = 0;
		for(int i = 1; i <= N; i++)
			if(!dfn[i]) Tarjan_scc(i);
		if(ccnt == 1) puts("No");
		else {
			puts("Yes");
			int cnt_j = 0, cnt_p = 0;
			for(int i = 1; i <= N; i++)
				if(col[i] == 1) cnt_j++;
				else cnt_p++;
			printf("%d %d\n", cnt_j, cnt_p);
			for(int i = 1; i <= N; i++)
				if(col[i] == 1) printf("%d ", i);
			puts("");
			for(int i = 1; i <= N; i++)
				if(col[i] > 1) printf("%d ", i);
			puts("");
		}
	}
	return 0;
}


#include <stack>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

typedef long long ll;
const int Maxn = 1e6;

int N, M;
vector<int> G[Maxn + 5];

int dfn[Maxn + 5], low[Maxn + 5], dcnt;
int col[Maxn + 5], ccnt;
bool vis[Maxn + 5];
int stk[Maxn + 5], tp;

void Tarjan_scc(int u) {
	dfn[u] = ++dcnt, low[u] = dcnt, vis[u] = true;
	stk[++tp] = u;
	for(int i = 0; i < (int)G[u].size(); i++) {
		int v = G[u][i];
		if(!dfn[v]) {
			Tarjan_scc(v);
			low[u] = min(low[u], low[v]);
		} else if(vis[v]) low[u] = min(low[u], low[v]);
	}
	if(dfn[u] == low[u]) {
		++ccnt;
		while(true) {
			col[stk[tp]] = ccnt;
			vis[stk[tp]] = false;
			if(stk[tp--] == u)
				break;
		}
	}
}

int main() {
#ifdef LOACL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	int T;
	scanf("%d", &T);
	while(T--) {
		scanf("%d %d", &N, &M);
		ccnt = 0;
		for(int i = 1; i <= N; i++)
			dfn[i] = 0, G[i].clear();
		for(int i = 1; i <= M; i++) {
			int u, v;
			scanf("%d %d", &u, &v);
			if(u == v) continue;
			G[u].push_back(v);
		}
		for(int i = 1; i <= N; i++)
			if(!dfn[i]) Tarjan_scc(i);
		if(ccnt == 1) puts("No");
		else {
			puts("Yes");
			int cnt_j = 0, cnt_p = 0;
			for(int i = 1; i <= N; i++)
				if(col[i] == 1) cnt_j++;
				else cnt_p++;
			printf("%d %d\n", cnt_j, cnt_p);
			for(int i = 1; i <= N; i++)
				if(col[i] == 1) printf("%d ", i);
			puts("");
			for(int i = 1; i <= N; i++)
				if(col[i] > 1) printf("%d ", i);
			puts("");
		}
	}
	return 0;
}


#include <stack>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

typedef long long ll;
const int Maxn = 10;

int N, M;
vector<int> G[Maxn + 5];

int dfn[Maxn + 5], low[Maxn + 5], dcnt;
int col[Maxn + 5], ccnt;
bool vis[Maxn + 5];
int stk[Maxn + 5], tp;

void Tarjan_scc(int u) {
	dfn[u] = ++dcnt, low[u] = dcnt, vis[u] = true;
	stk[++tp] = u;
	for(int i = 0; i < (int)G[u].size(); i++) {
		int v = G[u][i];
		if(!dfn[v]) {
			Tarjan_scc(v);
			low[u] = min(low[u], low[v]);
		} else if(vis[v]) low[u] = min(low[u], low[v]);
	}
	if(dfn[u] == low[u]) {
		col[u] = ++ccnt, vis[u] = false;
		while(stk[tp] != u) {
			col[stk[tp]] = ccnt;
			vis[stk[tp--]] = false;
		}
		tp--;
	}
}

int main() {
#ifdef LOACL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	int T;
	scanf("%d", &T);
	while(T--) {
		scanf("%d %d", &N, &M);
		for(int i = 1; i <= M; i++) {
			int u, v;
			scanf("%d %d", &u, &v);
			if(u == v) continue;
			G[u].push_back(v);
		}
		ccnt = 0;
		for(int i = 1; i <= N; i++)
			dfn[i] = 0, G[i].clear();
		for(int i = 1; i <= N; i++)
			if(!dfn[i]) Tarjan_scc(i);
		if(ccnt == 1) puts("No");
		else {
			puts("Yes");
			int cnt_j = 0, cnt_p = 0;
			for(int i = 1; i <= N; i++)
				if(col[i] == 1) cnt_j++;
				else cnt_p++;
			printf("%d %d\n", cnt_j, cnt_p);
			for(int i = 1; i <= N; i++)
				if(col[i] == 1) printf("%d ", i);
			puts("");
			for(int i = 1; i <= N; i++)
				if(col[i] > 1) printf("%d ", i);
			puts("");
		}
	}
	return 0;
}


#include <stack>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

typedef long long ll;
const int Maxn = 10;

int N, M;
vector<int> G[Maxn + 5];

int dfn[Maxn + 5], low[Maxn + 5], dcnt;
int col[Maxn + 5], ccnt;
bool vis[Maxn + 5];
int stk[Maxn + 5], tp;

void Tarjan_scc(int u) {
	dfn[u] = ++dcnt, low[u] = dcnt, vis[u] = true;
	stk[++tp] = u;
	for(int i = 0; i < (int)G[u].size(); i++) {
		int v = G[u][i];
		if(!dfn[v]) {
			Tarjan_scc(v);
			low[u] = min(low[u], low[v]);
		} else if(vis[v]) low[u] = min(low[u], low[v]);
	}
	if(dfn[u] == low[u]) {
		++ccnt;
		while(true) {
			col[stk[tp]] = ccnt;
			vis[stk[tp]] = false;
			if(stk[tp--] == u)
				break;
		}
	}
}

int main() {
#ifdef LOACL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	int T;
	scanf("%d", &T);
	while(T--) {
		scanf("%d %d", &N, &M);
		ccnt = 0;
		for(int i = 1; i <= N; i++)
			dfn[i] = 0, G[i].clear();
		for(int i = 1; i <= M; i++) {
			int u, v;
			scanf("%d %d", &u, &v);
			if(u == v) continue;
			G[u].push_back(v);
		}
		for(int i = 1; i <= N; i++)
			if(!dfn[i]) Tarjan_scc(i);
		if(ccnt == 1) puts("No");
		else {
			puts("Yes");
			int cnt_j = 0, cnt_p = 0;
			for(int i = 1; i <= N; i++)
				if(col[i] == 1) cnt_j++;
				else cnt_p++;
			printf("%d %d\n", cnt_j, cnt_p);
			for(int i = 1; i <= N; i++)
				if(col[i] == 1) printf("%d ", i);
			puts("");
			for(int i = 1; i <= N; i++)
				if(col[i] > 1) printf("%d ", i);
			puts("");
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e6+7;
vector<int>ve[maxn*2],ans1,ans2;
vector<int>ve2[maxn*2];
int tim[maxn*2],pick[maxn*2],boss[maxn*2],tot,low[maxn*2],sk[maxn*2];
int top,vis[maxn*2],sz,n,m,deg[maxn*2];
int tarjin(int x){
    tim[x]=low[x]=++tot;
    sk[++top]=x;
    vis[x]=1;
    for(int i=0;i<ve[x].size();i++){
        int v=ve[x][i];
        if(tim[v]==0){
            tarjin(v);
            low[x]=min(low[x],low[v]);
        }else if(vis[v])
            low[x]=min(low[x],low[v]);
    }
    if(low[x]==tim[x]){
        sz++;
        int v;
        while(1){
            v=sk[top--];
            vis[v]=0;
            boss[v]=x;
            if(v==x)break;
        }
    }
}
void dfs(int x){
    if(pick[x]!=0)return ;
    pick[x]=-1;
    for(int i=0;i<ve2[x].size();i++)
        dfs(ve2[x][i]);
}
void tp(){
    queue<int>que;
    for(int i=1;i<=2*n;i++){
        if(boss[i]==i&&deg[i]==0){
            que.push(i);
            for(int j=2*n;j>i;j--)
                if(boss[j]==j&&deg[j]==0)
                    que.push(j);
            break;
        }
    }

    while(que.size()){
        int v=que.front();
        if(pick[v]==0){
            pick[v]=1;
            int w=v;
            if(w>n)w-=n;
            else w+=n;
            dfs(boss[w]);
        }
        que.pop();
        for(int i=0;i<ve2[v].size();i++){
            deg[ve2[v][i]]--;
            if(deg[ve2[v][i]]==0)que.push(ve2[v][i]);
        }
    }
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d %d",&n,&m);
        for(int i=1;i<=m;i++){
            int a,b;
            scanf("%d %d",&a,&b);
            if(a==b)continue;
            ve[b].push_back(a);//a_>b
            ve[a+n].push_back(b+n);
        }
        for(int i=1;i<=2*n;i++)if(tim[i]==0)tarjin(i);
        if(sz==2){
            printf("No\n");
        }else{
            //cout<<sz<<endl;
            //cout<<boss[1]<<" "<<boss[3]<<endl;
            for(int i=1;i<=n*2;i++){
                for(int j=0;j<ve[i].size();j++){
                    int a=i,b=ve[i][j];
                    if(boss[a]==boss[b])continue;
                    ve2[a].push_back(b);
                    deg[b]++;
                }
            }
            tp();
            for(int i=1;i<=n;i++)
                if(pick[boss[i]]==1)ans1.push_back(i);
                else ans2.push_back(i);
            printf("Yes\n");
            printf("%d %d\n",ans1.size(),ans2.size());
            for(int i=0;i<ans1.size();i++)
                printf("%d%c",ans1[i]," \n"[i==ans1.size()-1]);
            for(int i=0;i<ans2.size();i++)
                printf("%d%c",ans2[i]," \n"[i==ans2.size()-1]);
        }
        tot=sz=0;
        ans1.clear();
        ans2.clear();
        for(int i=1;i<=2*n;i++)
            tim[i]=pick[i]=deg[i]=0,ve[i].clear(),ve2[i].clear();
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e6+7;
vector<int>ve[maxn*2],ans1,ans2;
vector<int>ve2[maxn*2];
int tim[maxn*2],pick[maxn*2],boss[maxn*2],tot,low[maxn*2],sk[maxn*2];
int top,vis[maxn*2],sz,n,m,deg[maxn*2];
int tarjin(int x){
    tim[x]=low[x]=++tot;
    sk[++top]=x;
    vis[x]=1;
    for(int i=0;i<ve[x].size();i++){
        int v=ve[x][i];
        if(tim[v]==0){
            tarjin(v);
            low[x]=min(low[x],low[v]);
        }else if(vis[v])
            low[x]=min(low[x],low[v]);
    }
    if(low[x]==tim[x]){
        sz++;
        int v;
        while(1){
            v=sk[top--];
            vis[v]=0;
            boss[v]=x;
            if(v==x)break;
        }
    }
}
void dfs(int x){
    if(pick[x]!=0)return ;
    pick[x]=-1;
    for(int i=0;i<ve2[x].size();i++)
        dfs(ve2[x][i]);
}
void tp(){
    queue<int>que;
    for(int i=1;i<=2*n;i++)
        if(boss[i]==i&&deg[i]==0)
            que.push(i);
    while(que.size()){
        int v=que.front();
        if(pick[v]==0){
            pick[v]=1;
            int w=v;
            if(w>n)w-=n;
            else w+=n;
            dfs(boss[w]);
        }
        que.pop();
        for(int i=0;i<ve2[v].size();i++){
            deg[ve2[v][i]]--;
            if(deg[ve2[v][i]]==0)que.push(ve2[v][i]);
        }
    }
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d %d",&n,&m);
        for(int i=1;i<=m;i++){
            int a,b;
            scanf("%d %d",&a,&b);
            if(a==b)continue;
            ve[b].push_back(a);//a_>b
            ve[a+n].push_back(b+n);
        }
        for(int i=1;i<=2*n;i++)if(tim[i]==0)tarjin(i);
        if(sz==2){
            printf("No\n");
        }else{
            //cout<<sz<<endl;
            //cout<<boss[1]<<" "<<boss[3]<<endl;
            for(int i=1;i<=n*2;i++){
                for(int j=0;j<ve[i].size();j++){
                    int a=i,b=ve[i][j];
                    if(boss[a]==boss[b])continue;
                    ve2[a].push_back(b);
                    deg[b]++;
                }
            }
            tp();
            for(int i=1;i<=n;i++)
                if(pick[boss[i]]==1)ans1.push_back(i);
                else ans2.push_back(i);
            printf("Yes\n");
            printf("%d %d\n",ans1.size(),ans2.size());
            for(int i=0;i<ans1.size();i++)
                printf("%d%c",ans1[i]," \n"[i==ans1.size()-1]);
            for(int i=0;i<ans2.size();i++)
                printf("%d%c",ans2[i]," \n"[i==ans2.size()-1]);
        }
        tot=sz=0;
        ans1.clear();
        ans2.clear();
        for(int i=1;i<=2*n;i++)
            tim[i]=pick[i]=deg[i]=0,ve[i].clear(),ve2[i].clear();
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e6+7;
vector<int>ve[maxn*2],ans1,ans2;
vector<int>ve2[maxn*2];
int tim[maxn*2],pick[maxn*2],boss[maxn*2],tot,low[maxn*2],sk[maxn*2];
int top,vis[maxn*2],sz,n,m,deg[maxn*2];
void tarjin(int x){
    tim[x]=low[x]=++tot;
    sk[++top]=x;
    vis[x]=1;
    for(int i=0;i<ve[x].size();i++){
        int v=ve[x][i];
        if(tim[v]==0){
            tarjin(v);
            low[x]=min(low[x],low[v]);
        }else if(vis[v])
            low[x]=min(low[x],low[v]);
    }
    if(low[x]==tim[x]){
        sz++;
        int v;
        while(1){
            v=sk[top--];
            vis[v]=0;
            boss[v]=x;
            if(v==x)break;
        }
    }
}
void dfs(int x){
    if(pick[x]!=0)return ;
    pick[x]=-1;
    for(int i=0;i<ve2[x].size();i++)
        dfs(ve2[x][i]);
}
void tp(){
    queue<int>que;
    for(int i=1;i<=2*n;i++){
        if(boss[i]==i&&deg[i]==0){
            que.push(i);
            for(int j=2*n;j>i;j--)
                if(boss[j]==j&&deg[j]==0)
                    que.push(j);
            break;
        }
    }

    while(que.size()){
        int v=que.front();
        if(pick[v]==0){
            pick[v]=1;
            int w=v;
            if(w>n)w-=n;
            else w+=n;
            dfs(boss[w]);
        }
        que.pop();
        for(int i=0;i<ve2[v].size();i++){
            deg[ve2[v][i]]--;
            if(deg[ve2[v][i]]==0)que.push(ve2[v][i]);
        }
    }
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d %d",&n,&m);
        for(int i=1;i<=m;i++){
            int a,b;
            scanf("%d %d",&a,&b);
            if(a==b)continue;
            ve[b].push_back(a);//a_>b
            ve[a+n].push_back(b+n);
        }
        for(int i=1;i<=2*n;i++)if(tim[i]==0)tarjin(i);
        if(sz==2){
            printf("No\n");
        }else{
            //cout<<sz<<endl;
            //cout<<boss[1]<<" "<<boss[3]<<endl;
            for(int i=1;i<=n*2;i++){
                for(int j=0;j<ve[i].size();j++){
                    int a=i,b=ve[i][j];
                    if(boss[a]==boss[b])continue;
                    ve2[a].push_back(b);
                    deg[b]++;
                }
            }
            tp();
            for(int i=1;i<=n;i++)
                if(pick[boss[i]]==1)ans1.push_back(i);
                else if(pick[boss[i]]==-1)ans2.push_back(i);
            printf("Yes\n");
            printf("%d %d\n",ans1.size(),ans2.size());
            for(int i=0;i<ans1.size();i++)
                printf("%d%c",ans1[i]," \n"[i==ans1.size()-1]);
            for(int i=0;i<ans2.size();i++)
                printf("%d%c",ans2[i]," \n"[i==ans2.size()-1]);
        }
        tot=sz=0;
        ans1.clear();
        ans2.clear();
        for(int i=1;i<=2*n;i++)
            tim[i]=pick[i]=deg[i]=0,ve[i].clear(),ve2[i].clear();
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e6+7;
vector<int>ve[maxn*2],ans1,ans2;
vector<int>ve2[maxn*2];
int tim[maxn*2],pick[maxn*2],boss[maxn*2],tot,low[maxn*2],sk[maxn*2];
int top,vis[maxn*2],sz,n,m,deg[maxn*2];
int tarjin(int x){
    tim[x]=low[x]=++tot;
    sk[++top]=x;
    vis[x]=1;
    for(int i=0;i<ve[x].size();i++){
        int v=ve[x][i];
        if(tim[v]==0){
            tarjin(v);
            low[x]=min(low[x],low[v]);
        }else if(vis[v])
            low[x]=min(low[x],low[v]);
    }
    if(low[x]==tim[x]){
        sz++;
        int v;
        while(1){
            v=sk[top--];
            vis[v]=0;
            boss[v]=x;
            if(v==x)break;
        }
    }
}
void dfs(int x){
    if(pick[x]!=0)return ;
    pick[x]=-1;
    for(int i=0;i<ve2[x].size();i++)
        dfs(ve2[x][i]);
}
void tp(){
    queue<int>que;
    for(int i=1;i<=2*n;i++){
        if(boss[i]==i&&deg[i]==0){
            que.push(i);
            for(int j=2*n;j>i;j--)
                if(boss[i]==i&&deg[i]==0)
                    que.push(i);
            break;
        }
    }

    while(que.size()){
        int v=que.front();
        if(pick[v]==0){
            pick[v]=1;
            int w=v;
            if(w>n)w-=n;
            else w+=n;
            dfs(boss[w]);
        }
        que.pop();
        for(int i=0;i<ve2[v].size();i++){
            deg[ve2[v][i]]--;
            if(deg[ve2[v][i]]==0)que.push(ve2[v][i]);
        }
    }
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d %d",&n,&m);
        for(int i=1;i<=m;i++){
            int a,b;
            scanf("%d %d",&a,&b);
            if(a==b)continue;
            ve[b].push_back(a);//a_>b
            ve[a+n].push_back(b+n);
        }
        for(int i=1;i<=2*n;i++)if(tim[i]==0)tarjin(i);
        if(sz==2){
            printf("No\n");
        }else{
            //cout<<sz<<endl;
            //cout<<boss[1]<<" "<<boss[3]<<endl;
            for(int i=1;i<=n*2;i++){
                for(int j=0;j<ve[i].size();j++){
                    int a=i,b=ve[i][j];
                    if(boss[a]==boss[b])continue;
                    ve2[a].push_back(b);
                    deg[b]++;
                }
            }
            tp();
            for(int i=1;i<=n;i++)
                if(pick[boss[i]]==1)ans1.push_back(i);
                else ans2.push_back(i);
            printf("Yes\n");
            printf("%d %d\n",ans1.size(),ans2.size());
            for(int i=0;i<ans1.size();i++)
                printf("%d%c",ans1[i]," \n"[i==ans1.size()-1]);
            for(int i=0;i<ans2.size();i++)
                printf("%d%c",ans2[i]," \n"[i==ans2.size()-1]);
        }
        tot=sz=0;
        ans1.clear();
        ans2.clear();
        for(int i=1;i<=2*n;i++)
            tim[i]=pick[i]=deg[i]=0,ve[i].clear(),ve2[i].clear();
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e6+7;
vector<int>ve[maxn*2],ans1,ans2;
vector<int>ve2[maxn*2];
int tim[maxn*2],pick[maxn*2],boss[maxn*2],tot,low[maxn*2],sk[maxn*2];
int top,vis[maxn*2],sz,n,m,deg[maxn*2];
void tarjin(int x){
    tim[x]=low[x]=++tot;
    sk[++top]=x;
    vis[x]=1;
    for(int i=0;i<ve[x].size();i++){
        int v=ve[x][i];
        if(tim[v]==0){
            tarjin(v);
            low[x]=min(low[x],low[v]);
        }else if(vis[v])
            low[x]=min(low[x],low[v]);
    }
    if(low[x]==tim[x]){
        sz++;
        int v;
        while(1){
            v=sk[top--];
            vis[v]=0;
            boss[v]=x;
            if(v==x)break;
        }
    }
}
void dfs(int x){
    if(pick[x]!=0)return ;
    pick[x]=-1;
    for(int i=0;i<ve2[x].size();i++)
        dfs(ve2[x][i]);
}
void tp(){
    queue<int>que;
    for(int i=1;i<=2*n;i++){
        if(boss[i]==i&&deg[i]==0){
            que.push(i);
            for(int j=2*n;j>i;j--)
                if(boss[j]==j&&deg[j]==0)
                    que.push(j);
            break;
        }
    }

    while(que.size()){
        int v=que.front();
        if(pick[v]==0){
            pick[v]=1;
            int w=v;
            if(w>n)w-=n;
            else w+=n;
            dfs(boss[w]);
        }
        que.pop();
        for(int i=0;i<ve2[v].size();i++){
            deg[ve2[v][i]]--;
            if(deg[ve2[v][i]]==0)que.push(ve2[v][i]);
        }
    }
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d %d",&n,&m);
        for(int i=1;i<=m;i++){
            int a,b;
            scanf("%d %d",&a,&b);
            if(a==b)continue;
            ve[b].push_back(a);//a_>b
            ve[a+n].push_back(b+n);
        }
        for(int i=1;i<=2*n;i++)if(tim[i]==0)tarjin(i);
        if(sz==2){
            printf("No\n");
        }else{
            //cout<<sz<<endl;
            //cout<<boss[1]<<" "<<boss[3]<<endl;
            for(int i=1;i<=n*2;i++){
                for(int j=0;j<ve[i].size();j++){
                    int a=boss[i],b=boss[ve[i][j]];
                    if(a==b)continue;
                    ve2[a].push_back(b);
                    deg[b]++;
                }
            }
            tp();
            for(int i=1;i<=n;i++)
                if(pick[boss[i]]==1)ans1.push_back(i);
                else if(pick[boss[i]]==-1)ans2.push_back(i);
            printf("Yes\n");
            printf("%d %d\n",ans1.size(),ans2.size());
            for(int i=0;i<ans1.size();i++)
                printf("%d%c",ans1[i]," \n"[i==ans1.size()-1]);
            for(int i=0;i<ans2.size();i++)
                printf("%d%c",ans2[i]," \n"[i==ans2.size()-1]);
        }
        tot=sz=0;
        ans1.clear();
        ans2.clear();
        for(int i=1;i<=2*n;i++)
            tim[i]=pick[i]=deg[i]=0,ve[i].clear(),ve2[i].clear();
    }
    return 0;
}


#include<bits/stdc++.h>
#define rep(X,A,B) for(int X=A;X<=B;X++)
#define tep(X,A,B) for(int X=A;X>=B;X--)
#define LL long long
const int N=1000010;
const int M=2000010;
const int maxn=60;
using namespace std;

int n,m;
int vis[N];

struct Graph{
	int edge[N],lst[N],nxt[N],t;

	void ADD(int x,int y){
		edge[++t]=y;nxt[t]=lst[x];lst[x]=t;
	}

	void MEM(){
		rep(i,1,n)lst[i]=0;t=0;
	}

	int SEARCH(int x){
		vis[x]=1;
		int res=0;
		for(int r=lst[x];r;r=nxt[r]){
			if(!vis[edge[r]])res+=SEARCH(edge[r]);
		}
		return res+1;
	}

	int CHK(int pd){
		rep(i,1,n)vis[i]=0;
		int now=SEARCH(1);
		if(now==n)return 0;
		printf("Yes\n");
		if(pd==0){
			printf("%d %d\n",now,n-now);
			rep(i,1,n)if(vis[i])printf("%d ",i);
			puts("");
			rep(i,1,n)if(!vis[i])printf("%d ",i);
		}
		else{
			printf("%d %d\n",n-now,now);
			rep(i,1,n)if(!vis[i])printf("%d ",i);
			puts("");
			rep(i,1,n)if(vis[i])printf("%d ",i);
		}
		puts("");
		return 1;
	}
}gr[2];

void SOLVE(){
	int u,v;
	scanf("%d%d",&n,&m);
	rep(i,0,1)gr[i].MEM();
	rep(i,1,m){
		scanf("%d%d",&u,&v);
		if(u==v)continue;
		gr[0].ADD(u,v);
		gr[1].ADD(v,u);
	}
	rep(i,0,1){
		if(gr[i].CHK(i))return;
	}
	printf("No\n");
}

int main(){
	int T;
	scanf("%d",&T);
	while(T--)SOLVE();
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
using namespace std;
const int N = 1e6+5;

int t, n, m, u, v, dfn[N], low[N], scc[N], times, sccno;
int S[N], top, vis[N];
vector<int> to[N];

void tarjan(int u) {
	dfn[u] = low[u] = ++times;
	S[++top] = u; vis[u] = 1;
	for (int sz = to[u].size(), i = 0, v; i < sz; i++) {
		v = to[u][i];
		if (vis[v]) low[u] = min(low[u], dfn[v]);
		else if (!dfn[v]) tarjan(v), low[u] = min(low[u], low[v]);
	}
	if (dfn[u] == low[u]) {
		scc[u] = ++sccno;
		while (S[top] != u)	{
			vis[S[top]] = 0;
			scc[S[top--]] = sccno;
		}
		vis[S[top--]] = 0;
	}
}
int main() {
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d", &n, &m);
		for (int i = 1; i <= n; i++)
			to[i].clear();
		for (int i = 1; i <= m; i++)
			scanf("%d%d", &u, &v),
			to[u].pb(v);
		for (int i = 1; i <= n; i++)
			dfn[i] = low[i] = scc[i] = 0;
		times = sccno = 0;
		for (int i = 1; i <= n; i++)
			if (!dfn[i]) tarjan(i);
		if (sccno == 1) puts("No");
		else {
			int cnt = 0;
			puts("Yes");
			for (int i = 1; i <= n; i++)
				if (scc[i] == 1) ++cnt;
			printf("%d %d\n", cnt, n-cnt);
			for (int i = 1; i <= n; i++)
				if (scc[i] == 1) printf("%d ", i);
			puts("");
			for (int i = 1; i <= n; i++)
				if (scc[i] != 1) printf("%d ", i);
			puts("");
		}
	}
	return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
inline int read(){
	int f=1,ans=0;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();}
	return f*ans;
}
const int MAXN=1000001;
int n,m,T,col[MAXN],sta[MAXN],num,dfn[MAXN],low[MAXN],Ans0[MAXN],Ans1[MAXN];
vector<int> vec[MAXN];
void Init(){
	num=0;Ans0[0]=0,Ans1[0]=0;
	for(int i=0;i<=n;i++) vec[i].clear();
	for(int i=0;i<=n;i++) col[i]=dfn[i]=low[i]=0;
}
void tarjan(int u){
	dfn[u]=low[u]=++num;sta[++sta[0]]=u;
	int siz=vec[u].size();
	for(int i=0;i<siz;i++){
		int v=vec[u][i];
		if(!dfn[v]){
			tarjan(v);
			low[u]=min(low[u],low[v]);
		}else if(!col[v]) low[u]=min(low[u],low[v]);
	}
	if(dfn[u]==low[u]){
		col[u]=++col[0];
		while(sta[sta[0]]!=u){
			col[sta[sta[0]]]=col[0];
			sta[0]--;
		}sta[0]--;
	}return;
}
int main(){
//	freopen("8.in","r",stdin);
	T=read();
	while(T--){
		n=read(),m=read();
		Init();
		for(int i=1;i<=m;i++){int u=read(),v=read();vec[u].push_back(v);}
		for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i);
		if(col[0]==1){printf("No\n");continue;}
		for(int i=1;i<=n;i++){
			if(col[i]==1) Ans0[++Ans0[0]]=i;
			else Ans1[++Ans1[0]]=i;
		}
		printf("Yes\n%d %d\n",Ans0[0],Ans1[0]);
		for(int i=1;i<=Ans0[0];i++) printf("%d ",Ans0[i]);printf("\n");
		for(int i=1;i<=Ans1[0];i++) printf("%d ",Ans1[i]);printf("\n");
	}return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 7;
int t, n, m;
vector<int> E[2][maxn];
int vis[2][maxn], cnt = 0;
void dfs(int x, int z){
    vis[z][x] = 1;
    cnt++;
    for (int i = 0; i < (int)E[z][x].size(); ++i){
        int v = E[z][x][i];
        if (vis[z][v]) continue;
        dfs(v, z);
    }
}
void clear(){
    for (int i = 1; i <= n; ++i){
        vis[0][i] = vis[1][i] = 0;
        E[0][i].clear();
        E[1][i].clear();
    }
}
void solve(){
    scanf("%d%d", &n, &m);
    clear();
    for (int i = 0; i < m; ++i){
        int x, y;
        scanf("%d%d", &x, &y);
        E[0][x].push_back(y);
        E[1][y].push_back(x);
    }

    for (int z = 0; z < 2; ++z){
        cnt = 0;
        dfs(1, z);
        if (cnt < n){
            puts("Yes");
            if (z == 0){
                cout << cnt << ' ' << n-cnt << endl;
            }else{
                cout << n-cnt << ' ' << cnt << endl;
            }
            for (int i = 1; i <= n; ++i){
                if (vis[z][i] == 1-z) cout << i << ' ';
            }
            cout << endl;
            for (int i = 1; i <= n; ++i){
                if (vis[z][i] == z) cout << i << ' ';
            }
            cout << endl;
            return;
        }
    }
    puts("No");
}
int main(void){
    scanf("%d", &t);
    while (t--) solve();
    return 0;
}


#include <bits/stdc++.h>
#define debug(a) cout << #a ": " << a << endl
using namespace std;

const int N = 1e6 + 10;

vector<int> e1[N], e2[N];

int dfn[N], low[N], deep;
stack<int> st;
int vis[N], ans, clor[N];

void targin(int u, vector<int> e[])
{
    dfn[u] = low[u] = ++deep;
    vis[u] = 1;
    st.push(u);
    for (int i = 0; i < (int)e[u].size(); ++i){
        int v = e[u][i];
        if (!dfn[v]){
            targin(v, e);
            low[u] = min(low[u], low[v]);
        }else if (vis[v]){
            low[u] = min(low[u], dfn[v]);
        }
    }
    if (dfn[u] == low[u]){
        while (!st.empty()){
            int tp = st.top(); st.pop();
            ans++;
            clor[tp] = 1;
            vis[tp] = 0;
            if (tp == u) break;
        }
    }
}

void init(int n){
    for (int i = 1; i <= n; ++i){
        dfn[i] = low[i] = clor[i] = 0;
    }
    deep = 0, ans = 0;
}

int main(void)
{
    ios::sync_with_stdio(false), cin.tie(0);
    int T; cin >> T;
    while (T--){
        int n, m; cin >> n >> m;
        for (int i = 1; i <= n; ++i) e1[i].clear(), e2[i].clear();
        for (int i = 0; i < m; ++i){
            int u, v; cin >> u >> v;
            if (u == v) continue;
            e1[u].push_back(v);
            e2[v].push_back(u);
        }
        //debug(n), debug(m);
//        for (int i= 1; i <= n; ++i){
//            for (int j = 0; j < (int)e1[i].size(); ++i){
//                cout << e1[i][j] << ' ';
//            }
//            cout << endl;
//        }
        init(n);
        targin(1, e1);
        int cnt = ans;
        if (cnt < n){
            cout << "Yes" << endl;
            cout << cnt << ' ' << n-cnt << endl;
            for (int i = 1; i <= n; ++i){
                if (clor[i] == 1) cout << i << ' ';
            }
            cout << endl;
            for (int i = 1; i <= n; ++i){
                if (clor[i] != 1) cout << i << ' ';
            }
            cout << endl;
        }else{
            init(n);
            targin(1, e2);
            cnt = ans;
            if (cnt < n){
                cout << "Yes" << endl;
                cout << n-cnt << ' ' << cnt << endl;
                for (int i = 1; i <= n; ++i){
                    if (clor[i] != 1) cout << i << ' ';
                }
                cout << endl;
                for (int i = 1; i <= n; ++i){
                    if (clor[i] == 1) cout << i << ' ';
                }
                cout << endl;
            }else{
                cout << "No" << endl;
            }
        }
    }
    return 0;
}


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

const int maxn = 2e6 + 10;
int n, m;

vector <int> e[maxn];

void chkmin(int& x, int y) {
  if (x > y) x = y;
}

bool vis[maxn];
int now, tot, top, st[maxn], sz[maxn], bl[maxn], dfn[maxn], low[maxn], val[maxn];

void tarjan(int u) {
  dfn[u] = low[u] = ++now;
  st[++top] = u, vis[u] = 1;
  for (int v : e[u]) {
    if (!dfn[v]) {
      tarjan(v);
      chkmin(low[u], low[v]);
    } else if (vis[v]) {
      chkmin(low[u], low[v]);
    }
  }
  if (dfn[u] == low[u]) {
    for (tot++; st[top + 1] != u; top--) {
      sz[tot]++;
      vis[st[top]] = 0;
      bl[st[top]] = tot;
    }
  }
}

vector <int> ans[2];

void solve() {
  now = tot = top = 0;
  memset(bl, 0, (n + 1) << 2);
  memset(sz, 0, (n + 1) << 2);
  memset(st, 0, (n + 1) << 2);
  memset(dfn, 0, (n + 1) << 2);
  memset(low, 0, (n + 1) << 2);
  memset(vis, 0, (n + 1) << 2);
  memset(val, 0, (n + 1) << 2);
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    e[i].clear();
  }
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    if (u != v) {
      e[u].push_back(v);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!dfn[i]) tarjan(i);
  }
  if (sz[bl[1]] == n) {
    puts("No"); return;
  }
  for (int i = 1; i <= n; i++) {
    for (int x : e[i]) {
      if (bl[i] != bl[x]) ++val[bl[i]];
    }
  }
  for (int i = 1; i <= tot; i++) {
    if (val[i]) continue;
    puts("Yes");
    ans[0].clear(), ans[1].clear();
    for (int j = 1; j <= n; j++) {
      ans[bl[j] == i].push_back(j);
    }
    printf("%d %d\n", (int) ans[1].size(), (int) ans[0].size());
    for (int x : ans[1]) printf("%d ", x); puts("");
    for (int x : ans[0]) printf("%d ", x); puts("");
    return;
  }
  puts("No");
}

int main() {
  int T;
  scanf("%d", &T);
  while (T--) solve();
  return 0;
}


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

const int maxn = 2e6 + 10;
int n, m;

vector <int> e[maxn];

void chkmin(int& x, int y) {
  if (x > y) x = y;
}

void addline(int u, int v) {
  e[u].push_back(v);
}

bool vis[maxn];
int now, tot, top, st[maxn], sz[maxn], bl[maxn], dfn[maxn], low[maxn], val[maxn];

void tarjan(int u) {
  vis[st[++top] = u] = 1;
  dfn[u] = low[u] = ++now;
  for (int v : e[u]) {
    if (!dfn[v]) {
      tarjan(v);
      chkmin(low[u], low[v]);
    } else if (vis[v]) {
      chkmin(low[u], low[v]);
    }
  }
  if (dfn[u] == low[u]) {
    for (tot++; st[top + 1] != u; top--) {
      sz[tot]++;
      vis[st[top]] = 0;
      bl[st[top]] = tot;
    }
  }
}

vector <int> ans[2];

void solve() {
  now = tot = top = 0;
  memset(bl, 0, (n + 1) << 2);
  memset(sz, 0, (n + 1) << 2);
  memset(dfn, 0, (n + 1) << 2);
  memset(low, 0, (n + 1) << 2);
  memset(vis, 0, (n + 1) << 2);
  memset(val, 0, (n + 1) << 2);
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    e[i].clear();
  }
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    if (u != v) {
      e[u].push_back(v);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!dfn[i]) tarjan(i);
  }
  if (sz[bl[n]] == n) {
    puts("No"); return;
  }
  for (int i = 1; i <= n; i++) {
    for (int x : e[i]) {
      if (bl[i] != bl[x]) ++val[bl[i]];
    }
  }
  for (int i = 1; i <= tot; i++) {
    if (val[i] || !sz[i]) continue;
    puts("Yes");
    ans[0].clear(), ans[1].clear();
    for (int j = 1; j <= n; j++) {
      ans[bl[j] == i].push_back(j);
    }
    printf("%d %d\n", (int) ans[1].size(), (int) ans[0].size());
    for (int x : ans[1]) printf("%d ", x); puts("");
    for (int x : ans[0]) printf("%d ", x); puts("");
    return;
  }
  puts("No");
}

int main() {
  int T;
  scanf("%d", &T);
  while (T--) solve();
  return 0;
}


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

const int maxn = 2e6 + 10;
int n, m;

vector <int> e[maxn];

void chkmin(int& x, int y) {
  if (x > y) x = y;
}

bool vis[maxn];
int now, tot, top, st[maxn], sz[maxn], bl[maxn], dfn[maxn], low[maxn], val[maxn];

void tarjan(int u) {
  dfn[u] = low[u] = ++now;
  st[++top] = u, vis[u] = 1;
  for (int v : e[u]) {
    if (!dfn[v]) {
      tarjan(v);
      chkmin(low[u], low[v]);
    } else if (vis[v]) {
      chkmin(low[u], low[v]);
    }
  }
  if (dfn[u] == low[u]) {
    for (tot++; st[top + 1] != u; top--) {
      sz[tot]++;
      vis[st[top]] = 0;
      bl[st[top]] = tot;
    }
  }
}

vector <int> ans[2];

void solve() {
  now = tot = top = 0;
//  memset(bl, 0, (n + 1) << 2);
//  memset(sz, 0, (n + 1) << 2);
//  memset(dfn, 0, (n + 1) << 2);
//  memset(low, 0, (n + 1) << 2);
//  memset(vis, 0, (n + 1) << 2);
//  memset(val, 0, (n + 1) << 2);
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    e[i].clear();
    bl[i] = sz[i] = dfn[i] = low[i] = val[i] = vis[i] = 0;
  }
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    if (u != v) {
      e[u].push_back(v);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!dfn[i]) tarjan(i);
  }
  if (sz[bl[1]] == n) {
    puts("No"); return;
  }
  for (int i = 1; i <= n; i++) {
    for (int x : e[i]) {
      if (bl[i] != bl[x]) ++val[bl[i]];
    }
  }
  for (int i = 1; i <= tot; i++) {
    if (val[i] || !sz[i]) continue;
    puts("Yes");
    ans[0].clear(), ans[1].clear();
    for (int j = 1; j <= n; j++) {
      ans[bl[j] == i].push_back(j);
    }
    printf("%d %d\n", (int) ans[1].size(), (int) ans[0].size());
    for (int x : ans[1]) printf("%d ", x); puts("");
    for (int x : ans[0]) printf("%d ", x); puts("");
    return;
  }
  puts("No");
}

int main() {
  int T;
  scanf("%d", &T);
  while (T--) solve();
  return 0;
}


#include <iostream>
#include <vector>
#include <stack>
using namespace std;
const int maxn = 1e6 + 100;
vector<int> G[maxn],val[maxn],ans1,ans2,tmp;
inline void addedge(int from,int to){G[from].push_back(to);}
int t,n,m,dfn[maxn],low[maxn],instk[maxn],col[maxn],deg[maxn],dfs_tot,col_tot;
stack<int> stk;
inline void clear(){
	ans1.clear();
	ans2.clear();
	for(int i = 1;i <= n;i++)
		G[i].clear(),val[i].clear(),deg[i] = dfn[i] = low[i] = instk[i] = col[i] = 0;
	dfs_tot = col_tot = 0;
}
inline void tarjan(int u){
	dfn[u] = low[u] = ++dfs_tot;
	stk.push(u);instk[u] = 1;
	for(int v : G[u])
		if(!dfn[v])tarjan(v),low[u] = min(low[u],low[v]);
		else if(instk[v])low[u] = min(low[u],dfn[v]);
	if(dfn[u] == low[u]){
		int t;
		col_tot++;
		do{
			t = stk.top();stk.pop(),instk[t] = 0;
			col[t] = col_tot;
			val[col_tot].push_back(t);
		}while(t != u);
	}
}
inline void solve(){
	clear();
	cin >> n >> m;
	for(int u,v,i = 1;i <= m;i++){
		cin >> u >> v;
		if(u != v)addedge(u,v);
	}
	for(int i = 1;i <= n;i++)
		if(!dfn[i])tarjan(i);
	for(int u = 1;u <= n;u++)
		for(int v : G[u])
			if(col[u] != col[v])deg[col[u]]++;
	for(int i = 1;i <= col_tot;i++)
		if(!deg[i])ans1.push_back(i);
		else ans2.push_back(i);
	if(ans1.empty()){
		cout << "No" << '\n';
		return;
	}
	if(ans2.empty())ans2.push_back(ans1.back()),ans1.pop_back();
	if(ans1.empty()){
		cout << "No" << '\n';
		return;
	}
	cout << "Yes" << '\n';
	tmp.clear();
	for(auto x : ans1)
		for(auto y : val[x])
			tmp.push_back(y);
	cout << tmp.size() << " ";

	tmp.clear();
	for(auto x : ans2)
		for(auto y : val[x])
			tmp.push_back(y);
	cout << tmp.size() << "\n";

	tmp.clear();
	for(auto x : ans1)
		for(auto y : val[x])
			tmp.push_back(y);
	for(auto x : tmp)
		cout << x << " ";
	cout << '\n';

	tmp.clear();
	for(auto x : ans2)
		for(auto y : val[x])
			tmp.push_back(y);
	for(auto x : tmp)
		cout << x << " ";
	cout << '\n';
}
int main(){
	ios::sync_with_stdio(false);
	cin >> t;
	while(t--)solve();
	return 0;
}


#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<ctime>
using namespace std;
typedef long long ll;
const int N = 1000100;
ll read() {
    ll x = 0,f = 1;char c = getchar();
    while(c < '0' || c > '9') {
        if(c == '-') f = -1;c = getchar();
    }
    while(c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}
vector<int>e1[N],e2[N];
int vis[N],cnt;
void dfs(int u) {
    vis[u] = 1;cnt++;
    for(vector<int>::iterator it = e1[u].begin();it != e1[u].end();++it) {
        if(vis[*it]) continue;
        dfs(*it);
    }
}
void dfs2(int u) {
    vis[u] = 1;cnt++;
    for(vector<int>::iterator it = e2[u].begin();it != e2[u].end();++it) {
        if(vis[*it]) continue;
        dfs2(*it);
    }
}
int main() {
    int T = read();
    while(T--) {
        int n = read(),m = read();
        cnt = 0;
        for(int i = 1;i <= n;++i) vis[i] = 0;
        for(int i = 1;i <= n;++i) e1[i].clear(),e2[i].clear();
        for(int i = 1;i <= m;++i) {
            int u = read(),v = read();
            e1[u].push_back(v);e2[v].push_back(u);
        }
        dfs(1);
        int bz = 1;
        if(cnt == n) {
            cnt = 0;
            for(int i = 1;i <= n;++i) vis[i] = 0;
            dfs2(1);
            if(cnt == n) {puts("No");continue;}
            cnt = n - cnt;
            bz = 0;
        }
        puts("Yes");
        printf("%d %d\n",cnt,n - cnt);
        for(int i = 1;i <= n;++i) {
            if(vis[i] == bz) printf("%d ",i);
        }
        puts("");
        for(int i = 1;i <= n;++i) {
            if(vis[i] != bz) printf("%d ",i);
        }
        puts("");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int cnt=0;
const int N=1e6+100;
vector<int>G1[N],G2[N];
bool vis[N];
void dfs1(int x)
{
	vis[x]=1;
	cnt++;
	for(int i=0;i<G1[x].size();i++)
	 {
	 	int v=G1[x][i];
	 	if(vis[v]) continue;
	 	dfs1(v);
	 }
}
void dfs2(int x)
{
	vis[x]=1;
	cnt++;
	for(int i=0;i<G2[x].size();i++)
	 {
	 	int v=G2[x][i];
	 	if(vis[v]) continue;
	 	dfs2(v);
	 }
}
int main()
{
	ios::sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--)
	{
		int n,m;
		cin>>n>>m;
		memset(vis,0,sizeof vis);
		for(int i=0;i<=n;i++) G1[i].clear(),G2[i].clear();
		for(int i=1;i<=m;i++)
		{
			int x,y;
			cin>>x>>y;
			G1[x].push_back(y);
			G2[y].push_back(x);
		}
		cnt=0;
		dfs1(1);
		int bz=1;
		if(cnt==n)
		{
			//memset(vis,0,sizeof vis);
			for(int i=0;i<=n;i++) vis[i]=0;
			cnt=0;
			dfs2(1);
			if(cnt==n)
			{
				cout<<"No"<<endl;
				continue;
			}
			cnt=n-cnt;
			bz=0;
		}
		cout<<"Yes"<<endl;
		cout<<cnt<<' '<<n-cnt<<endl;
		for(int i=1;i<=n;i++)
		 if(vis[i]==bz) cout<<i<<' ';
		cout<<endl;
		for(int i=1;i<=n;i++)
	     if(vis[i]!=bz) cout<<i<<' ';
		cout<<endl;

	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int cnt=0;
const int N=1e6+100;
vector<int>G1[N],G2[N];
bool vis[N];
void dfs1(int x)
{
	vis[x]=1;
	cnt++;
	for(int i=0;i<G1[x].size();i++)
	 {
	 	int v=G1[x][i];
	 	if(vis[v]) continue;
	 	dfs1(v);
	 }
}
void dfs2(int x)
{
	vis[x]=1;
	cnt++;
	for(int i=0;i<G2[x].size();i++)
	 {
	 	int v=G2[x][i];
	 	if(vis[v]) continue;
	 	dfs2(v);
	 }
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,m;
		cin>>n>>m;
		memset(vis,0,sizeof vis);
		for(int i=0;i<=n;i++) G1[i].clear(),G2[i].clear();
		for(int i=1;i<=m;i++)
		{
			int x,y;
			cin>>x>>y;
			G1[x].push_back(y);
			G2[y].push_back(x);
		}

		dfs1(1);
		int ans=0,bz=1;
		if(cnt==n)
		{
			//memset(vis,0,sizeof vis);
			for(int i=0;i<=n;i++) vis[i]=0;
			cnt=0;
			dfs2(1);
			if(cnt==n)
			{
				cout<<"No"<<endl;
				continue;
			}
			cnt=n-cnt;
			bz=0;
		}
		cout<<"Yes"<<endl;
		cout<<ans<<' '<<n-ans<<endl;
		for(int i=1;i<=n;i++)
		 if(vis[i]==bz) cout<<i<<' ';
		cout<<endl;
		for(int i=1;i<=n;i++)
	     if(vis[i]!=bz) cout<<i<<' ';
		cout<<endl;

	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int cnt=0;
const int N=1e6+100;
vector<int>G1[N],G2[N];
bool vis[N];
void dfs1(int x)
{
	vis[x]=1;
	cnt++;
	for(int i=0;i<G1[x].size();i++)
	 {
	 	int v=G1[x][i];
	 	if(vis[v]) continue;
	 	dfs1(v);
	 }
}
void dfs2(int x)
{
	vis[x]=1;
	cnt++;
	for(int i=0;i<G2[x].size();i++)
	 {
	 	int v=G2[x][i];
	 	if(vis[v]) continue;
	 	dfs2(v);
	 }
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,m;
		cin>>n>>m;
		memset(vis,0,sizeof vis);
		for(int i=0;i<=n;i++) G1[i].clear(),G2[i].clear();
		for(int i=1;i<=m;i++)
		{
			int x,y;
			cin>>x>>y;
			G1[x].push_back(y);
			G2[y].push_back(x);
		}
		cnt=0;
		dfs1(1);
		int bz=1;
		if(cnt==n)
		{
			//memset(vis,0,sizeof vis);
			for(int i=0;i<=n;i++) vis[i]=0;
			cnt=0;
			dfs2(1);
			if(cnt==n)
			{
				cout<<"No"<<endl;
				continue;
			}
			cnt=n-cnt;
			bz=0;
		}
		cout<<"Yes"<<endl;
		cout<<cnt<<' '<<n-cnt<<endl;
		for(int i=1;i<=n;i++)
		 if(vis[i]==bz) cout<<i<<' ';
		cout<<endl;
		for(int i=1;i<=n;i++)
	     if(vis[i]!=bz) cout<<i<<' ';
		cout<<endl;

	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int cnt=0;
const int N=1e6+100;
vector<int>G1[N],G2[N];
bool vis[N];
void dfs1(int x)
{
	vis[x]=1;
	cnt++;
	for(int i=0;i<G1[x].size();i++)
	 {
	 	int v=G1[x][i];
	 	if(vis[v]) continue;
	 	dfs1(v);
	 }
}
void dfs2(int x)
{
	vis[x]=1;
	cnt++;
	for(int i=0;i<G2[x].size();i++)
	 {
	 	int v=G2[x][i];
	 	if(vis[v]) continue;
	 	dfs2(v);
	 }
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,m;
		cin>>n>>m;
		memset(vis,0,sizeof vis);
		for(int i=0;i<=n;i++) G1[i].clear(),G2[i].clear();
		for(int i=1;i<=m;i++)
		{
			int x,y;
			cin>>x>>y;
			G1[x].push_back(y);
			G2[y].push_back(x);
		}
		cnt=0;
		dfs1(1);
		int ans=0,bz=1;
		if(cnt==n)
		{
			//memset(vis,0,sizeof vis);
			for(int i=0;i<=n;i++) vis[i]=0;
			cnt=0;
			dfs2(1);
			if(cnt==n)
			{
				cout<<"No"<<endl;
				continue;
			}
			cnt=n-cnt;
			bz=0;
		}
		cout<<"Yes"<<endl;
		cout<<ans<<' '<<n-ans<<endl;
		for(int i=1;i<=n;i++)
		 if(vis[i]==bz) cout<<i<<' ';
		cout<<endl;
		for(int i=1;i<=n;i++)
	     if(vis[i]!=bz) cout<<i<<' ';
		cout<<endl;

	}
	return 0;
}


#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
//#define int long long
#define fir first
#define sec second
#define pi pair<ll ,int >
#define mp(x,y) make_pair(x,y)
#define sqr(x) ((x)*(x))
using namespace std;
const int maxn=1e6+10;
struct node{int to,nxt;}e[2][maxn<<1];
int n,m,cnt,tot[2],head[2][maxn];bool vis[maxn];
int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
void print(int x){
    if(x<0)putchar('-'),x=-x;
    if(x>9)print(x/10);
    putchar(x%10+'0');
}
void write(int x){print(x);puts("");}
void init(){
    cnt=tot[0]=tot[1]=0;
    for(int i=1;i<=n;i++)vis[i]=0;
    for(int i=1;i<=n;i++)head[0][i]=head[1][i]=0;
}
void add(int opt,int u,int v){e[opt][++tot[opt]].to=v;e[opt][tot[opt]].nxt=head[opt][u];head[opt][u]=tot[opt];}
void dfs(int opt,int x){
    vis[x]=1;cnt++;
    for(int i=head[opt][x],v=e[opt][i].to;i;i=e[opt][i].nxt,v=e[opt][i].to)
        if(!vis[v])dfs(opt,v);
}
signed main(){
    int T=read();
    while(T--){
        n=read();m=read();init();
        for(int i=1;i<=m;i++){
            int u=read(),v=read();
            add(0,u,v);add(1,v,u);
        }dfs(0,1);bool flag=1;
        if(cnt==n){
            cnt=0;for(int i=1;i<=n;i++)vis[i]=0;dfs(1,1);
            if(cnt==n){puts("No");continue;}cnt=n-cnt;flag=0;
        }printf("Yes\n%d %d\n",cnt,n-cnt);
        for(int i=1;i<=n;i++)if(vis[i]==flag)printf("%d ",i);puts("");
        for(int i=1;i<=n;i++)if(vis[i]!=flag)printf("%d ",i);puts("");
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>

using namespace std;
typedef long long ll;
const int MAXN = 1000005;
int n,m;
struct Node
{
	int u,v;
	Node *next;
}*h[MAXN],pool[MAXN<<1];
int tot = 0;
inline void addEdge(int u, int v)
{
	Node *p = &pool[++tot];
	p->v = v; p->next = h[u]; h[u] = p;
}
int dfn[MAXN],low[MAXN],scc[MAXN],size[MAXN],stack[MAXN],top = 0, cnt = 0;
bool vis[MAXN];
inline void dfs(int u)
{
    stack[++top] = u;
    vis[u] = true;
    dfn[u] = low[u] = ++cnt;
    for(Node *p = h[u]; p; p=p->next)
    {
        if(!dfn[p->v])
        {
            dfs(p->v);
            low[u] = min(low[u],low[p->v]);
		}
		else if(vis[p->v]) low[u] = min(low[u],dfn[p->v]);
	}
	if(low[u]==dfn[u])
	{
		size[u] = 0;
        while(1)
        {
            scc[stack[top]] = u;
            size[u]++;
            vis[stack[top]] = false;
            if(stack[top--]==u)
			    break;
			size[stack[top+1]] = 0;
		}
    }
}
int du[MAXN];
inline void init()
{
	for(int i = 1; i<=n; i++)
	{
	    h[i] = NULL;
        vis[i] = false;
        dfn[i] = low[i] = 0;
	    size[i] = 1, scc[i] = i;
	    du[i] = 0;
	}
	tot = cnt = top = 0;
}

int main()
{
    int T;
    cin >> T;
    while(T--)
    {
        scanf("%d%d",&n,&m);
        init();
        int u,v;
        for(int i = 1; i<=m; i++)
        {
            scanf("%d%d",&u,&v);
			addEdge(u,v);
		}
		for(int i = 1; i<=n; i++)
		    if(!dfn[i])
		        dfs(i);
		bool flag = false;
		for(int i = 1; i<=n; i++)
		    if(scc[i]!=scc[1])
		        flag = true;
	    if(!flag)
	    {
	        printf("No\n");
	        continue;
		}
		for(int i = 1; i<=n; i++)
		    for(Node *p = h[i]; p; p=p->next)
		        if(scc[i]!=scc[p->v])
		            du[scc[i]]++;
		int num = 0;
		for(int i = 1; i<=n; i++)
		    if(i==scc[i]&&!du[i])
	            num = i;
	    printf("Yes\n%d %d\n",size[num],n-size[num]);
	    for(int i = 1; i<=n; i++)
	        if(scc[i]==num)
	            printf("%d ",i);
	    printf("\n");
	    for(int i = 1; i<=n; i++)
	        if(scc[i]!=num)
	            printf("%d ",i);
	    printf("\n");
	}
	return 0;
}


#include<bits/stdc++.h>
#define mp(a,b) memset(a,b,sizeof(a))
#define ma 1500000
#define cuts printf("\n----------------\n");
//#define debug

using namespace std;

struct data{
	int to,head;
}e[ma];
stack <int> s;
int head[ma],len,n,t,m,cnt,num;
int la[ma],sz[ma],dfn[ma],low[ma],vis[ma];
void add(int u,int v){
	e[++len].to=v; e[len].head=head[u];
	head[u]=len;
}
void dfs(int lab){
	dfn[lab]=low[lab]=++cnt;
	s.push(lab);
	for(int i=head[lab];i;i=e[i].head){
		if(!dfn[e[i].to]) dfs(e[i].to);
		if(!vis[e[i].to]) low[lab]=min(low[lab],low[e[i].to]);
	}
	if(dfn[lab]==low[lab])
	{
		//cuts
		la[lab]=++num; sz[num]=vis[lab]=1;
		int m1=s.top();
		//printf("%d:\n%d ",num,lab);
		while(m1!=lab)
		{
			la[m1]=num; s.pop(); vis[m1]=1;
			//printf("%d ",m1);
			m1=s.top(); sz[num]++;
		}
		s.pop(); //cuts
	}
}
void print(int lab)
{
	printf("Yes\n");
	printf("%d %d\n",sz[lab],n-sz[lab]);
	for(int i=1;i<=n;i++)
		if(la[i]==lab) printf("%d ",i);
	printf("\n");
	for(int i=1;i<=n;i++)
		if(la[i]!=lab) printf("%d ",i);
}
int main()
{
	int u,v;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++){
			head[i]=dfn[i]=low[i]=vis[i]=0;
		}
		len=num=cnt=0;
		for(int i=1;i<=m;i++){
			scanf("%d%d",&u,&v);
			if(u!=v) add(u,v);
		}
		#ifdef debug
			for(int i=1;i<=n;i++){
				printf("%d: ",i);
				for(int j=head[i];j;j=e[j].head){
					printf("%d ",e[j].to);
				}
				printf("\n");
			}
			cuts

		#endif
		for(int i=1;i<=n;i++){
			if(!vis[i]) dfs(i);
		}
		if(num==1) printf("No\n");
		else	print(1);
	}
}


#include <bits/stdc++.h>
#define VI vector<int>
#define pb push_back
#define pii pair<int,int>
#define mp make_pair
#define LL long long
using namespace std;
const int MAXN = 1e5;
VI g[MAXN+10];
int n, m, t;
bool v[MAXN+10], s[MAXN+10];
int in[MAXN+10], low[MAXN+10], out[MAXN+10], ind;
int B[MAXN+10];
stack<int> S;

int scc;
void tarjan(int cur){
	low[cur] = in[cur] = ind++;
	S.push(cur); s[cur] = true; v[cur] = true;

	for(int i = 0; i < (int)g[cur].size(); i++){
		int next = g[cur][i];
		if(!v[next]){
			tarjan(next);
			low[cur] = min(low[cur], low[next]);
		}else if(s[next]) low[cur] = min(low[cur], in[next]);
	}

	if(low[cur] == in[cur]){
		while(true){
			int node = S.top(); S.pop(); s[node] = false;
			B[node] = scc;
			if(node == cur) break;
		}
		scc++;
	}
}
void init(){
	for(int i = 0; i <= n; i++){
		v[i] = s[i] = false;
		g[i].clear();
		low[i] = in[i] = 0;
		out[i] = 0;
	}
	scc = ind = 0;
}

int main(){
	scanf("%d", &t);
	while(t--){
		scanf("%d %d", &n, &m);
		init();
		for(int i = 0; i < m; i++){
			int x, y;
			scanf("%d %d", &x, &y);
			if(x == y) continue;
			g[x].pb(y);
		}
		for(int i = 1; i <= n; i++){
			if(!v[i]) tarjan(i);
		}
		if(scc == 1){
			printf("No\n");
		}else{
			printf("Yes\n");
			for(int i = 1; i <= n; i++){
				for(int j = 0; j < (int)g[i].size(); j++){
					int x = B[i], y = B[g[i][j]];
					if(x == y) continue;
					out[x]++;
				}
			}
			int sc = -1;
			for(int i = 0; i < scc && sc == -1; i++){
				if(!out[i]) sc = i;
			}
			int res = 0;
			for(int i = 1; i <= n; i++) res += B[i] == sc;
			printf("%d %d\n", res, n-res);
			for(int i = 1; i <= n; i++) if(B[i] == sc) printf("%d ", i); printf("\n");
			for(int i = 1; i <= n; i++) if(B[i] != sc) printf("%d ", i); printf("\n");
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define VI vector<int>
#define pb push_back
#define pii pair<int,int>
#define mp make_pair
#define LL long long
using namespace std;
const int MAXN = 1e6;
VI g[MAXN+10];
int n, m, t;
bool v[MAXN+10], s[MAXN+10];
int in[MAXN+10], low[MAXN+10], out[MAXN+10], ind;
int B[MAXN+10];
stack<int> S;

int scc;
void tarjan(int cur){
	low[cur] = in[cur] = ind++;
	S.push(cur); s[cur] = true; v[cur] = true;

	for(int i = 0; i < (int)g[cur].size(); i++){
		int next = g[cur][i];
		if(!v[next]){
			tarjan(next);
			low[cur] = min(low[cur], low[next]);
		}else if(s[next]) low[cur] = min(low[cur], in[next]);
	}

	if(low[cur] == in[cur]){
		while(true){
			int node = S.top(); S.pop(); s[node] = false;
			B[node] = scc;
			if(node == cur) break;
		}
		scc++;
	}
}
void init(){
	for(int i = 0; i <= n; i++){
		v[i] = s[i] = false;
		g[i].clear();
		low[i] = in[i] = 0;
		out[i] = 0;
	}
	scc = ind = 0;
}

int main(){
	scanf("%d", &t);
	while(t--){
		scanf("%d %d", &n, &m);
		init();
		for(int i = 0; i < m; i++){
			int x, y;
			scanf("%d %d", &x, &y);
			if(x == y) continue;
			g[x].pb(y);
		}
		for(int i = 1; i <= n; i++){
			if(!v[i]) tarjan(i);
		}
		if(scc == 1){
			printf("No\n");
		}else{
			printf("Yes\n");
			for(int i = 1; i <= n; i++){
				for(int j = 0; j < (int)g[i].size(); j++){
					int x = B[i], y = B[g[i][j]];
					if(x == y) continue;
					out[x]++;
				}
			}
			int sc = -1;
			for(int i = 0; i < scc && sc == -1; i++){
				if(!out[i]) sc = i;
			}
			int res = 0;
			for(int i = 1; i <= n; i++) res += B[i] == sc;
			printf("%d %d\n", res, n-res);
			for(int i = 1; i <= n; i++) if(B[i] == sc) printf("%d ", i); printf("\n");
			for(int i = 1; i <= n; i++) if(B[i] != sc) printf("%d ", i); printf("\n");
		}
	}
	return 0;
}


#include <stdio.h>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <utility>
#include <sstream>
#include <iostream>
#include <algorithm>
// #include <unordered_map>
#define SZ(a) ((int)(a.size()))
#define rep(i,a,n) for(int i=a;i<n;++i)
#define per(i,a,n) for(int i=(n)-1;i>=a;--i)
#define foredge(u,i) for(int i=head[u];~i;i=l[i].nxt)
#define mem(a,x) memset(a,x,sizeof(a))
using namespace std;
#ifdef LOCAL
template<class T>void print_array(T *a,T *end){printf("#[");for(;a<end;++a)cout<<(*a)<<',';cout<<"\b]\n";};
template<class T>ostream &operator<<(ostream&os,vector<T>V){os<<"[";for(auto vv:V)os<<vv<<",";return os<<"\b]";}
void dbg(){cout<<"\b \n";}
template<typename T,typename...A>void dbg(T a,A...x){cout<<(a)<<',';dbg(x...);}
#define LOG(x...) {cout<<'#';dbg(x);}
#else
#define print_array(...)
#define LOG(...)
#endif
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<int,int> pii;
const double PI=acos(-1.0);
const double EPS=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
inline ll readll(){
	ll x(0),op(1);char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')op=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
	return x*op;
}
//const int sz=;

const int sz_n=1000015;
const int sz_m=5000015;
stack <int> s;
struct edge{
	int f,t,nxt;
} l[sz_m];
int head[sz_n],tot=0;
void build(int f,int t){
	l[tot].f=f,l[tot].t=t,l[tot].nxt=head[f];
	head[f]=tot++;
}
int n,m;
int dfs_clock,low[sz_n],dfn[sz_n];
int scc[sz_n],scc_num;
int tarjan(int u){
	low[u]=dfn[u]=++dfs_clock;
	s.push(u);

	foredge(u,i){
		int v=l[i].t;
		if(!dfn[v]){
			low[v]=tarjan(v);
			low[u]=min(low[u],low[v]);
		}else if(!scc[v]){
			low[u]=min(low[u],dfn[v]);
		}
	}
	if(dfn[u]==low[u]){
		scc_num++;
		while(true){
			int f=s.top();s.pop();
			scc[f]=scc_num;
			if(f==u) break;
		}
	}
	return low[u];
}

void init(int n){
	rep(i,0,n+5) head[i]=-1,low[i]=dfn[i]=scc[i]=0;
	// mem(head,-1);
	// // mem(l,0);
	// mem(low,0);
	// mem(dfn,0);
	// mem(scc,0);
	while(!s.empty()) s.pop();
	tot=dfs_clock=scc_num=0;
}
int c[sz_n];
int in1[sz_m],in2[sz_m];//离线的数据

int getd0(){
	int cnt=0;
	rep(i,1,scc_num+1){
		if(c[i]==0) return i;
	}
	return -1;
}
vec jv,cv;
void solve(){
	//建原图
	int a,b;
	rep(i,0,m){
		scanf("%d%d",&a,&b);
		in1[i]=a,in2[i]=b;
		if(a==b) continue;
		build(a,b);
	}
	//求scc
	rep(i,1,n+1){
		if(!scc[i]) tarjan(i);
	}
	//缩点建图
	rep(i,0,n+5) head[i]=-1;
	// mem(head,-1);
	// mem(l,0);
	tot=0;
	// LOG("scc_num",scc_num);
	rep(i,0,m){
		int a=in1[i],b=in2[i];
		a=scc[a],b=scc[b];
		if(a==b) continue;
		build(a,b);
		// LOG("insert",a,b);
	}
	//求入度
	rep(i,0,n+5) c[i]=0;
	// mem(c,0);
	rep(u,1,scc_num+1){
		foredge(u,i){
			c[l[i].f]++;
		}
	}
	//统计入度为0的点
	int jscc=getd0();
	jv.clear();cv.clear();
	rep(i,1,n+1){
		if(scc[i]==jscc) jv.push_back(i);
		else cv.push_back(i);
	}
	if(SZ(jv)>0&&SZ(jv)<n){
		puts("YES");
		printf("%d %d\n",SZ(jv),SZ(cv));
		rep(i,0,SZ(jv)){
			if(i) putchar(' ');
			printf("%d",jv[i]);
		}
			puts("");
		rep(i,0,SZ(cv)){
			if(i) putchar(' ');
			printf("%d",cv[i]);
		}
			puts("");
	}else{
		puts("NO");
	}
	// LOG("j",jv);
	// LOG("c",cv);
	// LOG("cnt",cnt);
	// return cnt;
}
int main(){
	int _=readll();
	while(_--){
		scanf("%d%d",&n,&m);
		init(n);
		solve();
		// printf("%d\n",solve());
	}
	return 0;
}


/*Program from Luvwgyx*/
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
//#define int long long
#define fir first
#define sec second
#define pi pair<ll ,int >
#define mp(x,y) make_pair(x,y)
#define sqr(x) ((x)*(x))
using namespace std;
const int maxn=1e6+10;
struct node{int to,nxt;}e[2][maxn<<1];
int n,m,cnt,tot[2],head[2][maxn];bool vis[maxn];
int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
void print(int x){
    if(x<0)putchar('-'),x=-x;
    if(x>9)print(x/10);
    putchar(x%10+'0');
}
void write(int x){print(x);puts("");}
void init(){
    cnt=tot[0]=tot[1]=0;
    memset(vis,0,sizeof(vis));
    memset(head,0,sizeof(head));
}
void add(int opt,int u,int v){e[opt][++tot[opt]].to=v;e[opt][tot[opt]].nxt=head[opt][u];head[opt][u]=tot[opt];}
void dfs(int opt,int x){
    vis[x]=1;cnt++;
    for(int i=head[opt][x],v=e[opt][i].to;i;i=e[opt][i].nxt,v=e[opt][i].to)
        if(!vis[v])dfs(opt,v);
}
signed main(){
    int T=read();
    while(T--){
        n=read();m=read();init();
        for(int i=1;i<=m;i++){
            int u=read(),v=read();
            add(0,u,v);add(1,v,u);
        }dfs(0,1);bool flag=1;
        if(cnt==n){
            cnt=0;memset(vis,0,sizeof(vis));dfs(1,1);
            if(cnt==n){puts("No");continue;}
            cnt=n-cnt;flag=0;
        }printf("Yes\n%lld %lld\n",cnt,n-cnt);
        for(int i=1;i<=n;i++)if(vis[i]==flag)printf("%lld ",i);puts("");
        for(int i=1;i<=n;i++)if(vis[i]!=flag)printf("%lld ",i);puts("");
    }
    return 0;
}