#include <bits/stdc++.h> using namespace std; #define int int long long #define fr(i, n) for (int i = 0; i < n; i++) #define fr1(i, n) for (int i = 1; i <= n; i++) #define S second #define F first #define pb(n) push_back(n) #define endl "\n" #define pr pair<int, int> #define so(a) sort(a.begin(), a.end()) #define intv vector<int> int mod = 1e9 + 7; int mod1 = -1 * mod; // cout << "Case #" << z << ": "<< "IMPOSSIBLE" << endl; // vector<int> v[1000001]; // int level[1000001] = {0}; // int in[1000001] = {0}; // int out[1000001] = {0}; // int low[1000001] = {0}; // int vis[1000001] = {0}, vis1[1000001] = {0}; // int visit[1001][1001] = {0}; // int dis[1001][1001] = {0}; int dx[] = {-1, 0, 1, 0, -1, 1, 1, -1}; int dy[] = {0, 1, 0, -1, 1, 1, -1, -1}; int timer = 0, n, m, t, k, eq = 0; vector<int> v[1000001]; int tourist_attractions[1000001]; int sum_to_Kplaces[1000001]; int level[1000001]; int parent[1000001]; int tot_nodes_in_subtree[1000001]; void simple_dfs(int x, int p) { parent[x] = p; for (auto child : v[x]) { if (child != p) { level[child] = 1 + level[x]; simple_dfs(child, x); tot_nodes_in_subtree[x] += tot_nodes_in_subtree[child]; } } return; } void Kdfs(int x, int p) { for (auto child : v[x]) { if (child != p) { sum_to_Kplaces[child] = sum_to_Kplaces[x] + (k - tot_nodes_in_subtree[child]) - tot_nodes_in_subtree[child]; Kdfs(child, x); } } return; } // part 2 priority_queue<int, vector<int>, greater<int>> pq[1000001]; int level_up[1000001]; void get_max2(int x, int p) { for (auto child : v[x]) { if (child != p) { get_max2(child, x); if (level_up[child]) { level_up[x] = max(level_up[x], 1 + level_up[child]); pq[x].push(level_up[child] + 1); if (pq[x].size() > 2) { pq[x].pop(); } } } } return; } int ans[1000001]; int max_from_parent_side[1000001]; void solve(int x, int p) { for (auto child : v[x]) { if (child != p) { ans[child] = max(ans[child], level_up[child]); int pp = -1, q = -1; if (max_from_parent_side[x] != 0) max_from_parent_side[child] = max(max_from_parent_side[child], 1 + max_from_parent_side[x]); while (pq[x].size()) { if (pp == -1) { pp = pq[x].top(); pq[x].pop(); } else { q = pq[x].top(); pq[x].pop(); } } if (q == -1) { if (pp != -1) { if (pp != 1 + level_up[child]) { max_from_parent_side[child] = max(max_from_parent_side[child], 1 + pp); } pq[x].push(pp); } } else { if (q != 1 + level_up[child]) { max_from_parent_side[child] = max(max_from_parent_side[child], 1 + q); } else { max_from_parent_side[child] = max(max_from_parent_side[child], 1 + pp); // ans[child] = max(ans[child], 1 + pp); } pq[x].push(pp); pq[x].push(q); } ans[child] = max(ans[child], max_from_parent_side[child]); solve(child, x); } } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); t = 1; cin >> t; while (t--) { cin >> n >> k; fr(i, n + 1) { tot_nodes_in_subtree[i] = 0; max_from_parent_side[i] = 0; level_up[i] = 0; v[i].clear(); ans[i] = 0; } fr(i, k) { cin >> tourist_attractions[i]; tot_nodes_in_subtree[tourist_attractions[i]] = 1; level_up[tourist_attractions[i]] = 1; ans[tourist_attractions[i]] = 1; max_from_parent_side[tourist_attractions[i]] = 1; } fr(i, n - 1) { int a, b; cin >> a >> b; v[a].pb(b); v[b].pb(a); } level[1] = 0; simple_dfs(1, -1); sum_to_Kplaces[1] = 0; fr(i, k) { sum_to_Kplaces[1] += level[tourist_attractions[i]]; } Kdfs(1, -1); // part 2 get_max2(1, -1); int x = -1, y = -1; while (pq[1].size()) { if (x == -1) { x = pq[1].top(); pq[1].pop(); } else { y = pq[1].top(); pq[1].pop(); } } if (y == -1) { if (x != -1) { ans[1] = x; pq[1].push(x); } } else { ans[1] = y; pq[1].push(x); pq[1].push(y); } solve(1, -1); fr1(i, n) { ans[i]--; } fr1(i, n) { ans[i] = sum_to_Kplaces[i] - ans[i]; } int answer = -1, yo = 1e15; fr1(i, n) { if (ans[i] <= yo) { answer = i; yo = ans[i]; } } // fr1(i, n) // { // cout << ans[i] << " "; // } // cout << endl; cout << answer << endl; } }