#include<bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
const long long N = 2e5 + 9;
const long long OO = 2e18;
const double PI = acos(-1.0);
#define IOS ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
#define int long long int
#define ld long double
#define ve vector<int>
#define vep vector<pair<int,int>>
#define graph vector<ve>
#define visit vector<bool>
#define db(...) 7
#define fix(res, n) fixed << setprecision(n) << (long double)res
#define tc int testcase; cin>>testcase; while(testcase--)
#define all(num) (num).begin(), (num).end()
#define el cout<<(testcase?"\n":"")
// "وَأَن لَّيْسَ لِلْإِنسَانِ إِلَّا مَا سَعَى ﴿39﴾ وَأَنَّ سَعْيَهُ سَوْفَ يُرَى ﴿40﴾ ثُمَّ يُجْزَاهُ الْجَزَاء الْأَوْفَى "
// My way to My dream
//#ifdef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
//#endif
//int n , m ;
//bool ok (int x , int y)
//{
// return x>=0&&y>=0&&x<n&&y<m;
//}
//ve dx={1,0,-1,0,1,-1,1,-1};
//ve dy={0,1,0,-1,-1,1,1,-1};
ve dx = {1, 0, -1, 0};
ve dy = {0, 1, 0, -1};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void bfs(int i, graph &g, ve &dist, set<int> &st, ve &mdist) {
queue<int> q;
q.push(i);
dist[i] = 0;
if (st.find(i) != st.end()) {
mdist.push_back(dist[i]);
}
while (!q.empty()) {
int node = q.front();
q.pop();
for (auto x: g[node]) {
if (dist[x] == -1) {
q.push(x);
dist[x] = dist[node] + 1;
if (st.find(x) != st.end()) {
mdist.push_back(dist[x]);
}
}
}
}
}
signed main() {
IOS
tc {
int n, k, start;
cin >> n >> k;
set<int> st;
for (int i = 0; i < k; ++i) {
int num;
cin >> num;
st.insert(num);
}
graph g(n + 1);
ve 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)
start = i;
}
bfs(start, g, dist, st, mset);
std::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());
el;
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmNvbnN0IGxvbmcgbG9uZyBNT0QgPSA5OTgyNDQzNTM7CmNvbnN0IGxvbmcgbG9uZyBOID0gMmU1ICsgOTsKY29uc3QgbG9uZyBsb25nIE9PID0gMmUxODsKY29uc3QgZG91YmxlIFBJID0gYWNvcygtMS4wKTsKI2RlZmluZSBJT1MgaW9zOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpLGNpbi50aWUoTlVMTCksY291dC50aWUoTlVMTCk7CiNkZWZpbmUgaW50IGxvbmcgbG9uZyBpbnQKI2RlZmluZSBsZCBsb25nIGRvdWJsZQojZGVmaW5lIHZlIHZlY3RvcjxpbnQ+CiNkZWZpbmUgdmVwIHZlY3RvcjxwYWlyPGludCxpbnQ+PgojZGVmaW5lIGdyYXBoIHZlY3Rvcjx2ZT4KI2RlZmluZSB2aXNpdCB2ZWN0b3I8Ym9vbD4KI2RlZmluZSBkYiguLi4pIDcKI2RlZmluZSBmaXgocmVzLCBuKSBmaXhlZCA8PCAgc2V0cHJlY2lzaW9uKG4pIDw8IChsb25nIGRvdWJsZSlyZXMKI2RlZmluZSB0YyAgICAgICAgIGludCB0ZXN0Y2FzZTsgICBjaW4+PnRlc3RjYXNlOyAgIHdoaWxlKHRlc3RjYXNlLS0pCiNkZWZpbmUgYWxsKG51bSkgICAgICAobnVtKS5iZWdpbigpLCAobnVtKS5lbmQoKQojZGVmaW5lIGVsICAgICAgICAgICAgY291dDw8KHRlc3RjYXNlPyJcbiI6IiIpCgoKLy8gICAgICAgICAgICAgItmI2Y7Yo9mO2YYg2YTZkdmO2Yr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gaWYgKHN0LmZpbmQoaSkgIT0gc3QuZW5kKCkpIHsKICAgICAgICBtZGlzdC5wdXNoX2JhY2soZGlzdFtpXSk7CiAgICB9CiAgICB3aGlsZSAoIXEuZW1wdHkoKSkgewogICAgICAgIGludCBub2RlID0gcS5mcm9udCgpOwogICAgICAgIHEucG9wKCk7CiAgICAgICAgZm9yIChhdXRvIHg6IGdbbm9kZV0pIHsKICAgICAgICAgICAgaWYgKGRpc3RbeF0gPT0gLTEpIHsKICAgICAgICAgICAgICAgIHEucHVzaCh4KTsKICAgICAgICAgICAgICAgIGRpc3RbeF0gPSBkaXN0W25vZGVdICsgMTsKICAgICAgICAgICAgICAgIGlmIChzdC5maW5kKHgpICE9IHN0LmVuZCgpKSB7CiAgICAgICAgICAgICAgICAgICAgbWRpc3QucHVzaF9iYWNrKGRpc3RbeF0pOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9CgpzaWduZWQgbWFpbigpIHsKICAgIElPUwogICAgdGMgewoKICAgICAgICBpbnQgbiwgaywgc3RhcnQ7CiAgICAgICAgY2luID4+IG4gPj4gazsKCiAgICAgICAgc2V0PGludD4gc3Q7CgogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgazsgKytpKSB7CiAgICAgICAgICAgIGludCBudW07CiAgICAgICAgICAgIGNpbiA+PiBudW07CiAgICAgICAgICAgIHN0Lmluc2VydChudW0pOwogICAgICAgIH0KCiAgICAgICAgZ3JhcGggZyhuICsgMSk7CiAgICAgICAgdmUgZGlzdChuICsgMSwgLTEpLCAgIG1zZXQsICBhbnMobiArIDEpOwoKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG4gLSAxOyArK2kpIHsKICAgICAgICAgICAgaW50IHUsIHY7CiAgICAgICAgICAgIGNpbiA+PiB1ID4+IHY7CgogICAgICAgICAgICBnW3VdLnB1c2hfYmFjayh2KTsKICAgICAgICAgICAgZ1t2XS5wdXNoX2JhY2sodSk7CgogICAgICAgIH0KCiAgICAgICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgKytpKSB7CgogICAgICAgICAgICBpZiAoZ1tpXS5zaXplKCkgPT0gMSkKICAgICAgICAgICAgICAgIHN0YXJ0ID0gaTsKCiAgICAgICAgfQoKICAgICAgICBiZnMoc3RhcnQsIGcsIGRpc3QsIHN0LCBtc2V0KTsKCiAgICAgICAgc3RkOjpzb3J0KG1zZXQuYmVnaW4oKSwgbXNldC5lbmQoKSk7CiAgICAgICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgKytpKSB7CgogICAgICAgICAgICBhbnNbaV0gPSBtYXgoYWJzKGRpc3RbaV0gLSBtc2V0LmZyb250KCkpLCBhYnMoZGlzdFtpXSAtIG1zZXQuYmFjaygpKSk7CgogICAgICAgIH0KCiAgICAgICAgY291dCA8PCAqbWluX2VsZW1lbnQoYW5zLmJlZ2luKCkgKyAxLCBhbnMuZW5kKCkpOwogICAgICAgIGVsOwogICAgfQp9