#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 1;
void solve() {
  int n;
  cin >> n;
  int l[n], r[n];
  int mil = INF, mar = -INF;
  for (int i = 0; i < n; i++) {
    cin >> l[i] >> r[i];
    if (mil > r[i]) mil = r[i];
    if (mar < l[i]) mar = l[i];
  }
  if (mar < mil)
    cout << 0;
  else
    cout << mar - mil;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
    cout << '\n';
  }
}
