#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 510, M = 5210;
bool st[N];
int cnt[N]; //判断是否有负环
int h[N], e[M], ne[M], idx, w[M];
int dist[N];
int q[N];
int n, m1, m2;
void add(int a, int b, int c)
{
w[idx] = c, e[idx] = b; ne[idx] = h[a], h[a] = idx ++;
}
bool spfa()
{
memset(dist, 0, sizeof dist);
memset(cnt, 0, sizeof cnt);
memset(st, 0, sizeof st);
int hh = 0, tt = 0;
for(int i = 1; i <= n; i ++)
{
q[tt ++] = i;
st[i] = true;
}
while(hh != tt)
{
int t = q[hh ++];
if(hh == N) hh = 0; //spfa最好用循环队列,因为不确定每个点要入队几次
st[t] = false;
for(int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
if(dist[j] > dist[t] + w[i])
{
dist[j] = dist[t] + w[i];
cnt[j] = cnt[t] + 1;
if(cnt[j] >= n) return true;
if(!st[j])
{
q[tt ++] = j;
if(tt == N ) tt = 0;
st[j] = true;
}
}
}
}
return false;
}
int main()
{
int T ;
cin >> T;
while(T --)
{
cin >> n >> m1 >> m2;
memset(h, -1, sizeof h);
idx = 0;
while(m1 --)
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, c), add(b, a, c);
}
while(m2 --)
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, -c); //注意虫洞是单向边
}
if(spfa()) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
//***************************************************
// 后记:spfa在判断负环的时候,如果数据范围过大,可能会超时
// 这时我们只需要略微修改,即可骗分通过
// 将spfa中的队列改为栈即可,而且不需要循环队列,只需在取元素时修改即可
// 修改代码:
// while(hh != tt)
// {
// int t = q[-- tt]; //这里修改,每次从栈顶取出元素
// st[t] = false;
// for(int i = h[t]; ~i; i = ne[i])
// {
// int j = e[i];
// if(dist[j] > dist[t] + w[i])
// {
// dist[j] = dist[t] + w[i];
// cnt[j] = cnt[t] + 1;
// if(cnt[j] >= n) return true;
// if(!st[j])
// {
// q[tt ++] = j;
// st[j] = true;
// }
// }
// }
// }