package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	rr = bufio.NewReader(os.Stdin)
	ww = bufio.NewWriter(os.Stdout)
)

const (
	MaxN int = 2e5 + 5
)

type Edge struct {
	u, v int
}

type Graph struct {
	adj [][]int
	e   []*Edge
}

func NewGraph(n int) *Graph {
	return &Graph{
		adj: make([][]int, n+1),
		e:   make([]*Edge, 0),
	}
}

func (g *Graph) AddEdge(u, v int) {
	g.adj[u] = append(g.adj[u], len(g.e))
	g.e = append(g.e, &Edge{u, v})
}

var (
	T         int
	n, k      int
	g         *Graph
	deadPoint []bool
)

func calcDeadPoint(u int, dep int, par int) int {
	deadP := 0
	if deadPoint[u] {
		deadP = (dep + 1) / 2
	}
	for _, i := range g.adj[u] {
		v := g.e[i].v
		if v == par {
			continue
		}
		vDeadP := calcDeadPoint(v, dep+1, u)
		if (deadP == 0 || vDeadP < deadP) && vDeadP != 0 {
			deadP = vDeadP
		}
	}
	if dep == deadP {
		deadPoint[u] = true
	}
	return deadP
}

func calcPath(u int, par int) bool {
	if deadPoint[u] {
		return false
	}

	isLeaf := true

	for _, i := range g.adj[u] {
		v := g.e[i].v
		if v == par {
			continue
		}

		isLeaf = false
		if calcPath(v, u) {
			return true
		}
	}

	return isLeaf
}

func Init() {
	g = NewGraph(n)
	deadPoint = make([]bool, n+1)
}

