#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
const int inf=1e9;
void bfs(int u, vector<vector<int>> &g, vector<int> &dist, vector<bool> &p, vector<int> &a) {
queue<int> q; q.push(u);
dist[u] = 0;
while (!q.empty()) {
int node = q.front(); q.pop();
if(p[node]) a.push_back(dist[node]);
for (auto &x: g[node]) {
if (dist[x] == -1) {
dist[x] = dist[node] + 1;
q.push(x);
}
}
}
}
void solve(){
int n,k; cin>>n>>k;
vector<bool> p(n+1);
for(int i=0;i<k;i++) {
int x; cin>>x;
p[x]=true;
}
int root=1;
vector<vector<int>> g(n+1);
vector<int> dist(n+1,-1),mset,ans(n+1);
for (int i = 0; i < n - 1; ++i) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; ++i)
if (g[i].size() == 1)
root=i;
bfs(root, g, dist, p, mset);
sort(mset.begin(), mset.end());
for (int i = 1; i <= n; ++i)
ans[i] = max(abs(dist[i] - mset.front()), abs(dist[i] - mset.back()));
cout << *min_element(ans.begin()+1,ans.end())<<'\n';
return;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int t; cin>>t;
while(t--){
solve();
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnVzaW5nIGk2NCA9IGxvbmcgbG9uZzsKCmNvbnN0IGludCBpbmY9MWU5OwoKdm9pZCBiZnMoaW50IHUsIHZlY3Rvcjx2ZWN0b3I8aW50Pj4gJmcsIHZlY3RvcjxpbnQ+ICZkaXN0LCB2ZWN0b3I8Ym9vbD4gJnAsIHZlY3RvcjxpbnQ+ICZhKSB7CiAgICBxdWV1ZTxpbnQ+IHE7IHEucHVzaCh1KTsKICAgIGRpc3RbdV0gPSAwOwogICAgd2hpbGUgKCFxLmVtcHR5KCkpIHsKICAgICAgICBpbnQgbm9kZSA9IHEuZnJvbnQoKTsgcS5wb3AoKTsKICAgICAgICBpZihwW25vZGVdKSBhLnB1c2hfYmFjayhkaXN0W25vZGVdKTsKICAgICAgICBmb3IgKGF1dG8gJng6IGdbbm9kZV0pIHsKICAgICAgICAgICAgaWYgKGRpc3RbeF0gPT0gLTEpIHsKICAgICAgICAgICAgICAgIGRpc3RbeF0gPSBkaXN0W25vZGVdICsgMTsgCiAgICAgICAgICAgICAgICBxLnB1c2goeCk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9Cn0KCnZvaWQgc29sdmUoKXsKICAgIGludCBuLGs7IGNpbj4+bj4+azsKCiAgICB2ZWN0b3I8Ym9vbD4gcChuKzEpOwogICAgZm9yKGludCBpPTA7aTxrO2krKykgewogICAgICAgIGludCB4OyBjaW4+Png7CiAgICAgICAgcFt4XT10cnVlOwogICAgfQogICAgaW50IHJvb3Q9MTsKICAgIHZlY3Rvcjx2ZWN0b3I8aW50Pj4gZyhuKzEpOwogICAgdmVjdG9yPGludD4gZGlzdChuKzEsLTEpLG1zZXQsYW5zKG4rMSk7CgogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuIC0gMTsgKytpKSB7CiAgICAgICAgaW50IHUsIHY7IGNpbiA+PiB1ID4+IHY7IAogICAgICAgIGdbdV0ucHVzaF9iYWNrKHYpOwogICAgICAgIGdbdl0ucHVzaF9iYWNrKHUpOwogICAgfQogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgKytpKQogICAgICAgIGlmIChnW2ldLnNpemUoKSA9PSAxKQogICAgICAgICAgICByb290PWk7CiAgICAKICAgIGJmcyhyb290LCBnLCBkaXN0LCBwLCBtc2V0KTsgCiAgICBzb3J0KG1zZXQuYmVnaW4oKSwgbXNldC5lbmQoKSk7CiAgICAKICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG47ICsraSkKICAgICAgICBhbnNbaV0gPSBtYXgoYWJzKGRpc3RbaV0gLSBtc2V0LmZyb250KCkpLCBhYnMoZGlzdFtpXSAtIG1zZXQuYmFjaygpKSk7CgogICAgY291dCA8PCAqbWluX2VsZW1lbnQoYW5zLmJlZ2luKCkrMSxhbnMuZW5kKCkpPDwnXG4nOwogICAgcmV0dXJuOwp9CgpzaWduZWQgbWFpbigpIHsKICAgIGlvczo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKICAgIGNpbi50aWUobnVsbHB0cik7IGNvdXQudGllKG51bGxwdHIpOwoKICAgIGludCB0OyBjaW4+PnQ7CiAgICB3aGlsZSh0LS0pewogICAgICAgIHNvbHZlKCk7CiAgICB9Cn0=