#include #include #include using namespace std; int a[100010],l[100010],r[100010]; int main() { int cas; scanf("%d",&cas); while (cas--) { int n,cc=0; scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d%d",&l[i],&r[i]),a[++cc]=l[i],a[++cc]=r[i]; sort(a+1,a+1+cc); cc=unique(a+1,a+1+cc)-a-1; for (int i=1;i<=n;i++) { l[i]=lower_bound(a+1,a+1+cc,l[i])-a; r[i]=lower_bound(a+1,a+1+cc,r[i])-a; } long long Ans=1LL<<60; for (int i=1;i<=cc;i++) { int nowl=a[i],nowr=a[i]; long long ans=0; for (int j=1;j<=n;j++) { if (nowl>=a[l[j]]&&nowl<=a[r[j]]) { nowr=min(nowr,a[r[j]]); continue; } if (nowr>=a[l[j]]&&nowr<=a[r[j]]) { nowl=max(nowl,a[l[j]]); continue; } if (nowr