#include<bits/stdc++.h>
using namespace std;
int t,n,cnt,m,k[1000001],head[1000001];
struct edge{
	int next;
	int id;
	int v;
}e[1000001];
void add(int u,int v){
	e[++cnt].v = v;
	e[cnt].id = u;
	e[cnt].next = head[u];
	head[u] = cnt;
}
int main(){
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	cin >> t;
	while(t--){
		cin >> n >> m;
		int flag = 1;
		for(int i = 1; i <= n; i++){
			int a,b;
			cin >> a >> b;
			if(a != i||b != i+1)	flag = 0;
			add(a,b);
			add(b,a);
		}
		for(int i = 1; i <= m; i++){
			cin >> k[i];
		}
		if(flag){
			cout << m <<endl;
			continue;
		}
		cout << n*m <<endl;
	}
	return 0;
}

