#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 100005;

int n, k;

int main(){
	freopen("traverse.in", "r", stdin);
	freopen("traverse.out", "w", stdout);
	
	int c, T;  std:: scanf("%d%d", &c, &T);
	while(T --){
		//clear
		bool flag = 1;
		std:: scanf("%d%d", &n, &k);
		for(int i=1; i<=n-1; ++i){
			int u, v;  std:: scanf("%d%d", &u, &v);
			if(!(u-v==1 || u-v==-1))  flag = 0;
		}
		for(int i=1; i<=k; ++i) {
			int x;  std:: scanf("%d", &x);
		}
		if(flag == 0)  std:: printf("%d\n", n-1);
		else           std:: printf("1\n");
	}
	return 0;
}
