#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define T int
typedef pair<T, T> pii;
typedef tuple<T, T, T> tii;
#define fr first
#define sc second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define lowbit(x) ((x) & -(x))
#define all(x) (x).begin(), (x).end()
#define rep(i, x, y) for (int i = (x); i <= (y); ++i)
#define per(i, x, y) for (int i = (x); i >= (y); --i)
inline T rd() {
T x = 0;
bool f = 0;
char c = getchar();
for (; !isdigit(c); c = getchar()) f |= (c == '-');
for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
return f ? -x : x;
}
#define N 500007
pii a[N];
int mx[N];
set<int> s;
inline void work() {
s.clear();
int n = rd(), ans = 1e9;
rep(i, 1, n) a[i] = mp(rd(), rd());
sort(a + 1, a + 1 + n);
mx[n + 1] = 0;
per(i, n, 1) mx[i] = max(mx[i + 1], a[i].sc);
rep(i, 1, n) {
if (i > 1 && a[i].fr == a[i - 1].fr) s.insert(a[i].sc);
if (i != n) ans = min(ans, abs(mx[i + 1] - a[i].fr));
if (mx[i + 1] < a[i].fr && !s.empty()) {
if (s.lower_bound(a[i].fr) != s.end())
ans = min(ans, *s.lower_bound(a[i].fr) - a[i].fr);
if (s.lower_bound(a[i].fr) != s.begin())
ans = min(ans, a[i].fr - *--s.lower_bound(a[i].fr));
}
s.insert(a[i].sc);
}
printf("%d\n", ans);
}
int main() {
per(t, rd(), 1) work();
return 0;
}
230B - T-primes | 630A - Again Twenty Five |
1234D - Distinct Characters Queries | 1183A - Nearest Interesting Number |
1009E - Intercity Travelling | 1637B - MEX and Array |
224A - Parallelepiped | 964A - Splits |
1615A - Closing The Gap | 4C - Registration System |
1321A - Contest for Robots | 1451A - Subtract or Divide |
1B - Spreadsheet | 1177A - Digits Sequence (Easy Edition) |
1579A - Casimir's String Solitaire | 287B - Pipeline |
510A - Fox And Snake | 1520B - Ordinary Numbers |
1624A - Plus One on the Subset | 350A - TL |
1487A - Arena | 1520D - Same Differences |
376A - Lever | 1305A - Kuroni and the Gifts |
1609A - Divide and Multiply | 149B - Martian Clock |
205A - Little Elephant and Rozdil | 1609B - William the Vigilant |
978B - File Name | 1426B - Symmetric Matrix |