#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long int n, i, j, ans;
    cin >> n;
    long long int l[n];
    long long int r[n];
    for (i = 0; i < n; i++) {
      cin >> l[i] >> r[i];
    }
    long long int lans = *max_element(l, l + n);
    long long int rans = *min_element(r, r + n);
    ans = lans - rans;
    if (ans > 0)
      std::cout << ans << std::endl;
    else
      cout << 0 << "\n";
  }
  return 0;
}
