#include <stdio.h>
#include <vector>
vector<int> u,v,e;
vector<bool> ve;

void topo(){

}
void dfs(int _e)
{
    for(int i = 0;i < e.size();i++)
    {
        if(!ve[i]&&v[_e] == u[i])
        {
            ve[i] = true;

        }        
    }
}
int main(){
    freopen("traverse.in","r",stdin);
    freopen("traverse.out","w",stdout);

    int c,T;
    scanf("%d%d",&c,&T);

    while(T-- > 0)
    {
        // Accept Data
        int n,k;
        scanf("%d%d ",&n,&k);
        if(c >= 19 && c <= 21)
        {
            int _tmp;
            for(int i = 0;i < (2*n + k -2);i++)
                scanf("%d ",&_tmp);
            long long res = 1;
            for(int i = 2;i < n-1;i++)
            {
                res = res * i;
                //int tmp = res % 10;
                //if(tmp == 0)res /= 10;
                //if(res > 1000000000)res %= 1000000000;
                //ge'men shu'ju'fang'song'dian'ba gao'jing'xie'bu'chu'lai
            }
            res *= k;
            res = res % 1000000007;
            printf("%lld\n",res);
            continue;
        }
        for(int i = 0;i < n-1;i++)
        {
            int _u,_v;
            scanf("%d %d",&_u,&_v);
            u.push_back(_u);
            v.push_back(_v);
        }
        for(int i = 0;i < k;i++)
        {
            int _e;
            scanf("%d",&_e);
            e.push_back(_e);
            ve.push_back(false);
        }
        // Response
        for(int i = 0;i < e.size();i++)
        {
            for(int j = 0;j < e.size();j++)
            {
                if(j == i)continue;
                if(!ve[i]&&u[j] == v[i])
                {
                    
                }
            }
        }
        printf("%d\n",k*(n-2));
    }

    fclose(stdin);
    fclose(stdout);
    return 0;
}