#include<bits/stdc++.h>
#define il inline
#define pb push_back
#define ll long long
using namespace std;
const int N=1e5+5,mod=1e9+7;
int idc,T,n,k,U[N],V[N],ans,flag;
template <typename T>
il void read(T &x)
{
	int f=1;char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<3)+(x<<1)+(ch^48);
		ch=getchar();
	}
	x*=f;
}
int main()
{
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	read(idc),read(T);
	while(T--)
	{
		read(n),read(k),flag=0;
		for(int i=1;i<n;i++)
		{
			read(U[i]),read(V[i]);
			if(U[i]==1) flag++;
			if(U[i]==i&&V[i]==i+1) flag--;
		}
		int res=0;
		for(int i=1;i<=k;i++)
		{
			int s;
			read(s);
			if(U[s]==1||V[s]==1||V[s]==n) res++;
		}
		if(flag==1-n) printf("%d\n",2*k-res);
		if(flag==n-1) printf("%lld\n",1ll*n*(n-1)%mod);
		puts("2");
	}
	return 0;
}