func main() {
	_, _ = fmt.Fscan(rr, &T)
	for iCase := 0; iCase < T; iCase++ {
		_, _ = fmt.Fscan(rr, &n, &k)

		Init()

		for i := 0; i < k; i++ {
			var u int
			_, _ = fmt.Fscan(rr, &u)
			deadPoint[u] = true
		}

		for i := 1; i < n; i++ {
			var u, v int
			_, _ = fmt.Fscan(rr, &u, &v)
			g.AddEdge(u, v)
			g.AddEdge(v, u)
		}

		calcDeadPoint(1, 0, 0)
		if calcPath(1, 0) {
			_, _ = fmt.Fprintln(ww, "YES")
		} else {
			_, _ = fmt.Fprintln(ww, "NO")
		}
	}
	_ = ww.Flush()
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

var tree [][]int
var mark []bool
var markDist []int
var queue []int
var seen []bool
var queueHead int
var queueTail int

func findPath(node, parent, dist int) bool {
	if markDist[node] <= dist {
		return false
	}
	if len(tree[node]) == 1 && node != 1 {
		return true
	}
	for _, child := range tree[node] {
		if child != parent && findPath(child, node, dist+1) {
			return true
		}
	}
	return false
}

func bfsPush(node int) {
	queue[queueTail] = node
	seen[node] = true
	queueTail++
}

func solveCase() bool {
	var n, k, a, b int
	fmt.Fscan(in, &n, &k)
	tree = make([][]int, n+1)
	mark = make([]bool, n+1)
	seen = make([]bool, n+1)
	queue = make([]int, n+1)
	markDist = make([]int, n+1)
	queueHead = 0
	queueTail = 0

	for i := 0; i < k; i++ {
		fmt.Fscan(in, &a)
		mark[a] = true
		bfsPush(a)
	}

	for i := 0; i < n-1; i++ {
		fmt.Fscan(in, &a, &b)
		tree[a] = append(tree[a], b)
		tree[b] = append(tree[b], a)
	}

	for ; queueHead < queueTail; queueHead++ {
		var node = queue[queueHead]
		for _, nextNode := range tree[node] {
			if !seen[nextNode] {
				bfsPush(nextNode)
				markDist[nextNode] = markDist[node] + 1
			}
		}
	}

	return findPath(1, 1, 0)
}

func main() {
	var ntc int
	fmt.Fscan(in, &ntc)
	for t := 0; t < ntc; t++ {
		if solveCase() {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
	out.Flush()
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1611E2(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T, n, k, v, w int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k)
		fr := make([]bool, n)
		for ; k > 0; k-- {
			Fscan(in, &v)
			fr[v-1] = true
		}
		g := make([][]int, n)
		for i := 1; i < n; i++ {
			Fscan(in, &v, &w)
			v--
			w--
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}

		minDep := make([]int, n)
		size := make([]int, n)
		var build func(v, fa, d int)
		build = func(v, fa, d int) {
			minDep[v] = 1e9
			if fr[v] {
				minDep[v] = d
				size[v] = 1
			}
			for _, w := range g[v] {
				if w != fa {
					build(w, v, d+1)
					minDep[v] = min(minDep[v], minDep[w])
					size[v] += size[w]
				}
			}
		}
		build(0, -1, 0)

		var f func(v, fa, d int) int
		f = func(v, fa, d int) (ans int) {
			if d*2 >= minDep[v] {
				return 1
			}
			if size[v] == 0 {
				return -1
			}
			for _, w := range g[v] {
				if w != fa {
					res := f(w, v, d+1)
					if res < 0 {
						return -1
					}
					ans += res
				}
			}
			return
		}
		if f(0, -1, 0) < 0 {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

func main() { CF1611E2(os.Stdin, os.Stdout) }

func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func scan(a ...interface{})             { fmt.Fscan(reader, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

func main() {
	defer writer.Flush()
	// redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

func solve() {
	var n, k int
	scan(&n, &k)
	friends := make(map[int]struct{}, k)
	for x, i := 0, 0; i < k; i++ {
		scan(&x)
		friends[x-1] = struct{}{}
	}
	adj := make([][]int, n)
	for i := 0; i < n-1; i++ {
		var u, v int
		scan(&u, &v)
		adj[u-1] = append(adj[u-1], v-1)
		adj[v-1] = append(adj[v-1], u-1)
	}

	minDist := GetMinDist(adj, friends)

	ans := false
	var walk func(parent, node, d int)
	walk = func(parent, node, d int) {
		if minDist[node] <= d {
			return
		}
		if node != 0 && len(adj[node]) == 1 {
			ans = true
		}
		for _, next := range adj[node] {
			if next == parent {
				continue
			}
			walk(node, next, d+1)
		}
	}
	walk(-1, 0, 0)

	if ans {
		printf("YES\n")
	} else {
		printf("NO\n")
	}
}

func GetMinDist(adj [][]int, friends map[int]struct{}) []int {
	dist := make([]int, len(adj))
	for i := range dist {
		dist[i] = (1 << 30)
	}
	var walk func(parent, node int)
	walk = func(parent, node int) {
		if _, ok := friends[node]; ok {
			dist[node] = 0
			return
		}
		for _, next := range adj[node] {
			if next == parent {
				continue
			}
			walk(node, next)
			if dist[node] > dist[next]+1 {
				dist[node] = dist[next] + 1
			}
		}
	}
	walk(-1, 0)
	return dist
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	rr = bufio.NewReader(os.Stdin)
	ww = bufio.NewWriter(os.Stdout)
)

const (
	MaxN int = 2e5 + 5
)

type Edge struct {
	u, v int
}

type Graph struct {
	adj [][]int
	e   []*Edge
}

func NewGraph(n int) *Graph {
	return &Graph{
		adj: make([][]int, n+1),
		e:   make([]*Edge, 0),
	}
}

func (g *Graph) AddEdge(u, v int) {
	g.adj[u] = append(g.adj[u], len(g.e))
	g.e = append(g.e, &Edge{u, v})
}

var (
	T         int
	n, k      int
	g         *Graph
	deadPoint []bool
)

func calcDeadPoint(u int, dep int, par int) int {
	deadP := 0
	if deadPoint[u] {
		deadP = (dep + 1) / 2
	}
	for _, i := range g.adj[u] {
		v := g.e[i].v
		if v == par {
			continue
		}
		vDeadP := calcDeadPoint(v, dep+1, u)
		if (deadP == 0 || vDeadP < deadP) && vDeadP != 0 {
			deadP = vDeadP
		}
	}
	if dep == deadP {
		deadPoint[u] = true
	}
	return deadP
}

func calcPath(u int, par int) bool {
	if deadPoint[u] {
		return false
	}

	isLeaf := true

	for _, i := range g.adj[u] {
		v := g.e[i].v
		if v == par {
			continue
		}

		isLeaf = false
		if calcPath(v, u) {
			return true
		}
	}

	return isLeaf
}

func Init() {
	g = NewGraph(n)
	deadPoint = make([]bool, n+1)
}

func main() {
	_, _ = fmt.Fscan(rr, &T)
	for iCase := 0; iCase < T; iCase++ {
		_, _ = fmt.Fscan(rr, &n, &k)

		Init()

		for i := 0; i < k; i++ {
			var u int
			_, _ = fmt.Fscan(rr, &u)
			deadPoint[u] = true
		}

		for i := 1; i < n; i++ {
			var u, v int
			_, _ = fmt.Fscan(rr, &u, &v)
			g.AddEdge(u, v)
			g.AddEdge(v, u)
		}

		calcDeadPoint(1, 0, 0)
		if calcPath(1, 0) {
			_, _ = fmt.Fprintln(ww, "YES")
		} else {
			_, _ = fmt.Fprintln(ww, "NO")
		}
		fmt.Println(deadPoint)
	}
	_ = ww.Flush()
}


package main

import (
	. "fmt"
	"math"
)

func gcd(a, b int) int {
	for a != 0 {
		a, b = b%a, a
	}
	return b
}

func main() {
	var n, k, v int
	Scan(&n, &k)
	step := int(math.Sqrt(float64(n)))
	for gcd(step, n) > 1 {
		step++
	}
	for i := 0; ; i += step {
		Println("?", i%n+1)
		Scan(&v)
		if v == k {
			continue
		}
		if v < k {
			for j := i + 1; ; j++ {
				Println("?", j%n+1)
				Scan(&v)
				if v == k {
					Println("!", j%n+1)
					return
				}
			}
		} else {
			for j := i - 1; ; j-- {
				Println("?", (j%n+n)%n+1)
				Scan(&v)
				if v == k {
					Println("!", (j%n+n)%n+1)
					return
				}
			}
		}
	}
}

package main

import (
	. "fmt"
	"math"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	gcd := func(a, b int) int {
		for a != 0 {
			a, b = b%a, a
		}
		return b
	}
	var n, k, v int
	Scan(&n, &k)
	step := int(math.Sqrt(float64(n)))
	for ; gcd(step, n) > 1; step++ {
	}
	for i := 0; ; i += step {
		Println("?", i%n+1)
		Scan(&v)
		if v == k {
			continue
		}
		if v < k {
			for j := i + 1; ; j++ {
				Println("?", j%n+1)
				Scan(&v)
				if v == k {
					Println("!", j%n+1)
					return
				}
			}
		} else {
			for j := i - 1; ; j-- {
				Println("?", (j%n+n)%n+1)
				Scan(&v)
				if v == k {
					Println("!", (j%n+n)%n+1)
					return
				}
			}
		}
	}
}

package main

import (
	"bufio"
	. "fmt"
	"math"
	"os"
)

type (
	input struct{ n, k int }
	req   struct{ i int }
	resp  struct{ v int }
	guess struct{ ans int }
)

// github.com/EndlessCheng/codeforces-go
func CF1470C(in input, Q func(req) resp) (gs guess) {
	q := func(i int) int { return Q(req{i}).v }
	n, k := in.n, in.k
	step := int(math.Sqrt(float64(n)))
	gcd := func(a, b int) int {
		for a != 0 {
			a, b = b%a, a
		}
		return b
	}
	for ; gcd(step, n) > 1; step++ {
	}
	for i := 0; ; i += step {
		v := q(i%n + 1)
		if v == k {
			continue
		}
		if v < k {
			for j := i + 1; ; j++ {
				v := q(j%n + 1)
				if v == k {
					gs.ans = j%n + 1
					return
				}
			}
		} else {
			for j := i - 1; ; j-- {
				v := q((j%n+n)%n + 1)
				if v == k {
					gs.ans = (j%n+n)%n + 1
					return
				}
			}
		}
	}
}

func ioq70() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)

	Q := func(req req) (resp resp) {
		Fprintln(out, "?", req.i)
		out.Flush()
		Fscan(in, &resp.v)
		return
	}

	d := input{}
	Fscan(in, &d.n, &d.k)
	gs := CF1470C(d, Q)
	Fprintln(out, "!", gs.ans)
	out.Flush()
}

func main() { ioq70() }

package main

import (
	. "fmt"
	"math"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	var n, k, v int
	Scan(&n, &k)
	step := int(math.Sqrt(float64(n)))
	for gcd(step, n) > 1 {
		step++
	}
	for i := 0; ; i += step {
		Println("?", i%n+1)
		Scan(&v)
		if v == k {
			continue
		}
		if v < k {
			for j := i + 1; ; j++ {
				Println("?", j%n+1)
				Scan(&v)
				if v == k {
					Println("!", j%n+1)
					return
				}
			}
		} else {
			for j := i - 1; ; j-- {
				Println("?", (j%n+n)%n+1)
				Scan(&v)
				if v == k {
					Println("!", (j%n+n)%n+1)
					return
				}
			}
		}
	}
}

func gcd(a, b int) int {
	for a != 0 {
		a, b = b%a, a
	}
	return b
}