#include <bits/stdc++.h>
using namespace std;
int const N = 1e6 + 6;
bool us[N], use[N];
long long a[N];
long long n, x, k, q, x1, x2, y, ibibibibbibibibiibigbabypanfewewf, y2, ans, m;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> x1;
  for (int I = 0; I < x1; I++) {
    cin >> n;
    y = 0;
    ibibibibbibibibiibigbabypanfewewf = 1e9 + 7;
    for (int i = 1; i <= n; i++) {
      cin >> k >> q;
      y = max(y, k);
      ibibibibbibibibiibigbabypanfewewf =
          min(ibibibibbibibibiibigbabypanfewewf, q);
    }
    cout << max(0ll, y - ibibibibbibibibiibigbabypanfewewf) << endl;
  }
}
