```go
package main
import (
"bufio"
"fmt"
"os"
"sort"
)
type Segment struct {
l, r int
len int
weight int
}
func main() {
in := bufio.NewReader(os.Stdin)
out := bufio.NewWriter(os.Stdout)
defer out.Flush()
var t int
if _, err := fmt.Fscan(in, &t); err != nil {
return
}
for k := 0; k < t; k++ {
var n int
fmt.Fscan(in, &n)
// We allocate n+1 to include a dummy segment that covers all others
segs := make([]Segment, n+1)
for i := 0; i < n; i++ {
fmt.Fscan(in, &segs[i].l, &segs[i].r)
segs[i].len = segs[i].r - segs[i].l
}
// Dummy segment larger than any possible input segment
// Coordinates are up to 2*10^5, so [0, 300000] is sufficient
segs[n] = Segment{l: 0, r: 300000, len: 300000}
// Sort all segments by length increasing
// This ensures that when we process segment i, all segments inside it (which must be shorter)
// have already been processed.
sort.Slice(segs, func(i, j int) bool {
return segs[i].len < segs[j].len
})
// DP calculation
// segs[i].weight stores the max size of valid subset strictly inside segs[i] plus segs[i] itself.
for i := 0; i <= n; i++ {
// Find all segments strictly inside segs[i]
// Since sorted by length, we only need to check j < i
var cands []Segment
for j := 0; j < i; j++ {
if segs[i].l <= segs[j].l && segs[j].r <= segs[i].r {
cands = append(cands, segs[j])
}
}
if len(cands) == 0 {
segs[i].weight = 1
continue
}
// We need to select a subset of disjoint candidates with maximum total weight.
// This is the Weighted Interval Scheduling problem.
// Sort candidates by right endpoint
sort.Slice(cands, func(a, b int) bool {
return cands[a].r < cands[b].r
})
// dpWis[x] = max weight using a subset of the first x+1 candidates
dpWis := make([]int, len(cands))
dpWis[0] = cands[0].weight
for x := 1; x < len(cands); x++ {
// Option 1: Don't include the current candidate
exc := dpWis[x-1]
// Option 2: Include the current candidate
inc := cands[x].weight
// Find the latest non-overlapping candidate index < x
// We need a candidate y such that cands[y].r < cands[x].l
// Binary search finds the first index where cands[y].r >= cands[x].l
// The index immediately before that is compatible.
idx := sort.Search(x, func(y int) bool {
return cands[y].r >= cands[x].l
})
if idx > 0 {
inc += dpWis[idx-1]
}
if inc > exc {
dpWis[x] = inc
} else {
dpWis[x] = exc
}
}
segs[i].weight = 1 + dpWis[len(cands)-1]
}
// The answer is the weight of the dummy segment minus 1 (excluding the dummy itself)
// Since the dummy is the longest, it ends up at the last index.
fmt.Fprintln(out, segs[n].weight - 1)
}
}
```