#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int n;
    cin >> n;
    int mi = 1000 * 1000 * 1000 + 10;
    int mi2 = 0;
    int ma = 0;
    int ma2 = 0;
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      if (a > ma) {
        ma = a;
      }
      if (b < mi) {
        mi = b;
      }
    }
    if (ma > mi) {
      cout << ma - mi << endl;
    } else {
      cout << 0 << endl;
    }
  }
  return 0;
}
