#include <bits/stdc++.h>   using namespace std;   void __print(int x) { cerr << x; }   void __print(long x) { cerr << x; }   void __print(long long x) { cerr << x; }   void __print(unsigned x) { cerr << x; }   void __print(unsigned long x) { cerr << x; }   void __print(unsigned long long x) { cerr << x; }   void __print(float x) { cerr << x; }   void __print(double x) { cerr << x; }   void __print(long double x) { cerr << x; }   void __print(char x) { cerr <<      << x <<     ; }   void __print(const char *x) { cerr <<     << x <<    ; }   void __print(const string &x) { cerr <<     << x <<    ; }   void __print(bool x) { cerr << (x ?  true  :  false ); }   template <typename T, typename V>   void __print(const pair<T, V> &x) {     cerr <<  { ;     __print(x.first);     cerr <<  , ;     __print(x.second);     cerr <<  } ;   }   template <typename T>   void __print(const T &x) {     int f = 0;     cerr <<  { ;     for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i);     cerr <<  } ;   }   void _print() { cerr <<  ] n ; }   template <typename T, typename... V>   void _print(T t, V... v) {     __print(t);     if (sizeof...(v)) cerr <<  ,  ;     _print(v...);   }   inline int GCD(int x, int y) {     if (x < y) swap(x, y);     if (x == 0) return y;     if (y == 0) return x;     return GCD(x % y, y);   }   int phi(int n) {     int result = n;     for (int i = 2; i * i <= n; i++) {       if (n % i == 0) {         while (n % i == 0) n /= i;         result -= result / i;       }     }     if (n > 1) result -= result / n;     return result;   }   int power(int x, int n, int mod) {     int res = 1;     x %= mod;     while (n) {       if (n & 1) {         res = ((res * x) % mod + mod) % mod;       }       x = ((x * x) % mod + mod) % mod;       n >>= 1;     }     return res;   }   inline bool func(const pair<int, int> &a, const pair<int, int> &b) {     if (a.second < b.second) return true;     if (a.second == b.second) {       if (a.first >= b.first) return true;       return false;     }     return false;   }   const int MAXN = 6e3 + 5;   int dp[MAXN][MAXN];   int SIZ, N;   vector<int> Rs[MAXN];   void clear() {     for (int i = 0; i <= SIZ; i++) {       for (int j = 0; j <= SIZ; j++) dp[i][j] = -1;       Rs[i].clear();     }   }   int solve(int l, int r) {     if (l > r) return 0;     if (dp[l][r] != -1) return dp[l][r];     int kitne = count(Rs[l].begin(), Rs[l].end(), r);     dp[l][r] = max(dp[l][r], kitne + (l + 1 > r ? 0 : solve(l + 1, r)));     for (int i = 0; i < Rs[l].size(); i++) {       int R = Rs[l][i];       if (R >= r) continue;       dp[l][r] = max(dp[l][r], kitne + solve(l, R) + solve(R + 1, r));     }     return dp[l][r];   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     int test;     cin >> test;     while (test--) {       cin >> N;       vector<int> v;       vector<int> L(N + 1), R(N + 1);       for (int i = 1; i <= N; i++) {         cin >> L[i] >> R[i];         v.push_back(L[i]);         v.push_back(R[i]);       }       sort(v.begin(), v.end());       v.resize(unique(v.begin(), v.end()) - v.begin());       for (int i = 1; i <= N; i++) {         L[i] = lower_bound(v.begin(), v.end(), L[i]) - v.begin();         R[i] = lower_bound(v.begin(), v.end(), R[i]) - v.begin();       }       SIZ = v.size() + 1;       clear();       for (int i = 1; i <= N; i++) {         Rs[L[i]].push_back(R[i]);       }       int ans = solve(0, SIZ);       cout << ans << endl;     }   }