#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    long long maxleft = 0, minright = 1e9 + 1;
    for (int j = 0; j < t; j++) {
      long long a, b;
      cin >> a >> b;
      if (a >= maxleft) {
        maxleft = a;
      }
      if (b <= minright) {
        minright = b;
      }
    }
    if ((t == 1) || (maxleft - minright < 0)) {
      cout << 0 << endl;
    } else {
      cout << maxleft - minright << endl;
    }
  }
}
