/*
nitesh kumar ( codeshaker )
*/
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <deque>
#include <vector>
#include <string>
#include <bitset>
#include <climits>
#include <complex>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef pair<pii,int> ppi;
typedef vector<pii> vpii;
typedef set<int> si;
typedef map<int,int> mii;
typedef map<string,int> msi;
#define PI (double)(3.141592653589793238)
#define inf 1<<30
#define eps 1e-9
#define mod LL(1e9+7)
#define pb push_back
struct stick
{
int l,w;
};
stick s[5005];
bool cmp1(const stick &a,const stick &b)
{
if(a.l!=b.l) return a.l<b.l;
return a.w<=b.w;
}
bool cmp2(const stick &a,const stick &b)
{
if(a.w!=b.w) return a.w<b.w;
return a.l<=b.l;
}
int main(void)
{
ios::sync_with_stdio(false);
int tc;
cin>>tc;
while(tc--)
{
int i,n;
cin>>n;
for(i=0;i<n;i++)
cin>>s[i].l>>s[i].w;
sort(s,s+n,cmp1);
int ans1=1;
for(i=1;i<n;i++)
{
if(s[i].w<s[i-1].w)
ans1++;
}
sort(s,s+n,cmp2);
int ans2=1;
for(i=1;i<n;i++)
{
if(s[i].l<s[i-1].l)
ans2++;
}
cout<<min(ans1,ans2)<<endl;
}
return 0;
}
LyoKICAgbml0ZXNoIGt1bWFyICggY29kZXNoYWtlciApCiovCgojaW5jbHVkZSA8Y21hdGg+CiNpbmNsdWRlIDxjc3RkaW8+CiNpbmNsdWRlIDxjc3RkbGliPgojaW5jbHVkZSA8Y3N0cmluZz4KI2luY2x1ZGUgPGNhc3NlcnQ+CiNpbmNsdWRlIDxmc3RyZWFtPgojaW5jbHVkZSA8aW9zdHJlYW0+CiNpbmNsdWRlIDxhbGdvcml0aG0+CiNpbmNsdWRlIDxtYXA+CiNpbmNs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IHN0aWNrICZhLGNvbnN0IHN0aWNrICZiKQp7CgogICBpZihhLnchPWIudykgcmV0dXJuIGEudzxiLnc7CiAgIHJldHVybiBhLmw8PWIubDsKfQppbnQgbWFpbih2b2lkKQp7CiAgaW9zOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogICBpbnQgdGM7CiAgIGNpbj4+dGM7CiAgIHdoaWxlKHRjLS0pCiAgIHsKICAgICAgaW50IGksbjsKICAgICAgY2luPj5uOwogICAgICBmb3IoaT0wO2k8bjtpKyspCiAgICAgIGNpbj4+c1tpXS5sPj5zW2ldLnc7CgogICAgICBzb3J0KHMscytuLGNtcDEpOwoKICAgICAgaW50IGFuczE9MTsKCiAgICAgIGZvcihpPTE7aTxuO2krKykKICAgICAgewogICAgICAgICBpZihzW2ldLnc8c1tpLTFdLncpCiAgICAgICAgIGFuczErKzsKICAgICAgfQogICAgICBzb3J0KHMscytuLGNtcDIpOwoKICAgICAgaW50IGFuczI9MTsKCiAgICAgIGZvcihpPTE7aTxuO2krKykKICAgICAgewogICAgICAgICBpZihzW2ldLmw8c1tpLTFdLmwpCiAgICAgICAgIGFuczIrKzsKICAgICAgfQoKICAgICAgY291dDw8bWluKGFuczEsYW5zMik8PGVuZGw7CiAgIH0KICAgcmV0dXJuIDA7Cgp9Cg==