#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, count;
  vector<long long int> a1;
  long long int a, b, max, min;
  cin >> count;
  for (long long int j = 0; j < count; j++) {
    max = 0;
    min = 1000000000;
    cin >> n;
    for (long long int i = 0; i < n; i++) {
      cin >> a >> b;
      if (a > max) {
        max = a;
      }
      if (b < min) {
        min = b;
      }
    }
    a1.push_back(max - min);
  }
  for (long long int i = 0; i < a1.size(); i++) {
    if (a1[i] > 0) {
      cout << a1[i];
    } else {
      cout << "0";
    }
    cout << endl;
  }
  return 0;
}
