#include using namespace std; typedef long long ll; typedef double db; typedef pair pii; #define fir first #define sec second #define rep(i,a,b) for (int i = (a) ; i <= (b) ; ++ i) #define rrp(i,a,b) for (int i = (a) ; i >= (b) ; -- i) #define gc() getchar() template inline void read(tp& x) { x = 0; char tmp; bool key = 0; for (tmp = gc() ; !isdigit(tmp) ; tmp = gc()) key = (tmp == '-'); for ( ; isdigit(tmp) ; tmp = gc()) x = (x << 3) + (x << 1) + (tmp ^ '0'); if (key) x = -x; } template inline void ckmn(tp& x,tp y) { x = x < y ? x : y; } template inline void ckmx(tp& x,tp y) { x = x < y ? y : x; } const int MOD = (int)(1e9 + 7); int power(int a,int b) { int ret = 1; while (b) { if (b&1) ret = (ll)ret * a % MOD; a = (ll) a * a % MOD; b >>= 1; } return ret; } const int N = 100010; struct edge { int la,b; } con[N << 1]; int tot, fir[N]; void add_edge(int from,int to) { con[++tot] = (edge) {fir[from], to}; fir[from] = tot; } int n, m; vector vec; bool dfs(int pos,int fa) { if (pos == m) return true; bool ret = 0; int deg = 0; for (int i = fir[pos]; i; i = con[i].la) { ++ deg; if (con[i].b == fa) continue; ret |= dfs(con[i].b, pos); } if (ret) vec.push_back(deg); return ret; } void init() { vec.clear(); memset(fir, 0, sizeof fir); tot = 0; } void solve() { int x,y; init(); read(n), read(m); rep (i, 1, n-1) { read(x), read(y); add_edge(x, y), add_edge(y, x); } dfs(1, 0); pii ans(1, 0); rep (i, 0, (int)vec.size()-1) { int tmp = vec[i]; x = power(tmp, MOD - 2); if (i == (int)vec.size()-1) { if (tmp == 1) y = 0; else y = power(tmp, MOD - 2); } else y = (ll)(tmp - 2) * power(tmp, MOD - 2) % MOD * power(tmp - 1, MOD - 2) % MOD; ans = pii((ll)ans.fir * x % MOD, ((ll)ans.fir * y + (ll)ans.sec * x) % MOD); } int tmp = (ans.fir + ans.sec) % MOD; printf("%d\n", tmp); } int main() { int T; read(T); while (T --) solve(); return 0; }