#include using namespace std; #define maxn 1002 #define inf 1000000005 int n,m,ans,F[maxn][4],c[maxn][4]; int A[maxn],cod[maxn],L[maxn],R[maxn],sz[maxn]; int dis(int x,int y) { return (abs(x-y)+1)>>1; } void solve() { scanf("%d",&n),ans=inf; for(int i=1;i<=n;i++) { scanf("%d %d",&L[i],&R[i]); sz[i]=0; c[i][sz[i]++]=L[i]; if(L[i]=1) { if(dn==1){ F[i][k]=0; break; } for(int j=0;j=L[dn-1]&&x<=R[dn-1]) --dn; else break; } } int l=-1,r=inf; for(int i=n;l<=r&&i>=1;i--) { for(int j=0;j=l&&c[i][j]<=r) ans=min(ans,F[i][j]); l=max(l,L[i]),r=min(r,R[i]); } printf("%d\n",ans); } int main() { int tmp; scanf("%d",&tmp); for(int Cas=tmp;Cas;--Cas) solve(); return 0; }