#include #define pii pair using namespace std; int NL, NR; struct mazan{ int val, cost; bool operator<(const mazan &other)const{ return cost > other.cost; } }; vector vs[5001]; int lmatch[5001]; int rmatch[5001]; int visited[5001], iter; int a[5001], b[5001]; bool dfs(int u, int mini) { if(visited[u] < iter){ visited[u] = iter; for(auto v : vs[u]){ if(v.cost < mini) break; if(rmatch[v.val] == -1 || dfs(rmatch[v.val], mini)){ lmatch[u] = v.val; rmatch[v.val] = u; return true; } } } return false; } int maxmatching(int mini){ fill(lmatch, lmatch + NL, -1); fill(rmatch, rmatch + NR, -1); int m = 0, dm = 1; while(dm){ dm = 0; iter++; for(int u = 0; u < NL; u++) if(lmatch[u] == -1) dm += dfs(u, mini); m += dm; } return m; } void solve() { int n; cin >> n; NL = NR = n; for(int i = 0; i < n; i++) cin >> a[i]; for(int i = 0; i < n; i++) cin >> b[i]; for(int i = 0; i < n; i++) vs[i].clear(); for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) vs[i].push_back({j, abs(a[i] - b[j])}); for(int i = 0; i < n; i++) sort(vs[i].begin(), vs[i].end()); int r = -1, pas = 1 << 29; while(pas) { if(maxmatching(r + pas) == n) r += pas; pas /= 2; } cout << r << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifdef LOCAL freopen("test.in", "r", stdin); freopen("test.out", "w", stdout); #else #endif int T = 1; cin >> T; while(T--) { solve(); } return 0; }