#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define N 500100
ll Tid,T,n,k,u,v,head[N],cnt,key_edge[N],ans,dgr[N],type[N];
bool subA,subB;
pair<ll,ll>id[N];
struct node
{
    ll nxt,to;
}e[N];
template<typename T>inline void read(T &x)
{
    bool f=1;x=0;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=!f;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    x=(f?x:-x);return;
}
template<typename T>inline void write(T x)
{
    if(x<0) putchar('-'),x=-x;
    if(x>9) write(x/10);
    putchar(x%10+'0');
    return;
}
void add(ll u,ll v)
{
    e[++cnt].to=v;
    e[cnt].nxt=head[u];
    head[u]=cnt;
}
int main()
{
    freopen("traverse.in","r",stdin);
    freopen("traverse.out","w",stdout);
    read(Tid),read(T);
    while(T--)
    {
        subA=subB=0;
        ans=0;
        read(n),read(k);
        memset(e,0,sizeof(e));
        memset(head,0,sizeof(head));
        memset(id,0,sizeof(id));
        memset(type,0,sizeof(type));
        memset(dgr,0,sizeof(dgr));
        cnt=0;
        for(int i=1;i<n;i++)
        {
            read(u),read(v);
            id[i]={u,v};
            if(id[i].first!=i||id[i].second!=i+1) subA=1;
            if(id[i].first!=1||id[i].second!=i+1) subB=1;
            dgr[u]++,dgr[v]++;
            add(u,v);
            add(v,u);
        }
        for(int i=1;i<=k;i++) read(key_edge[i]);
        if(!subA)
        {
            write(1);
            putchar('\n');
            continue;
        }
        else if(k==1)
        {
            if(n==2)
            {
                write(1);
                putchar('\n');
                continue;
            }
            else if(n==3)
            {
                write(1);
                putchar('\n');
                continue;
            }
            else if(n==4)
            {
                for(int i=1;i<=n;i++) type[dgr[i]]++;
                if(type[2]==2&&type[1]==2)
                {
                    write(1);
                    putchar('\n');
                    continue;
                }
                if(type[3]==1&&type[1]==3)
                {
                    write(2);
                    putchar('\n');
                    continue;
                }
            }
            else if(n==5)
            {
                for(int i=1;i<=n;i++) type[dgr[i]]++;
                if(type[3]==1&&type[2]==1&&type[1]==3)
                {
                    write(2);
                    putchar('\n');
                    continue;
                }
                else if(type[4]==1&&type[1]==4)
                {
                    write(6);
                    putchar('\n');
                    continue;
                }
                else
                {
                    write(1);
                    putchar('\n');
                    continue;
                }
            }
        }
    }
    return 0;
}