#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>

using namespace std;

vector<int> get_dist(int n,vector<vector<int>>&gr) {
    vector<int> dist(n, (int)1e9);
    deque<int> dq;
    dist[0] = 0;
    dq.push_back(0);
    while (!dq.empty()) {
        int v =  dq.front();
        dq.pop_front();
        for (auto to: gr[v]) {
            if (dist[to] > dist[v] + 1) {
                dist[to] = dist[v] + 1;
                dq.push_back(to);
            }
        }
    }
    int st=  max_element(dist.begin(), dist.end()) - dist.begin();
    dist.assign(n, (int)1e9);
    
    dist[st] = 0;
    dq.push_back(st);
    while (!dq.empty()) {
        int v =  dq.front();
        dq.pop_front();
        for (auto to: gr[v]) {
            if (dist[to] > dist[v] + 1) {
                dist[to] = dist[v] + 1;
                dq.push_back(to);
            }
        }
    }
    
    int fi = max_element(dist.begin(), dist.end()) - dist.begin();
    vector<int> dist1(n, (int)1e9);
    
    dist1[fi] = 0;
    dq.push_back(fi);
    while (!dq.empty()) {
        int v =  dq.front();
        dq.pop_front();
        for (auto to: gr[v]) {
            if (dist1[to] > dist1[v] + 1) {
                dist1[to] = dist1[v] + 1;
                dq.push_back(to);
            }
        }
    }
    for (int i = 0; i < n; i++) {
        dist[i] = max(dist[i], dist1[i]);
    }
    return dist;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin >> t;
    while (t--) {
        int n, k;
        cin >> n >> k;
        vector<vector<int>> gr1(n), gr2(n);
        for (int i = 0; i + 1 <  n; i++) {
            int a, b;
            cin >> a >> b;
            a--, b--;
            gr1[a].push_back(b);
            gr1[b].push_back(a);
        }
        for (int i = 0; i + 1 <  n; i++) {
            int a, b;
            cin >> a >> b;
            a--, b--;
            gr2[a].push_back(b);
            gr2[b].push_back(a);
        }
        
        vector<int> d1 = get_dist(n, gr1);
        vector<int> d2 = get_dist(n, gr2);
        vector<int> cnt1(n + 1), cnt2(n + 1);
        if (*max_element(d1.begin(),d1.end())>k || *max_element(d2.begin(),d2.end())>k) {
            cout << 0 << '\n';
            continue;
        }
        for (auto el: d1) cnt1[el]++;
        long long ans = 0;
        for (int i =1; i <= n; i++) cnt1[i] += cnt1[i - 1];
        for (auto el: d2) {
            if (0 <= k - 1 - el) {
                ans += cnt1[min(k - 1 - el, n)];
            }
        }
        cout << ans << '\n';
    }
}