#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, n, maxstart, minend, tmp;
  cin >> t;
  int* res = new int[t];
  for (int i = 0; i < t; i++) {
    cin >> n;
    maxstart = 0;
    minend = numeric_limits<int>::max();
    for (int s = 0; s < n; s++) {
      cin >> tmp;
      if (tmp > maxstart) maxstart = tmp;
      cin >> tmp;
      if (tmp < minend) minend = tmp;
    }
    res[i] = (maxstart < minend ? 0 : maxstart - minend);
  }
  for (int i = 0; i < t; i++) cout << res[i] << endl;
  return 0;
}
