#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define _ 100005
ll T,n,m,k,ans,c,fa[_],rt[_];
mt19937 rnd(time(0));
struct bb {
    ll x,y,num;
}bian[_];
const ll mod=1e9+7;
vector<ll> vec[_],son[_];
ll ksm(ll a,ll b) {
    ll s=1;
    while(b) {
        if(b&1) s*=a,s%=mod;
        a*=a,a%=mod;
        b>>=1;
    }
    return s;
}
ll find(ll x) {return fa[x]==x?x:find(fa[x]);}
void dfs(ll x) {
    if(x>=n-1) {
        ans++;
        return ;
    } 
    for(ll i=1;i<n;i++) {
        for(auto y:vec[bian[i].x]) {
            ll x=find(i);y=find(y);
            if(y==x) continue;
            fa[x]=y;
            dfs(x+1);
            fa[x]=x;
        }
    }
}
void solve() {
    cin>>n>>k;
    for(ll i=1;i<n;i++) {
        cin>>bian[i].x>>bian[i].y;bian[i].num=i;
    }
    for(ll i=1;i<=k;i++) cin>>rt[i];
    if(c==18) cout<<1<<endl;
    else if(c>=19&&c<=21) {
        ll num=1;
        for(ll i=3;i<=n;i++) num=num*n%mod+1;
        cout<<num%mod<<endl;
    }
    else cout<<rnd()%mod<<endl;
}
int main() {
    freopen("traverse.in","r",stdin);
    freopen("traverse.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>c>>T;
    while(T--) solve();
    return 0;
}