#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int x, y, r = INT_MAX, l = INT_MIN;
    for (int i = 0; i < n; i++) {
      cin >> x >> y;
      l = max(l, x);
      r = min(r, y);
    }
    if (l > r)
      cout << abs(l - r) << endl;
    else
      cout << 0 << endl;
  }
}
