// Arup Guha
// 3/5/2022
// Solution to 2021 SER D1/D2 Problem: Tree Hopping
// Uses my pre-written binary lifting code.

import java.util.*;
import java.io.*;

public class treehopping_alt {

	public static int n;
	public static int[] depth;
	public static ArrayList<Integer>[] kids;
	public static ArrayList<Integer>[] ancestors;

	public static void main(String[] args) throws Exception {

		// Get # of cases.
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		int nC = Integer.parseInt(stdin.readLine());
		
		// Store all answers here.
		StringBuffer sb = new StringBuffer();
		
		// Process cases.
		for (int loop=0; loop<nC; loop++) {
		
			// Set up tree.
			n = Integer.parseInt(stdin.readLine().trim());
			depth = new int[n];
			kids = new ArrayList[n];
			ancestors = new ArrayList[n];
			for (int i=0; i<n; i++) {
				kids[i] = new ArrayList<Integer>();
				ancestors[i] = new ArrayList<Integer>();
			}
			
			// Read in edges.
			for (int i=1; i<n; i++) {
				StringTokenizer tok = new StringTokenizer(stdin.readLine());
				int v1 = Integer.parseInt(tok.nextToken())-1;
				int v2 = Integer.parseInt(tok.nextToken())-1;
				kids[v1].add(v2);
				kids[v2].add(v1);
			}

			// Form our tree.
			makeTree(0, -1, 0);
			
			// Read in permutation.
			int[] perm = new int[n];
			for (int i=0; i<n; i++)
				perm[i] = Integer.parseInt(stdin.readLine())-1;

			int res = 1;

			// Check each pair of adjacent items in the permutation.
			for (int i=0; i<n-1; i++) {
				int dist = depth[perm[i]] + depth[perm[i+1]] - 2*depth[lca(perm[i], perm[i+1])];
				if (dist > 3) {
					res = 0;
					break;
				}
			}

			// Final answer.
			sb.append(res+"\n");
		}
		
		// Ta da!
		System.out.print(sb);
	}

	// Returns the lowest common ancestor of v1 and v2 in the tree.
	public static int lca(int v1, int v2) {

		// Make v1 further down than v2.
		if (depth[v1] < depth[v2]) {
			int tmp = v1;
			v1 = v2;
			v2 = tmp;
		}

		// Move v1 to same level as v2.
		if (depth[v1] != depth[v2]) v1 = hopUp(v1, depth[v1]-depth[v2]);

		// "Binary Search" the LCA using binary lifting.
		int maxjump = 1000;
		while (v1 != v2) {
			int shift = Math.min(maxjump, ancestors[v1].size()-1);
			while (shift > 0 && ancestors[v1].get(shift) == ancestors[v2].get(shift)) shift--;
			v1 = ancestors[v1].get(shift);
			v2 = ancestors[v2].get(shift);
		}
		return v1;
	}

	public static int hopUp(int node, int levels) {

		// Find smallest hop by bit.
		int shift = 0;
		int cur = node;

		// Hop until we get to the right place.
		while (levels > 0) {

			while (((1<<shift) & levels) == 0) shift++;

			// Hop up by 2^shift.
			cur = ancestors[cur].get(shift);
			levels -= (1<<shift);
			shift++;
		}

		// Ta da!
		return cur;
	}

	public static void makeTree(int root, int parent, int curDepth) {

		// Set up BFS.
		ArrayDeque<Integer> q = new ArrayDeque<Integer>();
		q.offer(root);
		int[] par = new int[n];
		par[0] = -1;
		depth[root] = 0;

		// Run BFS.
		while (q.size() > 0) {

			root = q.poll();

			// Handle root - put in depth and fill in binary lifting array.
			if (par[root] != -1) {
				ancestors[root].add(par[root]);
				int i = 1, ancestor = par[root];
				while (i-1 < ancestors[ancestor].size()) {
					ancestors[root].add(ancestors[ancestor].get(i-1));
					ancestor = ancestors[ancestor].get(i-1);
					i++;
				}
			}

			// Remove the parent from the kid list.
			kids[root].remove( (Integer)par[root] );

			// Enqueue the kids.
			for (int i=0; i<kids[root].size(); i++) {
				int next = kids[root].get(i);
				q.offer(next);
				par[next] = root;
				depth[next] = depth[root]+1;
			}
		}
	}
}