#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    set<int> s1, s2;
    int a, b;
    for (int i = 0; i < n; i++) {
      cin >> a >> b;
      s1.insert(a), s2.insert(b);
    }
    a = *(--s1.end());
    b = *s2.begin();
    if (a - b <= 0)
      cout << '0' << "\n";
    else
      cout << a - b << "\n";
  }
}
