```go
package main
import (
"bufio"
"fmt"
"os"
"sort"
)
type Segment struct {
c, l, r, id int
}
type ActiveSegment struct {
r, comp_id int
}
type DSU struct {
parent []int
size []int
}
func NewDSU(n int) *DSU {
dsu := &DSU{
parent: make([]int, n),
size: make([]int, n),
}
for i := 0; i < n; i++ {
dsu.parent[i] = i
dsu.size[i] = 1
}
return dsu
}
func (d *DSU) Find(i int) int {
if d.parent[i] == i {
return i
}
d.parent[i] = d.Find(d.parent[i])
return d.parent[i]
}
func (d *DSU) Union(i, j int) {
rootI := d.Find(i)
rootJ := d.Find(j)
if rootI != rootJ {
if d.size[rootI] < d.size[rootJ] {
rootI, rootJ = rootJ, rootI
}
d.parent[rootJ] = rootI
d.size[rootI] += d.size[rootJ]
}
}
func nextInt(reader *bufio.Reader) int {
var n int
var c byte
var err error
for {
c, err = reader.ReadByte()
if err != nil {
return 0
}
if c >= '0' && c <= '9' {
break
}
}
for {
n = n*10 + int(c-'0')
c, err = reader.ReadByte()
if err != nil || c < '0' || c > '9' {
break
}
}
return n
}
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
t := nextInt(reader)
if t == 0 {
return
}
for t > 0 {
t--
n := nextInt(reader)
segments := make([]Segment, n)
for i := 0; i < n; i++ {
segments[i].c = nextInt(reader)
segments[i].l = nextInt(reader)
segments[i].r = nextInt(reader)
segments[i].id = i
}
sort.Slice(segments, func(i, j int) bool {
if segments[i].l != segments[j].l {
return segments[i].l < segments[j].l
}
return segments[i].r < segments[j].r
})
dsu := NewDSU(n)
A := [2][]ActiveSegment{}
for i := 0; i < n; i++ {
s := segments[i]
otherColor := 1 - s.c
maxR := -1
for _, active := range A[otherColor] {
if active.r < s.l {
continue
}
dsu.Union(s.id, active.comp_id)
if active.r > maxR {
maxR = active.r
}
}
if maxR != -1 {
if cap(A[otherColor]) > 0 {
A[otherColor] = A[otherColor][:1]
A[otherColor][0] = ActiveSegment{r: maxR, comp_id: s.id}
} else {
A[otherColor] = []ActiveSegment{{r: maxR, comp_id: s.id}}
}
} else {
A[otherColor] = A[otherColor][:0]
}
A[s.c] = append(A[s.c], ActiveSegment{r: s.r, comp_id: s.id})
}
groups := 0
for i := 0; i < n; i++ {
if dsu.parent[i] == i {
groups++
}
}
fmt.Fprintln(writer, groups)
}
}
```