#include <stdio.h>
#include <stdlib.h>

#define MAXN 10000
#define MAXM 100000

int P, N, M;
int V[MAXN+1], E[2*MAXM][2];

int ncomp; /* number of conected components */
int comp[MAXN+1];

int matched[MAXN+1];

int match(int c)
{
	int i, j, a;

	i=1;
	while (i<=N && (comp[i] != c || matched[i])) i++;
	if (i>N) return 1;

	a = 0;
	matched[i] = 1;
	for (j=V[i-1]; j<V[i]; j++) if (!matched[E[j][1]])
	{
		matched[E[j][1]] = 1;
		a += match(c);
		matched[E[j][1]] = 0;
	}
	matched[i] = 0;
	return a;
}

void conected(int v, int c)
{
	int j;
	comp[v] = c;
	for (j=V[v-1]; j<V[v]; j++)
		if (!comp[E[j][1]]) conected(E[j][1], c);
}

int cmp(const void *a, const void *b)
{
	return *((int (*)[2])a)[0] - *((int (*)[2])b)[0];
}

int main(void)
{
	int i, j, a, b;

	scanf("%d ", &P);
	while (P--)
	{
		scanf("%d %d ", &N, &M);
		for (i=1; i<=N; i++) scanf("%*d %*d ");
		for (i=0; i<M; i++)
		{
			scanf("%d %d ", &a, &b);
			E[2*i][0] = a; E[2*i][1] = b;
			E[2*i+1][0] = b; E[2*i+1][1] = a;
		}
		qsort(E, 2*M, sizeof(int[2]), cmp);

		V[0] = 0; j = 0;
		for (i=1; i<=N; i++)
		{
			while (j < 2*M && i == E[j][0]) j++;
			V[i] = j;
		}

		/* find conected components */
		ncomp = 0;
		for (i=1; i<=N; i++) comp[i] = 0;
		for (i=1; i<=N; i++) if (!comp[i])
		{
			ncomp++;
			conected(i, ncomp);
		}

		/* for each component count the number of matchings
		 * and multiply it together */
		a = 1;
		for (i=1; i<=N; i++) matched[i] = 0;
		for (j=1; j<=ncomp; j++)
		{
			a *= match(j);
			if (a == 0) break; /* unmatchable */
		}

		printf("%d\n", a);
	}

	return 0;
}
