创建于 2026/07/18 · 更新于 2026/07/18
You are given a connected, undirected, unweighted simple graph with vertices and edges (no self-loops, no multiple edges). You are also given distinct waypoint vertices.
A simple cycle is a sequence of distinct vertices in which consecutive vertices are joined by an edge, including the first vertex and the last vertex; its length is its number of vertices. Moreover, we require that every cycle has length at least 3.
Find the minimum possible length of a simple cycle that passes through all waypoints, or report that no such path exists.
The first line contains an integer — the number of test cases.
Each test case: the first line has , , (, , ). Each of the next lines has an edge (, ); the graph is simple and connected. The last line lists the distinct waypoints.
It is guaranteed that the sum of over all test cases does not exceed .
For each test case, print one line: the minimum number of edges of a simple cycle through all waypoints, or if none exists.
2
4 4 2
1 2
2 3
3 4
1 4
1 4
4 3 3
1 2
1 3
1 4
2 3 4
4
-1