#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    long long minr = 1e9, maxl = 0;
    for (int i = 0; i < n; i++) {
      long long l, r;
      cin >> l >> r;
      minr = min(minr, r);
      maxl = max(l, maxl);
    }
    if (minr >= maxl)
      cout << 0 << endl;
    else
      cout << maxl - minr << endl;
  }
  return 0;
}
