模拟移动的过程。
#include<bits/stdc++.h>
using namespace std;
class point{
public:
stack<int>a;
int top()
{
return a.top();
}
void push(int x)
{
a.push(x);
}
void pop()
{
a.pop();
}
int size()
{
return a.size();
}
};
int main()
{
int t;cin>>t;
while(t--)
{
point a[4];
int n,m;
cin>>n>>m;
for(int i=n;i>=1;i--)
a[1].push(i);
int ok=1;
while(m--)
{
int x,y;
cin>>x>>y;
if(a[x].size()>0)
{
if(a[y].size()==0)
{
a[y ].push(a[x ].top());
a[x ].pop();
}
else if(a[y ].top()>a[x ].top())
{
//cout<<a[x ].top()<<':'<<a[y ].top()<<endl;
a[y ].push(a[x ].top());
a[x ].pop();
}
else ok=0;
}
else ok=0;
}
if(ok)
cout<<"legal"<<endl;
else
cout<<"illegal"<<endl;
}
}