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

using namespace std;

int max_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);
            }
        }
    }
    
    return *max_element(dist.begin(), dist.end());
}

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>> gr(n + n);
        for (int i = 0; i + 1 <  n; i++) {
            int a, b;
            cin >> a >> b;
            a--, b--;
            gr[a].push_back(b);
            gr[b].push_back(a);
        }
        for (int i = 0; i + 1 <  n; i++) {
            int a, b;
            cin >> a >> b;
            a--, b--;
            a += n;
            b += n;
            gr[a].push_back(b);
            gr[b].push_back(a);
        }
        int ans = 0;
        for (int i = 0; i < n; i++) {
            for (int j = n; j < n + n; j++) {
                gr[i].push_back(j);
                gr[j].push_back(i);
                if (max_dist(n + n, gr) <= k) {
                    ans++;
                }
                gr[i].erase(find(gr[i].begin(), gr[i].end(), j));
                gr[j].erase(find(gr[j].begin(), gr[j].end(), i));
            }
        }
        
        cout << ans << '\n';
    }
}