#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  for (int kk = 0; kk < t; ++kk) {
    int n;
    cin >> n;
    int max_beg = -1, min_end = 1e9 + 1;
    for (int i = 0; i < n; ++i) {
      int l, r;
      cin >> l >> r;
      max_beg = max(l, max_beg);
      min_end = min(r, min_end);
    }
    if (max_beg < min_end)
      cout << "0\n";
    else
      cout << max_beg - min_end << "\n";
  }
}
