#include <bits/stdc++.h>
using namespace std;
#define ll long long

const int N = 1e5 + 10;
const int mod = 1e9 + 7;

struct edge {
	int u, v, nxt;
} e[N << 1];

int head[N], ecnt = 0;

void add(int u, int v) {
	e[++ecnt] = (edge) {u, v, head[u]};
	head[u] = ecnt;
}

ll ans;

bool is[N];

void dfs(int u, int fa) {
	int son = 0;
	for(int i = head[u]; i; i = e[i].nxt) {
		int v = e[i].v;
		if(v == fa) continue;
		if(!is[v]) son++;
		dfs(v, u);
	}
	if(son > 0) ans = (ans * son) % mod;
}

int main() {
	freopen("traverse.in", "r", stdin);
	freopen("traverse.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	int c, t;
	cin >> c >> t;
	while(t--) {
		int n, k;
		cin >> n >> k;
		bool flag = false;
		for(int i = 1; i < n; i++) {
			int u, v;
			cin >> u >> v;
			if(u != i || v != i + 1) flag = true;
			add(u, v);
			add(v, u);
		}
		if(!flag) {
			cout << 1 << endl;
			continue;
		}
		int x;
		cin >> x;
		is[e[x].u] = is[e[x].v] = true;
		ans = 1;
		dfs(e[x].u, 0);
		cout << ans << endl;
	}
	
	return 0;
}
