#include <bits/stdc++.h>
#define LL long long
#define fu(x , y , z) for(int x = y ; x <= z ; x ++)
using namespace std;
const int N = 1e5 + 5;
const LL mod = 1e9 + 7;
int n , k , g[N] , vis[N] , fac[N] , rt;
struct B {
    int x , y;
} b[N];
vector<int> e[N] , E[N];
map<int , int> mp[N];
LL ans[N];
void dfs (int x) {
    ans[x] = 1;
    LL s = 0;
    for (auto i : e[x]) {
        if (vis[i]) continue;
        dfs (i);
        s ++;
        ans[x] = ans[x] * ans[i] % mod;
    }
    ans[x] = ans [x] * s % mod;
}
int main () {
    freopen ("traverse.in" , "r" , stdin);
    freopen ("traverse.out" , "w" , stdout);
    int T , id;
    fac[0] = 1;
    fu (i , 1 , 100000) fac[i] = fac[i - 1] * i % mod;
    scanf ("%d%d" , &id , &T);
    while (T --) {
        scanf ("%d%d" , &n , &k);
        fu (i , 1 , n - 1) {
            int u , v;
            scanf ("%d%d" , &u , &v);
            b[i] = {u , v};
            E[u].push_back (i) , E[v].push_back(i);
        }
        fu (i , 1 , k) scanf ("%d" , &g[i]);
        if (id == 18) {
            printf ("%lld\n" , k * 2);
            continue;
        }
        if (id >= 19 && id <= 21) {
            printf ("%d\n" , fac[n - 1]);
            continue;
        }
        if (id <= 6) {
            fu (i , 1 , n) mp[i].clear();
            fu (i , 1 , n) {
                for (auto j : E[i]) {
                    for (auto l : E[i]) {
                        if (j == l || mp[j][l]) continue;
                        e[j].push_back(l) , e[l].push_back(j);
                        mp[j][l] = mp[l][j] = 1;
                    }
                }
            }
            // fu (i , 1, n) {
            //     cout << i << " ";
            //     for (auto j : e[i]) cout << j << " ";
            //     cout << "\n";
            // }
            // return 0;
            LL s = 0;
            fu (i , 1 , k) {
                rt = g[i];
                dfs (g[i]);
            }
            printf ("%lld\n" , s);
            continue;
        }
    }
    return 0;
}