T半天竟然是引用耗时大 :)
#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;
const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;
#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))
#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
using ll=long long;
const int N=3e3+9;
int tc,n,m1,m2,s,t,ss,tt,hd[N],cur[N],flow[N],dep[N],sc[N],tot,totp,sum;
struct Edge{
int to,nxt,v;
}e[4*N];
inline void add(int u,int v,int w){
e[++tot]={v,hd[u],w},hd[u]=tot;
e[++tot]={u,hd[v],0},hd[v]=tot;
}
bool bfs(int st,int en){
for(int i=0;i<=totp;++i)dep[i]=0;
queue<int>q;
memcpy(cur,hd,sizeof(hd));
q.push(st);
dep[st]=1;
while(!q.empty()){
auto u=q.front();
q.pop();
for(int eg=hd[u];eg;eg=e[eg].nxt){
if(!dep[e[eg].to]&&e[eg].v>0){
dep[e[eg].to]=dep[u]+1;
q.push(e[eg].to);
}
}
}
return !!dep[en];
}
int dfs(int u,int en,int flow){
if(u==en)return flow;
int r=flow;
for(int eg=cur[u];eg&&r;eg=e[eg].nxt){
cur[u]=eg;
if(e[eg].v>0&&dep[e[eg].to]==dep[u]+1){
int c=dfs(e[eg].to,en,min(r,e[eg].v));
r-=c;
e[eg].v-=c,e[eg^1].v+=c;
}
}
return flow-r;
}
int dinic(int st,int en){
int ret=0;
while(bfs(st,en)){
ret+=dfs(st,en,0x3f3f3f3f);
}
return ret;
}
bool solve(){
tot=1,sum=0;
cin>>n>>m1>>m2;
s=n+m2+1,t=s+1,tt=t+1,totp=tt;
for(int i=0;i<=totp;++i)hd[i]=0,sc[i]=0,flow[i]=0;
for(int i=1,x,y,z;i<=m1;++i){
cin>>x>>y>>z;
if(z==1)sc[x]+=1;
else sc[y]+=1;
}
for(int i=1,x,y;i<=m2;++i){
cin>>x>>y;
if(x==1||y==1)sc[1]+=1;
else{
add(s,n+i,1);
flow[s]-=1,flow[n+i]+=1;
add(n+i,x,1),add(n+i,y,1);
}
}
for(int i=2;i<=n;++i)if(sc[i]>sc[1])return false;
for(int i=2;i<=n;++i){
add(i,t,sc[1]-sc[i]);
}
for(int i=n+1;i<tt;++i){
if(flow[i]>0){
sum+=flow[i];
add(ss,i,flow[i]);
}else if(flow[i]<0)add(i,tt,-flow[i]);
}
add(t,s,0x3f3f3f3f);
return sum==dinic(ss,tt);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>tc;
f(sb,1,tc){
cout<<(solve()?"YES\n":"NO\n");
}
return 0;
}