///////////////////////////////////////////////////////////////////////////////////////
//2351692 2010-11-17 16:53:46 Accepted 1029 C 0 164 VRS
//1029 房间移桌子 贪心法
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct _MOVING
{
int roomFrom;
int roomTo;
int flags;
}MOVING;
MOVING move[205];
int cmp(const void *x,const void *y)
{
MOVING *a=(MOVING *)x;
MOVING *b=(MOVING *)y;
return a->roomFrom-b->roomFrom;
}
int main()
{
int testCase,i;
int tempa,tempb;
int moveNum,lastLoc,currentLoc;
int unsoveNum,unsoveCurrent,unsoveCount;
int count;
int unsoveMove[205];
scanf("%d", &testCase);
while(testCase--)
{
count=0;
memset(move,0,sizeof(move));
scanf("%d", &moveNum);
for(i=0;i<moveNum;i++)
{
scanf("%d %d", &tempa, &tempb);
if(tempa<=tempb)
{
move[i].roomFrom=tempa;
move[i].roomTo=tempb;
}
else
{
move[i].roomFrom=tempb;
move[i].roomTo=tempa;
}
move[i].flags=0;
}
qsort(move,moveNum,sizeof(move[0]),cmp);
count=0;
lastLoc=0;currentLoc=1;
while(1)
{
while(move[lastLoc].flags==1 && lastLoc<moveNum) lastLoc++;
if(lastLoc<moveNum)
{
move[lastLoc].flags=1;
count+=10;
}
else
break;
currentLoc=lastLoc+1;
while(currentLoc<moveNum)
{
while(move[currentLoc].flags==1 && currentLoc<moveNum) currentLoc++;
if(currentLoc<moveNum
&& move[lastLoc].roomTo<move[currentLoc].roomFrom
&& !(move[lastLoc].roomTo%2==1 && move[lastLoc].roomTo==move[currentLoc].roomFrom-1)
)
{
move[currentLoc].flags=1;
lastLoc=currentLoc;
}
currentLoc++;
}
lastLoc=1;
}
printf("%d\n",count);
}
return 0;
}