# sol

## code

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int gi()
{
int x=0,w=1;char ch=getchar();
while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if (ch=='-') w=0,ch=getchar();
while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return w?x:-x;
}
const int N = 605;
const int M = 4e5+5;
struct edge{int u,v;}E[M];
bool Cross(int l1,int r1,int l2,int r2)
{
if (l1>l2) swap(l1,l2),swap(r1,r2);
return l1<l2&&l2<r1&&r1<r2;
}
{
}
void Tarjan(int u)
{
dfn[u]=low[u]=++tim;
Stack[++top]=u;vis[u]=1;
if (!dfn[to[e]]) Tarjan(to[e]),low[u]=min(low[u],low[to[e]]);
else if (vis[u]) low[u]=min(low[u],dfn[to[e]]);
if (dfn[u]==low[u])
{
++scc;int v;
do{
v=Stack[top--];
vis[v]=0;bel[v]=scc;
}while (u!=v);
}
}
bool check()
{
for (int i=1;i<=tot;++i)
if (bel[i]==bel[i+tot]) return false;
return true;
}
int main()
{
T=gi();
while (T--)
{
memset(dfn,0,sizeof(dfn));
cnt=tot=scc=tim=0;
n=gi();m=gi();
for (int i=1;i<=m;++i) E[i]=(edge){gi(),gi()};
for (int i=1;i<=n;++i) p[gi()]=i;
if (m>3*n-6) {puts("NO");continue;}
for (int i=1;i<=m;++i)
{
E[i].u=p[E[i].u];E[i].v=p[E[i].v];
if (E[i].u>E[i].v) swap(E[i].u,E[i].v);
if (E[i].u+1!=E[i].v&&!(E[i].u==1&&E[i].v==n)) E[++tot]=E[i];
}
for (int i=1;i<=tot;++i)
for (int j=i+1;j<=tot;++j)
if (Cross(E[i].u,E[i].v,E[j].u,E[j].v))