from collections import deque, defaultdict, Counter from bisect import bisect_left, bisect_right, insort from itertools import permutations, combinations, groupby from heapq import heappop, heappush import math, sys input = lambda: sys.stdin.readline().rstrip("\r\n") def printl(li, sep=" "): print(sep.join(map(str, li))) def yn(flag): print(Yes if flag else No) _int = lambda x: int(x)-1 MOD = 998244353 #10**9+7 INF = 1<<60 Yes, No = "Yes", "No" for _ in range(int(input())): N, M = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) C = list(map(int, input().split())) D = list(map(int, input().split())) cumX = [0] for a in A: cumX.append(cumX[-1]+a) cumY = [0] for b in B: cumY.append(cumY[-1]+b) def check(m): colX = defaultdict(lambda:-INF) for i in range(m-1, N-m+1): val = cumX[i+m]-cumX[i-m+1] colX[C[i]] = max(colX[C[i]], val) colY = defaultdict(lambda:-INF) for i in range(m-1, M-m+1): val = cumY[i+m]-cumY[i-m+1] colY[D[i]] = max(colY[D[i]], val) ans = -1 for c in colX: x = colX[c] y = colY[c] ans = max(ans, x+y) return ans l, r = 0, N+1 while r-l>2: d = (r-l)//3 m1 = l+d m2 = l+d+d if check(m1) < check(m2): l = m1 else: r = m2 m = (l+r)//2 print(check(m))