#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define SZ(a) ((int)a.size())
#define pb push_back
#define ALL(v) v.begin(), v.end()
const int N = 33333 * 3 * 2 * 2 * 2 * 2;
struct SAT {
int low[N], dfn[N], bln[N], n, Time, nScc;
bool instack[N], istrue[N];
stack<int> st;
vector<int> G[N], SCC[n];
void init(int _n) {
n = _n, assert(n * 2 <= N);
for (int i = 0; i < n + n; ++i) G[i].clear();
}
void add_edge(int a, int b) { G[a].pb(b); }
int rv(int a) {
if (a >= n) return a - n;
return a + n;
}
void add_clause(int a, int b) {
add_edge(rv(a), b), add_edge(rv(b), a);
}
void dfs(int u) {
dfn[u] = low[u] = ++Time;
instack[u] = 1, st.push(u);
for (int i : G[u])
if (!dfn[i])
dfs(i), low[u] = min(low[i], low[u]);
else if (instack[i] && dfn[i] < dfn[u])
low[u] = min(low[u], dfn[i]);
if (low[u] == dfn[u]) {
int tmp;
do {
tmp = st.top(), st.pop();
instack[tmp] = 0, bln[tmp] = nScc;
} while (tmp != u);
++nScc;
}
}
bool solve() {
Time = nScc = 0;
for (int i = 0; i < n + n; ++i)
SCC[i].clear(), low[i] = dfn[i] = bln[i] = 0;
for (int i = 0; i < n + n; ++i)
if (!dfn[i]) dfs(i);
for (int i = 0; i < n + n; ++i) SCC[bln[i]].pb(i);
for (int i = 0; i < n; ++i) {
if (bln[i] == bln[i + n]) return false;
istrue[i] = bln[i] < bln[i + n];
istrue[i + n] = !istrue[i];
}
return true;
}
} sat;
int T[33333 * 3 * 4], F[33333 * 3 * 4], val[40000 * 3], tp;
int newnode() {
return tp++;
}
void build(int l, int r, int rt) {
if (l == r) return;
T[rt] = newnode(), F[rt] = newnode();
int mid = (l + r) >> 1;
build(l, mid, rt << 1), build(mid + 1, r, rt << 1 | 1);
}
void build_edge(int l, int r, int rt) {
if (l == r) return T[rt] = val[l], F[rt] = sat.rv(val[l]), void();
int mid = (l + r) >> 1;
build_edge(l, mid, rt << 1);
build_edge(mid + 1, r, rt << 1 | 1);
sat.add_clause(sat.rv(T[rt]), T[rt << 1]);
sat.add_clause(sat.rv(T[rt]), T[rt << 1 | 1]);
sat.add_clause(sat.rv(F[rt]), F[rt << 1]);
sat.add_clause(sat.rv(F[rt]), F[rt << 1 | 1]);
}
void add_edge(int L, int R, int l, int r, int rt, int v, int *tag) {
if (L <= l && R >= r)
return sat.add_caluse(sat.rv(v), tag[rt]);
int mid = (l + r) >> 1;
if (L <= mid) add_edge(L, R, l, mid, rt << 1, v, tag);
if (R > mid) add_edge(L, R, mid + 1, r, rt << 1 | 1, v, tag);
}
pii qry[100005];
void solve() {
int n, q;
cin >> n >> q;
tp = 0;
for (int i = 0; i < n * 3; ++i)
val[i] = newnode();
for (int i = 0; i < q; ++i) {
qry[i].X = newnode();
qry[i].Y = newnode();
}
build(0, 3 * n - 1, 1);
build_edge(0, 3 * n - 1, 1);
sat.init(tp);
for (int i = 1; i <= q; ++i) {
int a, b, c, d;
cin >> a >> b >> c >> d;
--a, --b, --c, --d;
add_edge(a, b, 0, 3 * n - 1, 1, qry[i].X, F);
add_edge(c, d, 0, 3 * n - 1, 1, qry[i].Y, T);
}
sat.solve();
cout << "NIE\n";
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
}