#include<bits/stdc++.h>
#define int long long
using namespace std;
int t, n, m;
vector<int> p[100001];
stack<int> st;
int low[100001], num[100001], dele[100001], cc = 0, scc = 0;
void nhap() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
p[a].push_back(b);
}
}
void dfs(int u) {
num[u] = low[u] = ++cc;
st.push(u);
for (int v : p[u]) {
if (dele[v]) continue;
if (!num[v]) {
dfs(v);
low[u] = min(low[u], low[v]);
} else
low[u] = min(low[u], num[v]);
}
if (num[u] == low[u]) {
scc++;
while (!st.empty()) {
int v = st.top();
st.pop();
dele[v] = scc;
if (v == u) break;
}
}
}
signed main() {
cin >> t;
while (t--) {
nhap();
cc = scc = 0;
fill(num, num + n + 1, 0);
fill(low, low + n + 1, 0);
fill(dele, dele + n + 1, 0);
for (int i = 1; i <= n; i++) {
if (!num[i]) dfs(i);
}
if (scc == 1) cout << "NO" << "\n";
else cout << "YES" << "\n";
for (int i = 1; i <= n; i++) {
p[i].clear();
}
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBpbnQgbG9uZyBsb25nCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgdCwgbiwgbTsKdmVjdG9yPGludD4gcFsxMDAwMDFdOwpzdGFjazxpbnQ+IHN0OwppbnQgbG93WzEwMDAwMV0sIG51bVsxMDAwMDFdLCBkZWxlWzEwMDAwMV0sIGNjID0gMCwgc2NjID0gMDsKCnZvaWQgbmhhcCgpIHsKICAgIGNpbiA+PiBuID4+IG07CiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBtOyBpKyspIHsKICAgICAgICBpbnQgYSwgYjsKICAgICAgICBjaW4gPj4gYSA+PiBiOwogICAgICAgIHBbYV0ucHVzaF9iYWNrKGIpOwogICAgfQp9Cgp2b2lkIGRmcyhpbnQgdSkgewogICAgbnVtW3VdID0gbG93W3VdID0gKytjYzsKICAgIHN0LnB1c2godSk7CiAgICBmb3IgKGludCB2IDogcFt1XSkgewogICAgICAgIGlmIChkZWxlW3ZdKSBjb250aW51ZTsKICAgICAgICBpZiAoIW51bVt2XSkgewogICAgICAgICAgICBkZnModik7CiAgICAgICAgICAgIGxvd1t1XSA9IG1pbihsb3dbdV0sIGxvd1t2XSk7CiAgICAgICAgfSBlbHNlCiAgICAgICAgICAgIGxvd1t1XSA9IG1pbihsb3dbdV0sIG51bVt2XSk7CiAgICB9CiAgICBpZiAobnVtW3VdID09IGxvd1t1XSkgewogICAgICAgIHNjYysrOwogICAgICAgIHdoaWxlICghc3QuZW1wdHkoKSkgewogICAgICAgICAgICBpbnQgdiA9IHN0LnRvcCgpOwogICAgICAgICAgICBzdC5wb3AoKTsKICAgICAgICAgICAgZGVsZVt2XSA9IHNjYzsKICAgICAgICAgICAgaWYgKHYgPT0gdSkgYnJlYWs7CiAgICAgICAgfQogICAgfQp9CgpzaWduZWQgbWFpbigpIHsKICAgIGNpbiA+PiB0OwogICAgd2hpbGUgKHQtLSkgewogICAgICAgIG5oYXAoKTsKICAgICAgICBjYyA9IHNjYyA9IDA7CiAgICAgICAgZmlsbChudW0sIG51bSArIG4gKyAxLCAwKTsKICAgICAgICBmaWxsKGxvdywgbG93ICsgbiArIDEsIDApOwogICAgICAgIGZpbGwoZGVsZSwgZGVsZSArIG4gKyAxLCAwKTsKICAgICAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyBpKyspIHsKICAgICAgICAgICAgaWYgKCFudW1baV0pIGRmcyhpKTsKICAgICAgICB9CiAgICAgICAgaWYgKHNjYyA9PSAxKSBjb3V0IDw8ICJOTyIgPDwgIlxuIjsKICAgICAgICBlbHNlIGNvdXQgPDwgIllFUyIgPDwgIlxuIjsKICAgICAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyBpKyspIHsKICAgICAgICAgICAgcFtpXS5jbGVhcigpOwogICAgICAgIH0KICAgIH0KfQo=