//1 define
//2 注释在输出的字符中
//3 函数的判断
//4 分行符号
#include<algorithm>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define P(a,b,c) make_pair(a,make_pair(b,c))
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define CLR(vis) memset(vis,0,sizeof(vis))
#define MST(vis,pos) memset(vis,pos,sizeof(vis))
#define pb push_back
//#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef pair<int,pair<int,int> >pii;
typedef long long ll;
typedef unsigned long long ull;
const ll mod = 1000000007;
const int INF = 0x3f3f3f3f;
ll gcd(ll a, ll b) {
return b ? gcd(b, a%b) : a;
}
template<class T>inline void gmax(T &A, T B) {
(A<B)&&(A=B);//if(B>A)A=B;
}
template<class T>inline void gmin(T &A, T B) {
(A>B)&&(A=B);//if(B<A)A=B;
}
template <class T>
inline bool scan(T &ret) {
char c;
int sgn;
if(c=getchar(),c==EOF) return 0; //EOF
while(c!='-'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')?-1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
ret*=sgn;
return 1;
}
inline void outt(int x) {
if(x>9) outt(x/10);
putchar(x%10+'0');
}
const int N = 1e5+10;
int n,up[N],dw[N],d[N],u[N],b[N],sz[N];
int head[N],ver[N*2],nxt[N*2],tot;
void add(int x,int y) {
ver[++tot] = y;
nxt[tot] = head[x];
head[x] = tot;
}
void init() {
tot = 0;
CLR(head);
CLR(b);
CLR(dw);
}
bool dfs(int x,int fa) {
int down=0,UP=1;
for(int i=head[x]; i; i=nxt[i]) {
int y=ver[i];
if (y==fa) continue;
if (!dfs(y,x)) return 0;
down+=d[y];
UP+=u[y]; //累加所有儿子的上下界
}
d[x]=max(dw[x],down); //缩小上下界范围
u[x]=min(up[x],UP);
return d[x]<=u[x]; //如果矛盾,则当前枚举答案不可行
}
//求每个块的大小
void getsize(int x,int fa) {
sz[x]=1;
for(int i=head[x]; i; i=nxt[i]) {
int y=ver[i];
if (y==fa) continue;
getsize(y,x);
sz[x]+=sz[y];
}
}
bool check(int x) {
for(int i=1; i<=n; i++) {
up[i]=min(x-b[i],sz[i]);
if (up[i]<0) return 0; //x是二分产生,不保证x>=b[i]
}
return dfs(1,0)&&u[1]>=x&&d[1]<=x; //最后要求枚举的答案,在根的上下界范围之内
}
int main() {
int T_T;
cin>>T_T;
while(T_T--) {
init();
scanf("%d\n",&n);
for(int i=1; i<n; i++) {
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
int m,flag=0;
getsize(1,0);
scanf("%d",&m);
while(m--) {
int x,y;
scanf("%d%d",&x,&y);
dw[x]=max(dw[x],y);
if (y>sz[x]) flag=1;//对于一个结点可重复输入,下限取最大
}
scanf("%d",&m);
while(m--) {
int x,y;
scanf("%d%d",&x,&y);
b[x]=max(y,b[x]); //上限n-y取最小,故y取最大
if (y>n-sz[x]) flag=1;
}
if (flag) {
puts("-1");
continue;
}
int l=0,r=n,mid;
while(l<r){
mid=(l+r)/2;
if(check(mid))r=mid;
else l = mid+1;
}
printf("%d\n",l);
}
return 0;
}