/*CODED BY mahmudulsakib2019
DATE:-06/3/2024;TIME:-07:39 pm
BANGALDESH , SYLHET*/
#include <iostream>
#include <vector>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
int T1, T2, N;
cin >> T1 >> T2 >> N;
vector<int> A(N), B(N);
for (int i = 0; i < N; ++i) cin >> A[i];
for (int i = 0; i < N; ++i) cin >> B[i];
int minTrain = min(T1, T2);
int minTime = 1e9;
for (int i = 0; i < N; ++i) {
if (B[i] > minTrain) {
minTime = min(minTime, max(A[i], N - 1 - A[i]));
}
}
if (minTime == 1e9) {
cout << -1 << endl;
} else {
cout << minTime << endl;
}
}
return 0;
}