#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    long long l = 0, r = 1000000007;
    for (int i = 0; i < n; i++) {
      long long a, b;
      cin >> a >> b;
      l = max(l, a);
      r = min(r, b);
    }
    if (n != 1) {
      cout << (l > r ? (l - r) : 0) << endl;
    } else
      cout << 0 << endl;
  }
}
