#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, t, l[100000], r[100000], ma, mi, i;
  cin >> t;
  while (t--) {
    cin >> n;
    for (i = 0; i < n; i++) {
      cin >> l[i] >> r[i];
    }
    if (n == 1) {
      cout << "0\n";
    } else {
      ma = l[0];
      for (i = 1; i < n; i++) {
        if (ma < l[i]) ma = l[i];
      }
      mi = r[0];
      for (i = 1; i < n; i++) {
        if (mi > r[i]) mi = r[i];
      }
      if (ma - mi <= 0)
        cout << "0" << endl;
      else
        cout << ma - mi << endl;
    }
  }
}
