//这里填你的代码^^
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int N = 510, M = 5210;
int h[N], e[M], ne[M], w[M], idx;
int n, m1, m2;
int dist[N], cnt[N];
bool st[N];
inline void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
inline bool spfa()
{
queue<int> q;
memset(cnt, 0, sizeof cnt);
memset(dist, 0x3f, sizeof dist);
memset(st, 0, sizeof st);
dist[1] = 0;
for (int i = 1; i <= n; i ++ )
{
q.push(i);
st[i] = true;
}
while (q.size())
{
auto t = q.front();
q.pop();
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.push(j);
st[j] = true;
}
}
}
}
return false;
}
inline void solve()
{
idx = 0;
cin >> n >> m1 >> m2;
memset(h, -1, sizeof h);
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;
}
int main()
{
cin.tie(nullptr) -> sync_with_stdio(0);
int t = 1;
cin >> t;
while (t -- ) solve();
return 0;
}
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~