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 \le x \le 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 \le l_i$ and $r_i \le 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 \le t \le 1000$) — the number of test cases. Then $t$ test cases follow.

The first line of the test case contains one integer $n$ ($1 \le n \le 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 \le l_i \le r_i \le 2 \cdot 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$ ($\sum n \le 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
[{"code": "import sys\nfrom collections import deque\n\ninput=sys.stdin.readline\n\nt=1\nt=int(input())\nfor _ in range(t):\n    n=int(input())\n    val=set([0,2*10**5+1])\n    seg=[(0,2*10**5+1)]\n    for i in range(n):\n        l,r=list(map(int,input().split()))\n        val.add(l)\n        val.add(r)\n        seg.append((l,r))\n    val=list(val)\n    val.sort()\n    comp={i:e+1 for e,i in enumerate(val)}\n    for i in range(n+1):\n        l,r=seg[i]\n        seg[i]=(comp[l],comp[r])\n\n    deg=[0]*(n+1)\n    out=[[] for i in range(n+1)]\n    for i in range(n+1):\n        for j in range(i+1,n+1):\n            l,r=seg[i]\n            L,R=seg[j]\n            if L<=l and r<=R:\n                out[j].append(i)\n                deg[i]+=1\n            elif l<=L and R<=r:\n                out[i].append(j)\n                deg[j]+=1\n\n    ans=[0]\n    deq=deque(ans)\n\n    while deq:\n        v=deq.popleft()\n        for nv in out[v]:\n            deg[nv]-=1\n            if deg[nv]==0:\n                deq.append(nv)\n                ans.append(nv)\n\n    dp=[0]*(n+1)\n\n    def solve(v):\n        query=[[] for i in range(2*n+3)]\n        for nv in out[v]:\n            l,r=seg[nv]\n            query[r].append((l,dp[nv]))\n        subdp=[0]*(2*n+3)\n        for i in range(1,2*n+3):\n            res=subdp[i-1]\n            for l,val in query[i]:\n                test=subdp[l-1]+val\n                res=max(test,res)\n            subdp[i]=res\n\n        dp[v]=subdp[-1]+1\n\n    for v in ans[::-1]:\n        solve(v)\n\n    print(dp[0]-1)\n", "passed": true, "time": 0.14, "memory": 14264.0, "status": "done"}, {"code": "import sys\nfrom collections import deque\n\ninput=sys.stdin.readline\n\nt=1\nt=int(input())\nfor _ in range(t):\n    n=int(input())\n    val=set([0,2*10**5+1])\n    seg=[(0,2*10**5+1)]\n    for i in range(n):\n        l,r=map(int,input().split())\n        val.add(l)\n        val.add(r)\n        seg.append((l,r))\n    val=list(val)\n    val.sort()\n    comp={i:e+1 for e,i in enumerate(val)}\n    for i in range(n+1):\n        l,r=seg[i]\n        seg[i]=(comp[l],comp[r])\n\n    deg=[0]*(n+1)\n    out=[[] for i in range(n+1)]\n    for i in range(n+1):\n        for j in range(i+1,n+1):\n            l,r=seg[i]\n            L,R=seg[j]\n            if L<=l and r<=R:\n                out[j].append(i)\n                deg[i]+=1\n            elif l<=L and R<=r:\n                out[i].append(j)\n                deg[j]+=1\n\n    ans=[0]\n    deq=deque(ans)\n\n    while deq:\n        v=deq.popleft()\n        for nv in out[v]:\n            deg[nv]-=1\n            if deg[nv]==0:\n                deq.append(nv)\n                ans.append(nv)\n\n    dp=[0]*(n+1)\n\n    def solve(v):\n        query=[[] for i in range(2*n+3)]\n        for nv in out[v]:\n            l,r=seg[nv]\n            query[r].append((l,dp[nv]))\n        subdp=[0]*(2*n+3)\n        for i in range(1,2*n+3):\n            res=subdp[i-1]\n            for l,val in query[i]:\n                test=subdp[l-1]+val\n                res=max(test,res)\n            subdp[i]=res\n\n        dp[v]=subdp[-1]+1\n\n    for v in ans[::-1]:\n        solve(v)\n\n    print(dp[0]-1)", "passed": true, "time": 0.14, "memory": 14248.0, "status": "done"}, {"code": "import sys\nfrom collections import deque\n\nt=1\nfor _ in range(int(input())):\n    n=int(input())\n    val=set([0,2*10**5+1])\n    seg=[(0,2*10**5+1)]\n    for i in range(n):\n        l,r=map(int,input().split())\n        val.add(l)\n        val.add(r)\n        seg.append((l,r))\n    val=list(val)\n    val.sort()\n    comp={i:e+1 for e,i in enumerate(val)}\n    for i in range(n+1):\n        l,r=seg[i]\n        seg[i]=(comp[l],comp[r])\n\n    deg=[0]*(n+1)\n    out=[[] for i in range(n+1)]\n    for i in range(n+1):\n        for j in range(i+1,n+1):\n            l,r=seg[i]\n            L,R=seg[j]\n            if L<=l and r<=R:\n                out[j].append(i)\n                deg[i]+=1\n            elif l<=L and R<=r:\n                out[i].append(j)\n                deg[j]+=1\n\n    ans=[0]\n    deq=deque(ans)\n\n    while deq:\n        v=deq.popleft()\n        for nv in out[v]:\n            deg[nv]-=1\n            if deg[nv]==0:\n                deq.append(nv)\n                ans.append(nv)\n\n    dp=[0]*(n+1)\n\n    def solve(v):\n        query=[[] for i in range(2*n+3)]\n        for nv in out[v]:l,r=seg[nv];query[r].append((l,dp[nv]))\n        subdp=[0]*(2*n+3)\n        for i in range(1,2*n+3):\n            res=subdp[i-1]\n            for l,val in query[i]:test=subdp[l-1]+val;res=max(test,res)\n            subdp[i]=res\n\n        dp[v]=subdp[-1]+1\n\n    for v in ans[::-1]:solve(v)\n\n    print(dp[0]-1)", "passed": true, "time": 0.23, "memory": 14248.0, "status": "done"}, {"code": "from collections import deque\n\nt=1\nfor _ in range(int(input())):\n    n=int(input())\n    val=set([0,2*10**5+1])\n    seg=[(0,2*10**5+1)]\n    for i in range(n):\n        l,r=map(int,input().split())\n        val.add(l)\n        val.add(r)\n        seg.append((l,r))\n    val=list(val)\n    val.sort()\n    comp={i:e+1 for e,i in enumerate(val)}\n    for i in range(n+1):\n        l,r=seg[i]\n        seg[i]=(comp[l],comp[r])\n\n    deg=[0]*(n+1)\n    out=[[] for i in range(n+1)]\n    for i in range(n+1):\n        for j in range(i+1,n+1):\n            l,r=seg[i];L,R=seg[j]\n            if L<=l and r<=R:out[j].append(i);deg[i]+=1\n            elif l<=L and R<=r:out[i].append(j);deg[j]+=1\n\n    ans=[0]\n    deq=deque(ans)\n\n    while deq:\n        v=deq.popleft()\n        for nv in out[v]:\n            deg[nv]-=1\n            if deg[nv]==0:deq.append(nv);ans.append(nv)\n\n    dp=[0]*(n+1)\n\n    def solve(v):\n        query=[[] for i in range(2*n+3)]\n        for nv in out[v]:l,r=seg[nv];query[r].append((l,dp[nv]))\n        subdp=[0]*(2*n+3)\n        for i in range(1,2*n+3):\n            res=subdp[i-1]\n            for l,val in query[i]:test=subdp[l-1]+val;res=max(test,res)\n            subdp[i]=res\n\n        dp[v]=subdp[-1]+1\n\n    for v in ans[::-1]:solve(v)\n\n    print(dp[0]-1)", "passed": true, "time": 0.14, "memory": 14400.0, "status": "done"}, {"code": "from collections import deque\n\nt=1\nfor _ in range(int(input())):\n    n=int(input())\n    val=set([0,2*10**5+1])\n    seg=[(0,2*10**5+1)]\n    for i in range(n):l,r=map(int,input().split());val.add(l);val.add(r);seg.append((l,r))\n    val=sorted(list(val))\n    comp={i:e+1 for e,i in enumerate(val)}\n    for i in range(n+1):l,r=seg[i];seg[i]=(comp[l],comp[r])\n\n    deg=[0]*(n+1);out=[[] for i in range(n+1)]\n    for i in range(n+1):\n        for j in range(i+1,n+1):\n            l,r=seg[i];L,R=seg[j]\n            if L<=l and r<=R:out[j].append(i);deg[i]+=1\n            elif l<=L and R<=r:out[i].append(j);deg[j]+=1\n\n    ans=[0]\n    deq=deque(ans)\n\n    while deq:\n        v=deq.popleft()\n        for nv in out[v]:\n            deg[nv]-=1\n            if deg[nv]==0:deq.append(nv);ans.append(nv)\n\n    dp=[0]*(n+1)\n\n    def solve(v):\n        query=[[] for i in range(2*n+3)]\n        for nv in out[v]:l,r=seg[nv];query[r].append((l,dp[nv]))\n        subdp=[0]*(2*n+3)\n        for i in range(1,2*n+3):\n            res=subdp[i-1]\n            for l,val in query[i]:test=subdp[l-1]+val;res=max(test,res)\n            subdp[i]=res\n\n        dp[v]=subdp[-1]+1\n\n    for v in ans[::-1]:solve(v)\n\n    print(dp[0]-1)", "passed": true, "time": 0.18, "memory": 14464.0, "status": "done"}, {"code": "from collections import deque\n\nfor _ in range(int(input())):\n    n=int(input());val=set([0,2*10**5+1]);seg=[(0,2*10**5+1)]\n    for i in range(n):l,r=map(int,input().split());val.add(l);val.add(r);seg.append((l,r))\n    val=sorted(list(val));comp={i:e+1 for e,i in enumerate(val)}\n    for i in range(n+1):l,r=seg[i];seg[i]=(comp[l],comp[r])\n\n    deg=[0]*(n+1);out=[[] for i in range(n+1)]\n    for i in range(n+1):\n        for j in range(i+1,n+1):\n            l,r=seg[i];L,R=seg[j]\n            if L<=l and r<=R:out[j].append(i);deg[i]+=1\n            elif l<=L and R<=r:out[i].append(j);deg[j]+=1\n\n    ans=[0]\n    deq=deque(ans)\n\n    while deq:\n        v=deq.popleft()\n        for nv in out[v]:\n            deg[nv]-=1\n            if deg[nv]==0:deq.append(nv);ans.append(nv)\n\n    dp=[0]*(n+1)\n\n    def solve(v):\n        query=[[] for i in range(2*n+3)]\n        for nv in out[v]:l,r=seg[nv];query[r].append((l,dp[nv]))\n        subdp=[0]*(2*n+3)\n        for i in range(1,2*n+3):\n            res=subdp[i-1]\n            for l,val in query[i]:test=subdp[l-1]+val;res=max(test,res)\n            subdp[i]=res\n\n        dp[v]=subdp[-1]+1\n\n    for v in ans[::-1]:solve(v)\n\n    print(dp[0]-1)", "passed": true, "time": 0.23, "memory": 14200.0, "status": "done"}, {"code": "from collections import deque\nfor _ in range(int(input())):\n    n=int(input());val=set([0,2*10**5+1]);seg=[(0,2*10**5+1)]\n    for i in range(n):l,r=map(int,input().split());val.add(l);val.add(r);seg.append((l,r))\n    val=sorted(list(val));comp={i:e+1 for e,i in enumerate(val)};deg=[0]*(n+1);out=[[] for i in range(n+1)]\n    for i in range(n+1):l,r=seg[i];seg[i]=(comp[l],comp[r])    \n    for i in range(n+1):\n        for j in range(i+1,n+1):\n            l,r=seg[i];L,R=seg[j]\n            if L<=l and r<=R:out[j].append(i);deg[i]+=1\n            elif l<=L and R<=r:out[i].append(j);deg[j]+=1\n    ans=[0];deq=deque(ans);dp=[0]*(n+1)\n    while deq:\n        v=deq.popleft()\n        for nv in out[v]:\n            deg[nv]-=1\n            if deg[nv]==0:deq.append(nv);ans.append(nv)    \n    def solve(v):\n        query=[[] for i in range(2*n+3)];subdp=[0]*(2*n+3)\n        for nv in out[v]:l,r=seg[nv];query[r].append((l,dp[nv]))        \n        for i in range(1,2*n+3):\n            res=subdp[i-1]\n            for l,val in query[i]:test=subdp[l-1]+val;res=max(test,res)\n            subdp[i]=res\n        dp[v]=subdp[-1]+1\n    for v in ans[::-1]:solve(v)\n    print(dp[0]-1)", "passed": true, "time": 0.14, "memory": 14236.0, "status": "done"}, {"code": "# Fast IO (only use in integer input)\n\n# import os,io\n# input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline\n\nt = int(input())\nfor _ in range(t):\n    n = int(input())\n    pointList = []\n    pointOrderDict = {}\n    interval = [] # (l,r) tuple\n    intervalOrder = [] # interval list compressed as an order\n    for _ in range(n):\n        l,r = map(int,input().split())\n        pointList.append(l)\n        pointList.append(r)\n        interval.append((l,r))\n    pointList.sort()\n    cnt = 0\n    for i in range(2 * n):\n        if i == 0 or pointList[i] != pointList[i-1]:\n            pointOrderDict[pointList[i]] = cnt\n            cnt += 1\n\n    for elem in interval:\n        intervalOrder.append((pointOrderDict[elem[0]],pointOrderDict[elem[1]]))\n    \n    intervalList = []\n    dp = []\n\n    for i in range(cnt):\n        dp.append([])\n        intervalList.append([])\n        for j in range(cnt):\n            dp[i].append(-1)\n\n    for elem in intervalOrder:\n        intervalList[elem[0]].append(elem[1])\n\n    for i in range(cnt): # r - l\n        for j in range(cnt - i): # l\n            ans1 = 0 # is there [l,r]\n            ans2 = 0 # max of [l+1,r] and [l,nr] + [nr + 1,r]\n            if i != 0:\n                ans2 = dp[j + 1][i + j]\n            for elem in intervalList[j]:\n                if elem == i + j:\n                    ans1 += 1\n                elif elem < i + j and ans2 < dp[j][elem] + dp[elem + 1][i + j]:\n                    ans2 = dp[j][elem] + dp[elem + 1][i + j]\n            dp[j][i+j] = ans1 + ans2\n    \n    print(dp[0][cnt - 1])", "passed": true, "time": 0.21, "memory": 14204.0, "status": "done"}, {"code": "import sys\nfor _ in range(int(input())):\n    M = int(input())\n    LR_raw = []\n    val = set()\n    for _ in range(M):\n        l, r = list(map(int, input().split()))\n        LR_raw.append((l, r))\n        val.add(l)\n        val.add(r)\n    val = sorted(list(val))\n    val2idx = {x: i for i, x in enumerate(val)}\n    LR = []\n    N = len(val)\n    segment = [set() for _ in range(N)]\n    for l_, r_ in LR_raw:\n        l = val2idx[l_]\n        r = val2idx[r_]\n        LR.append((l, r))\n        segment[l].add(r)\n \n    dp = [[0] * N for _ in range(N)]\n    for d in range(1, N+1):\n        for l in range(N):\n            r = l + d - 1\n            if r < N:\n                if l+1 <= r:\n                    dp[l][r] = dp[l+1][r]\n                for rr in segment[l]:\n                    if rr >= r:\n                        continue\n                    dp[l][r] = max(dp[l][r], dp[l][rr] + dp[rr+1][r])\n                if r in segment[l]:\n                    dp[l][r] += 1\n    print(dp[0][-1])\n \n", "passed": true, "time": 0.14, "memory": 14344.0, "status": "done"}, {"code": "import sys\ndef rs(): return sys.stdin.readline().rstrip()\ndef ri(): return int(sys.stdin.readline())\ndef ria(): return list(map(int, sys.stdin.readline().split()))\ndef ws(s): sys.stdout.write(s); sys.stdout.write('\\n')\ndef wi(n): sys.stdout.write(str(n)); sys.stdout.write('\\n')\ndef wia(a, sep=' '): sys.stdout.write(sep.join([str(x) for x in a])); sys.stdout.write('\\n')\n\n\ndef solve(n, segs):\n    vals = set()\n    for l, r in segs:\n        vals.add(l)\n        vals.add(r)\n    vals = sorted(list(vals))\n    d = {x: i for i, x in enumerate(vals)}\n    m = len(vals)\n\n    c_segs = []\n    r_segs = [[] for _ in range(m)]\n    for l, r in segs:\n        ll = d[l]\n        rr = d[r]\n        c_segs.append((ll, rr))\n        r_segs[ll].append(rr)\n\n    dp = [[0] * m for _ in range(m)]\n    for ln in range(1, m + 1):\n        for l in range(m):\n            r = l + ln - 1\n            if r >= m: continue\n            if l + 1 <= r:\n                dp[l][r] = dp[l + 1][r]\n            for rr in r_segs[l]:\n                if rr >= r:\n                    continue\n                dp[l][r] = max(dp[l][r], dp[l][rr] + dp[rr + 1][r])\n            if r in r_segs[l]:\n                dp[l][r] += 1\n\n    return dp[0][-1]\n\n\ndef main():\n    for _ in range(ri()):\n        n = ri()\n        segs = []\n        for i in range(n):\n            l, r = ria()\n            segs.append((l, r))\n        wi(solve(n, segs))\n\n\ndef __starting_point():\n    main()\n\n__starting_point()", "passed": true, "time": 0.14, "memory": 14272.0, "status": "done"}, {"code": "from bisect import bisect_left as lower\nimport sys\ninput = sys.stdin.readline\n\ndef put():\n    return list(map(int, input().split()))\n\ndef func(size,seg):\n    dp = [[0]*size for i in range(size)]\n    for k in range(1, size+1):\n        for l in range(size):\n            r = l+k-1\n            if r<size:\n                if l+1<=r:\n                    dp[l][r] = dp[l+1][r]\n                same = 0\n                for i in seg[l]:\n                    if i==r:\n                        same=1\n                    if i<r:\n                        dp[l][r] = max(dp[l][r], dp[l][i]+ dp[i+1][r])\n                dp[l][r]+=same\n    return dp[0][-1]\n\n\ndef solve():\n    t = int(input())\n    for _ in range(t):\n        n = int(input())\n        l,r,m = [],[],set()\n        for i in range(n):\n            x,y = put()\n            l.append(x);r.append(y)\n            m.add(x);m.add(y)\n        m = sorted(m)\n        size = len(m)\n        seg = [[] for i in range(size)]\n        for i in range(n):\n            l[i] = lower(m, l[i])\n            r[i] = lower(m, r[i])\n            seg[l[i]].append(r[i])\n        print(func(size, seg))\n\nsolve()\n", "passed": true, "time": 0.14, "memory": 14108.0, "status": "done"}]