#include <iostream>
#include <iomanip>
#include <math.h>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cassert>
#include <deque>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#include <fstream>
#include <ctime>
#include <numeric>

using namespace std;

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2")

#define int long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vec vector
#define pb push_back

const int INF = 1e9;

void bfs(int start, int n, const vec<vec<int>>& adj, vec<int>& dist) {
    fill(dist.begin(), dist.end(), -1);
    queue<int> q;
    q.push(start);
    dist[start] = 0;
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (int v : adj[u]) {
            if (dist[v] == -1) {
                dist[v] = dist[u] + 1;
                q.push(v);
            }
        }
    }
}

vec<int> get_resentricities(int n, const vec<vec<int>>& adj) {
    vec<int> dist(n), dist_u(n), dist_v(n);
    bfs(0, n, adj, dist);
    int u = 0;
    for (int i = 0; i < n; ++i) {
        if (dist[i] > dist[u]) u = i;
    }
    bfs(u, n, adj, dist_u);
    int v = 0;
    for (int i = 0; i < n; ++i) {
        if (dist_u[i] > dist_u[v]) v = i;
    }
    bfs(v, n, adj, dist_v);
    vec<int> res(n);
    for (int i = 0; i < n; ++i) {
        res[i] = max(dist_u[i], dist_v[i]);
    }
    return res;
}

void solve() {
    int n, k;
    cin >> n >> k;
    vec<vec<int>> g1(n);
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        --u; --v;
        g1[u].push_back(v);
        g1[v].push_back(u);
    }
    vec<vec<int>> g2(n);
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        --u; --v;
        g2[u].push_back(v);
        g2[v].push_back(u);
    }
    vec<int> res1 = get_resentricities(n, g1), res2 = get_resentricities(n, g2);
    sort(res1.begin(), res1.end());
    sort(res2.begin(), res2.end());
    int ans = 0;
    for (int& d1 : res1) {
        int limit = k - 1 - d1;
        if (limit < 0) continue;
        auto it = upper_bound(res2.begin(), res2.end(), limit);
        ans += (it - res2.begin());
    }
    cout << ans << endl;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t; cin >> t;
    for (; t > 0; --t) solve();
    return 0;
}