#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; ++i) {
    int n;
    cin >> n;
    long long min_r = 10e10;
    long long max_l = 0;
    for (int j = 0; j < n; ++j) {
      long long l, r;
      cin >> l >> r;
      if (l > max_l) {
        max_l = l;
      }
      if (r < min_r) {
        min_r = r;
      }
    }
    if (max_l - min_r < 0) {
      cout << 0 << '\n';
    } else {
      cout << max_l - min_r << '\n';
    }
  }
  return 0;
}
