#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 2;
const int M = 1e5 + 2;
const int mod = 1e9 + 7;
const int inf = 1e9;
const long long INF = 1e18;
void data() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); }
int t, n;
pair<int, int> p[N];
int main() {
  data();
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    int ml = 0, mr = inf;
    for (int i = 0, l, r; i < n; ++i) {
      scanf("%d %d", &l, &r);
      ml = max(ml, l);
      mr = min(mr, r);
    }
    if (ml < mr)
      printf("0\n");
    else
      printf("%d\n", ml - mr);
  }
}
