#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define DEBUG(...) debug(#__VA_ARGS__, __VA_ARGS__)
#else
#define DEBUG(...) 6
#endif
template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) {return os << "(" << p.first << ", " << p.second << ")";}
template<typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type* = nullptr>
ostream& operator << (ostream &os, const C &c) {bool f = true; os << "["; for (const auto &x : c) {if (!f) os << ", "; f = false; os << x;} return os << "]";}
template<typename T> void debug(string s, T x) {cerr << "\033[1;35m" << s << "\033[0;32m = \033[33m" << x << "\033[0m\n";}
template<typename T, typename... Args> void debug(string s, T x, Args... args) {for (int i=0, b=0; i<(int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++; else
if (s[i] == ')' || s[i] == '}') b--; else if (s[i] == ',' && b == 0) {cerr << "\033[1;35m" << s.substr(0, i) << "\033[0;32m = \033[33m" << x << "\033[31m | "; debug(s.substr(s.find_first_not_of(' ', i + 1)), args...); break;}}
template<typename T>
struct BIT {
int n, lg;
vector<T> bit;
BIT(int _n) : n(_n), lg(__lg(n)), bit(n + 1) {}
BIT(const vector<T> &a) : n((int) a.size()), lg(__lg(n)), bit(n + 1) {
for (int i=1; i<=n; i++) {
bit[i] += a[i-1];
if (i + (i & -i) <= n)
bit[i+(i&-i)] += bit[i];
}
}
T query(int i) {
T ret = 0;
for (i++; i>0; i-=i&-i)
ret += bit[i];
return ret;
}
T query(int l, int r) {
return l > r ? 0 : query(r) - query(l - 1);
}
void update(int i, T val) {
for (i++; i<=n; i+=i&-i)
bit[i] += val;
}
int kth(T k) {
int ret = 0;
for (int i=lg; i>=0; i--) {
ret += 1 << i;
if (ret <= n && bit[ret] < k)
k -= bit[ret];
else
ret -= 1 << i;
}
return ret;
}
};
const int MAXC = 200;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<array<int, 4>> pts;
vector<int> compress;
for (int i=0; i<n; i++) {
int l, r, c;
cin >> l >> r >> c;
pts.push_back({l, -r, i, c});
pts.push_back({r, -l, i + n, c});
compress.push_back(l);
compress.push_back(r);
}
sort(compress.begin(), compress.end());
compress.erase(unique(compress.begin(), compress.end()), compress.end());
int o = (int) compress.size();
sort(pts.begin(), pts.end());
vector<vector<int>> dp(o + 1, vector<int>(MAXC + 1, -1e9));
BIT<int> bit(o);
dp[o][0] = 0;
int mx = -1;
for (auto [x, y, i, c] : pts) {
x = int(lower_bound(compress.begin(), compress.end(), x) - compress.begin());
y = int(lower_bound(compress.begin(), compress.end(), -y) - compress.begin());
vector<vector<int>> ndp(o + 1, vector<int>(MAXC + 1, -1e9));
vector<int> lazy(o + 1, -1e9);
for (int j=0; j<=o; j++) {
int above = bit.query(y, j - 1), nxt = bit.kth(bit.query(y) + 1);
for (int k=0; k<=MAXC; k++)
if (dp[j][k] >= 0) {
if (i < n) {
// regular point
if (y >= j) {
assert(k != 0);
// set to color k
if (c == 0 || c == k)
ndp[j][k] = max(ndp[j][k], dp[j][k]);
// set to different color
if (c == 0) {
if (nxt == o) {
if (y == mx)
ndp[o][0] = max(ndp[o][0], dp[j][k]);
else
lazy[y] = max(lazy[y], dp[j][k]);
} else {
ndp[nxt][k] = max(ndp[nxt][k], dp[j][k]);
}
} else if (c != k) {
if (nxt == o) {
if (y == mx)
ndp[o][0] = max(ndp[o][0], dp[j][k]);
else
ndp[y][c] = max(ndp[y][c], dp[j][k]);
} else {
ndp[nxt][k] = max(ndp[nxt][k], dp[j][k]);
}
}
} else {
// set to color k
if (k != 0 && (c == 0 || c == k)) {
if (above == 0)
ndp[y][k] = max(ndp[y][k], dp[j][k]);
else
ndp[j][k] = max(ndp[j][k], dp[j][k]);
}
// set to different color
if (j == o && y > mx) {
if (c == 0)
lazy[y] = max(lazy[y], dp[j][k]);
else
ndp[y][c] = max(ndp[y][c], dp[j][k]);
} else {
ndp[j][k] = max(ndp[j][k], dp[j][k]);
}
}
} else {
// query point
ndp[j][k] = max(ndp[j][k], dp[j][k] + (above == 0 && x >= j));
}
}
}
for (int j=0; j<=o; j++)
for (int k=0; k<=MAXC; k++) {
if (k > 0)
ndp[j][k] = max(ndp[j][k], lazy[j]);
dp[j][k] = ndp[j][k];
}
if (i < n) {
bit.update(y, 1);
mx = max(mx, y);
}
}
int ret = 0;
for (int j=0; j<=o; j++)
for (int k=0; k<=MAXC; k++)
ret = max(ret, dp[j][k]);
cout << ret << "\n";
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojaWZkZWYgTE9DQUwKI2RlZmluZSBERUJVRyguLi4pIGRlYnVnKCNfX1ZBX0FSR1NfXywgX19WQV9BUkdTX18pCiNlbHNlCiNkZWZpbmUgREVCVUcoLi4uKSA2CiNlbmRpZgoKdGVtcGxhdGU8dHlwZW5hbWUgVCwgdHlwZW5hbWUgUz4gb3N0cmVhbSYgb3BlcmF0b3IgPDwgKG9zdHJlYW0gJm9zLCBjb25zdCBwYWlyPFQsIFM+ICZwKSB7cmV0dXJuIG9zIDw8ICIoIiA8PCBwLmZpcnN0IDw8ICIsICIgPDwgcC5zZWNvbmQgPDwgIikiO30KdGVtcGxhdGU8dHlwZW5hbWUgQywgdHlwZW5hbWUgVCA9IGRlY2F5PGRlY2x0eXBlKCpiZWdpbihkZWNsdmFsPEM+KCkpKT4sIHR5cGVuYW1lIGVuYWJsZV9pZjwhaXNfc2FtZTxDLCBzdHJpbmc+Ojp2YWx1ZT46OnR5cGUqID0gbnVsbHB0cj4Kb3N0cmVhbSYgb3BlcmF0b3IgPDwgKG9zdHJlYW0gJm9zLCBjb25zdCBDICZjKSB7Ym9vbCBmID0gdHJ1ZTsgb3MgPDwgIlsiOyBmb3IgKGNvbnN0IGF1dG8gJnggOiBjKSB7aWYgKCFmKSBvcyA8PCAiLCAiOyBmID0gZmFsc2U7IG9zIDw8IHg7fSByZXR1cm4gb3MgP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AgICAgICAgICAgICAgICBiaXRbaSsoaSYtaSldICs9IGJpdFtpXTsKICAgICAgICB9CiAgICB9CgogICAgVCBxdWVyeShpbnQgaSkgewogICAgICAgIFQgcmV0ID0gMDsKICAgICAgICBmb3IgKGkrKzsgaT4wOyBpLT1pJi1pKQogICAgICAgICAgICByZXQgKz0gYml0W2ldOwogICAgICAgIHJldHVybiByZXQ7CiAgICB9CgogICAgVCBxdWVyeShpbnQgbCwgaW50IHIpIHsKICAgICAgICByZXR1cm4gbCA+IHIgPyAwIDogcXVlcnkocikgLSBxdWVyeShsIC0gMSk7CiAgICB9CgogICAgdm9pZCB1cGRhdGUoaW50IGksIFQgdmFsKSB7CiAgICAgICAgZm9yIChpKys7IGk8PW47IGkrPWkmLWkpCiAgICAgICAgICAgIGJpdFtpXSArPSB2YWw7CiAgICB9CgogICAgaW50IGt0aChUIGspIHsKICAgICAgICBpbnQgcmV0ID0gMDsKICAgICAgICBmb3IgKGludCBpPWxnOyBpPj0wOyBpLS0pIHsKICAgICAgICAgICAgcmV0ICs9IDEgPDwgaTsKICAgICAgICAgICAgaWYgKHJldCA8PSBuICYmIGJpdFtyZXRdIDwgaykKICAgICAgICAgICAgICAgIGsgLT0gYml0W3JldF07CiAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgIHJldCAtPSAxIDw8IGk7CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXQ7CiAgICB9Cn07Cgpjb25zdCBpbnQgTUFYQyA9IDIwMDsKCmludCBtYWluKCkgewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBjaW4udGllKE5VTEwpOwoKICAgIGludCB0OwogICAgY2luID4+IHQ7CiAgICB3aGlsZSAodC0tKSB7CiAgICAgICAgaW50IG47CiAgICAgICAgY2luID4+IG47CiAgICAgICAgdmVjdG9yPGFycmF5PGludCwgND4+IHB0czsKICAgICAgICB2ZWN0b3I8aW50PiBjb21wcmVzczsKICAgICAgICBmb3IgKGludCBpPTA7IGk8bjsgaSsrKSB7CiAgICAgICAgICAgIGludCBsLCByLCBjOwogICAgICAgICAgICBjaW4gPj4gbCA+PiByID4+IGM7CiAgICAgICAgICAgIHB0cy5wdXNoX2JhY2soe2wsIC1yLCBpLCBjfSk7CiAgICAgICAgICAgIHB0cy5wdXNoX2JhY2soe3IsIC1sLCBpICsgbiwgY30pOwogICAgICAgICAgICBjb21wcmVzcy5wdXNoX2JhY2sobCk7CiAgICAgICAgICAgIGNvbXByZXNzLnB1c2hfYmFjayhyKTsKICAgICAgICB9CgogICAgICAgIHNvcnQoY29tcHJlc3MuYmVnaW4oKSwgY29tcHJlc3MuZW5kKCkpOwogICAgICAgIGNvbXByZXNzLmVyYXNlKHVuaXF1ZShjb21wcmVzcy5iZWdpbigpLCBjb21wcmVzcy5lbmQoKSksIGNvbXByZXNzLmVuZCgpKTsKICAgICAgICBpbnQgbyA9IChpbnQpIGNvbXByZXNzLnNpemUoKTsKCiAgICAgICAgc29ydChwdHMuYmVnaW4oKSwgcHRzLmVuZCgpKTsKICAgICAgICB2ZWN0b3I8dmVjdG9yPGludD4+IGRwKG8gKyAxLCB2ZWN0b3I8aW50PihNQVhDICsgMSwgLTFlOSkpOwogICAgICAgIEJJVDxpbnQ+IGJpdChvKTsKICAgICAgICBkcFtvXVswXSA9IDA7CiAgICAgICAgaW50IG14ID0gLTE7CiAgICAgICAgZm9yIChhdXRvIFt4LCB5LCBpLCBjXSA6IHB0cykgewogICAgICAgICAgICB4ID0gaW50KGxvd2VyX2JvdW5kKGNvbXByZXNzLmJlZ2luKCksIGNvbXByZXNzLmVuZCgpLCB4KSAtIGNvbXByZXNzLmJlZ2luKCkpOwogICAgICAgICAgICB5ID0gaW50KGxvd2VyX2JvdW5kKGNvbXByZXNzLmJlZ2luKCksIGNvbXByZXNzLmVuZCgpLCAteSkgLSBjb21wcmVzcy5iZWdpbigpKTsKICAgICAgICAgICAgdmVjdG9yPHZlY3RvcjxpbnQ+PiBuZHAobyArIDEsIHZlY3RvcjxpbnQ+KE1BWEMgKyAxLCAtMWU5KSk7CiAgICAgICAgICAgIHZlY3RvcjxpbnQ+IGxhenkobyArIDEsIC0xZTkpOwogICAgICAgICAgICBmb3IgKGludCBqPTA7IGo8PW87IGorKykgewogICAgICAgICAgICAgICAgaW50IGFib3ZlID0gYml0LnF1ZXJ5KHksIGogLSAxKSwgbnh0ID0gYml0Lmt0aChiaXQucXVlcnkoeSkgKyAxKTsKICAgICAgICAgICAgICAgIGZvciAoaW50IGs9MDsgazw9TUFYQzsgaysrKQogICAgICAgICAgICAgICAgICAgIGlmIChkcFtqXVtrXSA+PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmIChpIDwgbikgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgLy8gcmVndWxhciBwb2ludAogICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHkgPj0gaikgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGFzc2VydChrICE9IDApOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC8vIHNldCB0byBjb2xvciBrCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGMgPT0gMCB8fCBjID09IGspCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIG5kcFtqXVtrXSA9IG1heChuZHBbal1ba10sIGRwW2pdW2tdKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAvLyBzZXQgdG8gZGlmZmVyZW50IGNvbG9yCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGMgPT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAobnh0ID09IG8pIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmICh5ID09IG14KQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIG5kcFtvXVswXSA9IG1heChuZHBbb11bMF0sIGRwW2pdW2tdKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBsYXp5W3ldID0gbWF4KGxhenlbeV0sIGRwW2pdW2tdKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIG5kcFtueHRdW2tdID0gbWF4KG5kcFtueHRdW2tdLCBkcFtqXVtrXSk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgaWYgKGMgIT0gaykgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAobnh0ID09IG8pIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmICh5ID09IG14KQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIG5kcFtvXVswXSA9IG1heChuZHBbb11bMF0sIGRwW2pdW2tdKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBuZHBbeV1bY10gPSBtYXgobmRwW3ldW2NdLCBkcFtqXVtrXSk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBuZHBbbnh0XVtrXSA9IG1heChuZHBbbnh0XVtrXSwgZHBbal1ba10pOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAvLyBzZXQgdG8gY29sb3IgawogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChrICE9IDAgJiYgKGMgPT0gMCB8fCBjID09IGspKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChhYm92ZSA9PSAwKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgbmRwW3ldW2tdID0gbWF4KG5kcFt5XVtrXSwgZHBbal1ba10pOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBuZHBbal1ba10gPSBtYXgobmRwW2pdW2tdLCBkcFtqXVtrXSk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC8vIHNldCB0byBkaWZmZXJlbnQgY29sb3IKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoaiA9PSBvICYmIHkgPiBteCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoYyA9PSAwKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgbGF6eVt5XSA9IG1heChsYXp5W3ldLCBkcFtqXVtrXSk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIG5kcFt5XVtjXSA9IG1heChuZHBbeV1bY10sIGRwW2pdW2tdKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBuZHBbal1ba10gPSBtYXgobmRwW2pdW2tdLCBkcFtqXVtrXSk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgLy8gcXVlcnkgcG9pbnQKICAgICAgICAgICAgICAgICAgICAgICAgICAgIG5kcFtqXVtrXSA9IG1heChuZHBbal1ba10sIGRwW2pdW2tdICsgKGFib3ZlID09IDAgJiYgeCA+PSBqKSk7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZm9yIChpbnQgaj0wOyBqPD1vOyBqKyspCiAgICAgICAgICAgICAgICBmb3IgKGludCBrPTA7IGs8PU1BWEM7IGsrKykgewogICAgICAgICAgICAgICAgICAgIGlmIChrID4gMCkKICAgICAgICAgICAgICAgICAgICAgICAgbmRwW2pdW2tdID0gbWF4KG5kcFtqXVtrXSwgbGF6eVtqXSk7CiAgICAgICAgICAgICAgICAgICAgZHBbal1ba10gPSBuZHBbal1ba107CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChpIDwgbikgewogICAgICAgICAgICAgICAgYml0LnVwZGF0ZSh5LCAxKTsKICAgICAgICAgICAgICAgIG14ID0gbWF4KG14LCB5KTsKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgaW50IHJldCA9IDA7CiAgICAgICAgZm9yIChpbnQgaj0wOyBqPD1vOyBqKyspCiAgICAgICAgICAgIGZvciAoaW50IGs9MDsgazw9TUFYQzsgaysrKQogICAgICAgICAgICAgICAgcmV0ID0gbWF4KHJldCwgZHBbal1ba10pOwogICAgICAgIGNvdXQgPDwgcmV0IDw8ICJcbiI7CiAgICB9CgogICAgcmV0dXJuIDA7Cn0=