// 本题为考试单行多行输入输出规范示例,无需提交,不计分。
package Example;
import java.util.*;
/*
2
5 5 1
5 3 4 1 2
0 6 7 9 8
5 5 0
5 3 4 1 2
0 6 7 9 8
0
1
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while(T-- > 0){
int n = sc.nextInt(),m = sc.nextInt(),k = sc.nextInt();
long[] nums1 = new long[n];
long[] nums2 = new long[m];
for(int i = 0;i < n;++i) nums1[i] = sc.nextLong();
for(int i = 0;i < m;++i) nums2[i] = sc.nextLong();
Arrays.sort(nums1); Arrays.sort(nums2);
long t = k;
long t1 = Math.min(check(nums1,nums2,t),check(nums2,nums1,t));
long t2 = Math.min(check(nums1,nums2,-t),check(nums2,nums1,-t));
long res = Math.min(t1,t2);
System.out.println(res);
}
}
static long check(long[] nums1,long[] nums2,long t){
int n = nums1.length,m = nums2.length;
int p1 = 0,p2 = 0;
long res = Long.MAX_VALUE;
while(p1 < n && p2 < m){
long val = nums1[p1] - nums2[p2]-t;
if(val == 0) return 0;
else if(val > 0){
++p2;
res = Math.min(res,val);
} else ++p1;
}
return res;
}
}