///////////////////////////////////////////////////////////////////////////////////
//2351191 2010-11-17 00:58:08 Accepted 1025 C 10 216 VRS
//1025 木棍分堆 贪心法 分别通过了C++版本和C版本
//#include<stdlib.h>
/*#include<stdio.h>
//C/C++混合版
#include <string.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct STICK
{
int length;
int weight;
bool flags;
};
STICK stick[5005];
bool cmp(const STICK &a,const STICK &b)
{
if(a.length==b.length)
return a.weight<b.weight;
else
return a.length<b.length;
}
int main()
{
int testcase=0;
int sticknum;
int i,count;
int currenLoc,lastLoc;
scanf("%d",&testcase);
while(testcase--)
{
memset(stick,0,sizeof(stick));
scanf("%d",&sticknum);
for(i=0;i<sticknum;i++)
{
scanf("%d %d", &stick[i].length,&stick[i].weight);
stick[i].flags=0;
}
sort(stick,stick+sticknum,cmp);
count=0;
lastLoc=0;currenLoc=1;
while(1)
{
while(stick[lastLoc].flags==1 && lastLoc<sticknum) lastLoc++;
if(lastLoc<sticknum)
{
stick[lastLoc].flags=1;
count++;
}
else
break;
currenLoc=lastLoc;
while(currenLoc<sticknum)
{
while(stick[currenLoc].flags==1 && currenLoc<sticknum) currenLoc++;
if(currenLoc<sticknum && stick[lastLoc].weight<=stick[currenLoc].weight)
{
stick[currenLoc].flags=1;
lastLoc=currenLoc;
}
currenLoc++;
}
lastLoc=1;
}
printf("%d\n",count);
}
return 0;
}
*/
//C版本
#include<stdlib.h>
#include<stdio.h>
#include <string.h>
typedef struct _STICK
{
int length;
int weight;
int flags;
}STICK;
STICK stick[5005];
int cmp(const void *x,const void *y)
{
STICK *a=(STICK *)x;
STICK *b=(STICK *)y;
if(a->length==b->length)
return (a->weight)-(b->weight);
else
return (a->length)-(b->length);
}
int main()
{
int testcase=0;
int sticknum;
int i,count;
int currenLoc,lastLoc;
scanf("%d",&testcase);
while(testcase--)
{
memset(stick,0,sizeof(stick));
scanf("%d",&sticknum);
for(i=0;i<sticknum;i++)
{
scanf("%d %d", &stick[i].length,&stick[i].weight);
stick[i].flags=0;
}
qsort(stick,sticknum,sizeof(stick[0]),cmp);
count=0;
lastLoc=0;currenLoc=1;
while(1)
{
while(stick[lastLoc].flags==1 && lastLoc<sticknum) lastLoc++;
if(lastLoc<sticknum)
{
stick[lastLoc].flags=1;
count++;
}
else
break;
currenLoc=lastLoc;
while(currenLoc<sticknum)
{
while(stick[currenLoc].flags==1 && currenLoc<sticknum) currenLoc++;
if(currenLoc<sticknum && stick[lastLoc].weight<=stick[currenLoc].weight)
{
stick[currenLoc].flags=1;
lastLoc=currenLoc;
}
currenLoc++;
}
lastLoc=1;
}
printf("%d\n",count);
}
return 0;
}