#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
vector<pair<int, int>> s;
vector<vector<int>> dp;
vector<vector<int>> in;
int n;
int calc(int l, int r) {
  if (l > r) return 0;
  int &ret = dp[l][r];
  if (ret != -1) return ret;
  ret = calc(l + 1, r);
  for (int nr : in[l]) {
    if (nr >= r) break;
    ret = max(ret, calc(l, nr) + calc(nr + 1, r));
  }
  ret += count((in[l]).begin(), (in[l]).end(), r);
  return ret;
}
void solve() {
  cin >> n;
  s.clear();
  s.resize(n);
  vector<int> st;
  for (int i = 0; i < n; i++) {
    cin >> s[i].first >> s[i].second;
    st.push_back(s[i].first);
    st.push_back(s[i].second);
  }
  sort(st.begin(), st.end());
  st.resize(unique(st.begin(), st.end()) - st.begin());
  int cnt = st.size();
  dp.clear();
  in.clear();
  dp.resize(cnt + 1, vector<int>(cnt + 1, -1));
  in.resize(cnt + 1);
  for (int i = 0; i < s.size(); i++) {
    s[i].first = lower_bound(st.begin(), st.end(), s[i].first) - st.begin();
    s[i].second = lower_bound(st.begin(), st.end(), s[i].second) - st.begin();
    in[s[i].first].push_back(s[i].second);
  }
  for (int i = 0; i < cnt; i++) sort((in[i]).begin(), (in[i]).end());
  cout << calc(0, cnt - 1) << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
}

### Prompt

Please create a solution in CPP to the following problem:

You are given n segments on a coordinate axis OX. The i-th segment has borders [l_i; r_i]. All points x, for which l_i ≤ x ≤ r_i holds, belong to the i-th segment.

Your task is to choose the maximum by size (the number of segments) subset of the given set of segments such that each pair of segments in this subset either non-intersecting or one of them lies inside the other one.

Two segments [l_i; r_i] and [l_j; r_j] are non-intersecting if they have no common points. For example, segments [1; 2] and [3; 4], [1; 3] and [5; 5] are non-intersecting, while segments [1; 2] and [2; 3], [1; 2] and [2; 2] are intersecting.

The segment [l_i; r_i] lies inside the segment [l_j; r_j] if l_j ≤ l_i and r_i ≤ r_j. For example, segments [2; 2], [2, 3], [3; 4] and [2; 4] lie inside the segment [2; 4], while [2; 5] and [1; 4] are not.

You have to answer t independent test cases.

Input

The first line of the input contains one integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n (1 ≤ n ≤ 3000) — the number of segments. The next n lines describe segments. The i-th segment is given as two integers l_i and r_i (1 ≤ l_i ≤ r_i ≤ 2 ⋅ 10^5), where l_i is the left border of the i-th segment and r_i is the right border of the i-th segment.

Additional constraint on the input: there are no duplicates in the list of segments.

It is guaranteed that the sum of n does not exceed 3000 (∑ n ≤ 3000).

Output

For each test case, print the answer: the maximum possible size of the subset of the given set of segments such that each pair of segments in this subset either non-intersecting or one of them lies inside the other one.

Example

Input


4
4
1 5
2 4
2 3
3 4
5
1 5
2 3
2 5
3 5
2 2
3
1 3
2 4
2 3
7
1 10
2 8
2 5
3 4
4 4
6 8
7 7


Output


3
4
2
7

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
vector<pair<int, int>> s;
vector<vector<int>> dp;
vector<vector<int>> in;
int n;
int calc(int l, int r) {
  if (l > r) return 0;
  int &ret = dp[l][r];
  if (ret != -1) return ret;
  ret = calc(l + 1, r);
  for (int nr : in[l]) {
    if (nr >= r) break;
    ret = max(ret, calc(l, nr) + calc(nr + 1, r));
  }
  ret += count((in[l]).begin(), (in[l]).end(), r);
  return ret;
}
void solve() {
  cin >> n;
  s.clear();
  s.resize(n);
  vector<int> st;
  for (int i = 0; i < n; i++) {
    cin >> s[i].first >> s[i].second;
    st.push_back(s[i].first);
    st.push_back(s[i].second);
  }
  sort(st.begin(), st.end());
  st.resize(unique(st.begin(), st.end()) - st.begin());
  int cnt = st.size();
  dp.clear();
  in.clear();
  dp.resize(cnt + 1, vector<int>(cnt + 1, -1));
  in.resize(cnt + 1);
  for (int i = 0; i < s.size(); i++) {
    s[i].first = lower_bound(st.begin(), st.end(), s[i].first) - st.begin();
    s[i].second = lower_bound(st.begin(), st.end(), s[i].second) - st.begin();
    in[s[i].first].push_back(s[i].second);
  }
  for (int i = 0; i < cnt; i++) sort((in[i]).begin(), (in[i]).end());
  cout << calc(0, cnt - 1) << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
}

```