#include<iostream>
#include<cstdio>
using namespace std;
int main()
{ int T,n;
cin>>T;
while(T--)
{ bool flag=0;
int a[100001]={0},b,c,m,i,j,k;
cin>>n;m=n;
for(i=0;i<n;i++)
{ scanf("%d %d",&b,&c);
for(j=b;j<=c&&j<=n;j++) a[j]+=1;
}
for(i=n;i>0;i--)
if(a[i]==i) {flag=1,cout<<i<<endl; break;}
if(flag==0) cout<<"-1"<<endl;
}
}