program poj2528; type jd=record l,r,c,mid,ll,rr:longint; end; var a:array[0..300000] of longint; dr,dl:array[0..100000]of longint; e:array[0..10000000] of boolean; cc:array[1..200000] of boolean; i,j,k,l,m,n,t,task,ans,u:longint; tr:array[0..100000] of jd; procedure sort(l,r:longint); var x,y,i,j:longint; begin i:=l;j:=r;x:=a[random(r-l+1)+l]; repeat while a[i]<x do inc(i); while a[j]>x do dec(j); if i<=j then begin y:=a[i];a[i]:=a[j];a[j]:=y; inc(i);dec(j) end until i>j; if l<j then sort(l,j); if i<r then sort(i,r); end; {-----------------} procedure build(lj,rj:longint); begin with tr[t] do begin l:=lj;r:=rj; if l=r then exit; mid:=(l+r)shr 1; if mid=r then dec(mid); inc(t); ll:=t; build(l,mid); inc(t); rr:=t; build(mid+1,r); end; end; {-----------------} procedure cover(lj,rj,i,t:longint); var k:longint; begin if lj>rj then exit; with tr[i] do begin if (lj=l)and(rj=r) then begin c:=t; exit; end else begin if c<>0 then begin k:=c;c:=0; cover(l,lj-1,i,k); cover(rj+1,r,i,k); end; end; if l=r then exit; if rj<=mid then begin cover(lj,rj,ll,t); exit; end; if lj>mid then begin cover(lj,rj,rr,t); exit; end; if lj<=mid then cover(lj,mid,ll,t); if mid<rj then cover(mid+1,rj,rr,t); end; end; {-----------------} procedure find(i:longint); begin with tr[i] do begin if l=r then begin if c<>0 then begin if not cc[c] then inc(ans); cc[c]:=true; end; exit; end; if c=0 then begin find(ll);find(rr); end else begin if not cc[c] then inc(ans); cc[c]:=true; end; end; end; {-----------------} function ef(p:longint):longint; var l,r,mid:longint; begin l:=1;r:=u; while l<r do begin mid:=(l+r)shr 1; if a[mid]=p then exit(mid); if p>a[mid] then l:=mid+1; if p<a[mid] then r:=mid-1; end; if p=a[l] then exit(l) else exit(r); end; {-----------------} procedure init; begin fillchar(t,sizeof(t),0); fillchar(cc,sizeof(cc),0); fillchar(e,sizeof(e),false); ans:=0; l:=0; readln(n); u:=0; for i:=1 to n do begin readln(dl[i],dr[i]); if not e[dr[i]] then begin inc(u);a[u]:=dr[i]; end; if not e[dl[i]] then begin inc(u);a[u]:=dl[i]; end; e[dr[i]]:=true; e[dl[i]]:=true; end; sort(1,u); t:=1; build(1,u); t:=0; for i:=1 to n do begin inc(t); dl[i]:=ef(dl[i]); dr[i]:=ef(dr[i]); cover(dl[i],dr[i],1,t); end; find(1); end; {----------------} begin randomize; readln(task); for task:=1 to task do begin init; writeln(ans); end; end.