#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <functional>

using namespace std;

void solve(int test_ind){
	int n;
	cin >> n;
	vector<int> a(n), b(n);
	for (int &i: a) cin >> i;
	for (int &i: b) cin >> i;
	map<int, int> mp;
	for (int i=0;i<n;++i) {
		if (a[i] > b[i]) {
			cout << -1 << endl;
			return;
		}
		if (mp.find(b[i]) != mp.end() && mp[b[i]] != a[i]) {
			cout << -1 << endl;
			return;
		}
		mp[b[i]] = a[i];
	}
	for (auto it = mp.begin();it!=mp.end();++it) {
		if (it == mp.begin()) continue;
		auto it2 = it;
		--it2;
		if (it2->first - it2->second > it->first - it->second || it->second < it2->second) {
			cout << -1 << endl;
			return;
		}
	}
	cout << prev(mp.end())->first - prev(mp.end())->second << endl;
}

int main(){
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	int t=1;
	cin >> t;
	for(int i=0;i<t;i++){
		solve(i);
	}
	return 0;
}
