My solution :
include <bits/stdc++.h>
using namespace std;
define rep(i,a,b) for (int i = a; i < b; i++)
define ll long long
template istream& operator>>(istream& fin, vector& v) { for (auto it = v.begin(); it != v.end(); ++it) fin >> *it; return fin; } inline void solve(){ ll n;cin >> n; vector a(n),b(n); cin >> a >> b;
vector<ll> v = {a.front(),b.front(),a.back(),b.back()}; bool done[4] = {0}; ll ans = 0; rep(i,0,4){ if(done[i]){ continue; } if(i & 1){ ll mn = 1e10; for(auto j : a){ mn = min(mn,abs(j - v[i])); } ans += mn; rep(j,0,4){ if(!done[j] && mn == abs(v[j] - v[i])){ done[j] = 1; break; } } } else{ ll mn = 1e10; for(auto j : b){ mn = min(mn,abs(j - v[i])); } ans += mn; rep(j,0,4){ if(!done[j] && mn == abs(v[j] - v[i])){ done[j] = 1; break; } } } done[i] = 1; } cout << ans;
} int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; cin >> t; while (t--) { solve(); cout<<endl; } return 0; }