#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  return x * f;
}
const int INF = 1e9;
int main() {
  int T = read();
  while (T--) {
    int N = read(), R = INF, L = -INF;
    for (int i = 1; i <= N; ++i) {
      int l = read(), r = read();
      R = min(R, r);
      L = max(L, l);
    }
    printf("%d\n", R <= L ? L - R : 0);
  }
  return 0;
}
