/* !!;~, .,-:;;;;!!!!!!!!!!!!;!;;;!!!!!!;;;;!!!;;! !!;~, .-:;;!!!!!!!!!!!!!!;!!!!!!!!!!!!!!!!!!!! !!;~,. ,-:;;!!!!!!!!!!!;;;;;;!!!!!!!!!!!!!!!!!! **!:-. .,~;!!!!!!!!*!!;;;;;;;;;;;!!!!!!!!!!!!!!! **!;-,. ..~:;!!!!!***!!!;;;;;;;;;;;;!!*********!!! **!;-. .~;;!!!!****!!;;;;;;;;;;;;;;!**********!* **!;-. ,~;!!!!***!;!;;;;:;;;;;;;;;!**********!* **!:, .-;;!!!!!!!!;;;;;::::;::::;!!*********!* !;:~. .-~~:~:;!!!!;;;;;;;::;:;;;;!!*******!!!! !;:~, .-~~:::;!!!!;;!!;~;;;;;;;;!!!!********!! *!;:-. .,;;;!!!!!!!;;::;;.;;;!!!;!!!!!*=******** *!;:~,...,-:;;!!!*!!!;;:~;:.~;;!!!!!!!!!*=*=====** :~-,.. .,,-~~~!!!!!;:.-::.,;;!!!!!!!!!*=====*==* ,,. ....-!!!!!.~ ,:. ;;!!!!!!!!!*========= ,. .::::!*!!; . ;;!!;!!!!!!;::;!===== .. -~~~;!-:. ., -~- :!!!*!*:~~~:===== . -~~::!~: ,-,, -!!!*!*:~~~:===== . -~~~;!~:. .. :!!*!*:~~~:*==== . -~~:!!;: .-, ,!!**!:~~~~:==== -,,..,......~~~:*!!~ .~, !!**;~~~~--==== ,.. .,,,,,..-~~:!!*! -!:**;~~~~.-==== ..,---,,. -~~::!!!- .;~, :..!*;~~~~ ~===$ ,...,----,..-~~:~:**! .~~:: ,: -*!:~~~- ~===$ .. . ,,. -~~:*!**!~ ..,- .:-,**;~~~~- :==== . -~:;!!!*:;. ,,. ,:;!!**!~~:-~ ~!=== . -~~!!***;~-. . -:!****!;;~-~ , .~=== , , -~~!*=!::~-.,-..-:*****;;!~~.. .!=$ , , --~~:;:~:~- .~~:***;:::~~. ;=$ . -::::;~;:;- ..:!!!:!~--::, :== :. ,~ ~::;:;!~! .,,...;:~:~:::;;- ;== :- .~::::::::!!;- .~~...~;;;::::::- .;$$ -~. :::::::::;*!. .-*!,. :!!!::::::- -*$$ :~-----::::::::::!!..:***!, ;!*;::::::~. .:=$$ ;::::::::::;:::::!!.!=$;;$!.;!!;:::::::~-,. -*=$$ -::::::::;;;;;::::~!=$$::$$!~:;;::;;::::::~~;*$$$ .;::::::;;;;;;;::;~ =$$;;$$!~~;;;;;;;:::::::!=$$$ .:;;:::;;;;;;;;::, :=;;=. -:;;;;;;;::::::;*$$$$ ,;::;;;;;;;;;;;::, =*-;=. ::;;;;;;;;::::;;*$$$$ -::;:;;:;;;;;;::, .=; :=, ::;;;;;;;;::;;;;=$$$$ ...,:;:;;;;;;;;;:::, ;*- -*! :::;;;;;;;;;;:;!=$$$$ ,...--;;::;;;;;;;:~. -, .,- :::;;;;;;;;::;;;$$$$$ ~~~--~::*;;;;;!*;:~. ~!!!;;;;;;;:;!~;===== !!;;;;;;;;;;;!=*-::. :!;=*;;;;;;!:~~;=$$$$ =****!!!~!!;;!=*,,~, . . ::;:=*;;;;;;:~::;$$$$$ !!;;:~;;-!!;;;!*!;;. -~. :-!**!;;;;!;:~::;=$$$$ *!!;::!*:*!;;;;;;::. ... ,;-:;;;;;;!!:~~::;=$$$$ ===****=!*!;;;;::~~. . ;*!!:::;;;!;~~~::;$$$$$ ==========!;;;;::::, ::**;::;;;;:~~~::;$$$$$ ===$$$===*;;;;;:::::. ~:;;:::;;;;~~~~~:;$$$$$ $$$$$$===*;;;;;:::::. ~;;::::;;;;:~~~~:;=;:~, */ #include #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") // mt19937 engine((unsigned ll)time(NULL)); // uniform_int_distribution distribution(0, (ll)1e18); // auto generator = bind(distribution, engine); // ll x = generator(); // #include // #include // using namespace __gnu_pbds; // #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> // #define ordered_multiset tree, rb_tree_tag,tree_order_statistics_node_update> using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); return 0; } using ll = long long; using ull = unsigned long long; using ld = long double; using vi = std::vector; using vvi = std::vector; using vl = std::vector; using vii = std::vector>; using vvl = std::vector; using vll = std::vector>; using vd = std::vector; using vvd = std::vector; using vs = std::vector; using vvs = std::vector; using vb = std::vector; using vvb = std::vector; using vc = std::vector; using vvc = std::vector; using pii = std::pair; using pll = std::pair; using piil = std::pair, ll>; using mii = std::map; using mll = std::map; using pql = std::priority_queue; using pqi = std::priority_queue; using pqiil = std::priority_queue, ll>>; using pqii = std::priority_queue>; #define pb push_back #define ps push #define eb emplace_back #define is insert #define er erase #define f first #define s second #define lb lower_bound #define ub upper_bound #define sf(i) sizeof(i) #define endl "\n" #define all(v) (v).begin(), (v).end() #define rep(i, L, R) for (ll i = L; i <= R; i++) #define pcis precision #define sz(a) ((ll)a.size()) template struct infinity { static constexpr T max = std::numeric_limits::max(); static constexpr T min = std::numeric_limits::min(); static constexpr T value = std::numeric_limits::max() / 2; static constexpr T mvalue = std::numeric_limits::min() / 2; }; template constexpr T INF = infinity::value; constexpr ll lINF = INF; constexpr int iINF = INF; constexpr ld PI = 3.1415926535897932384626; void solve() { ll N, M; cin >> N >> M; ll A[N + 1]; rep(i, 1, N) cin >> A[i]; ll B[M + 1]; rep(i, 1, M) cin >> B[i]; ll C[N + 1]; rep(i, 1, N) cin >> C[i]; ll D[M + 1]; rep(i, 1, M) cin >> D[i]; ll S1[N + 1]; memset(S1, 0, sf(S1)); rep(i, 1, N) S1[i] = S1[i - 1] + A[i]; ll S2[M + 1]; memset(S2, 0, sf(S2)); rep(i, 1, M) S2[i] = S2[i - 1] + B[i]; vl lstA[N + M + 1]; vl lstB[N + M + 1]; rep(i, 1, N) lstA[C[i]].pb(i); rep(j, 1, M) lstB[D[j]].pb(j); ll K = N + M; ll RES = -1; auto calcanswer = [&](ll x, ll y) -> void { if (C[x] != D[y]) return; ll MN = min(min(N - x, x - 1), min(M - y, y - 1)); ll tmp = S1[x + MN] - S1[x - MN - 1] + S2[y + MN] - S2[y - MN - 1]; RES = max(RES, tmp); return; }; rep(i, 1, K) { if (lstA[i].empty() || lstB[i].empty()) continue; { ll at = -1; for (auto j : lstA[i]) if (j - 1 <= N - j) at = j; if (at != -1) { for (auto k : lstB[i]) calcanswer(at, k); } } { ll at = lINF / 2; for (auto j : lstA[i]) if (j - 1 >= N - j) at = min(at, j); if (at != lINF / 2) { for (auto k : lstB[i]) calcanswer(at, k); } } { ll at = -1; for (auto j : lstB[i]) if (j - 1 <= M - j) at = j; if (at != -1) { for (auto k : lstA[i]) calcanswer(k, at); } } { ll at = lINF / 2; for (auto j : lstB[i]) if (j - 1 >= M - j) at = min(at, j); if (at != lINF / 2) { for (auto k : lstA[i]) calcanswer(k, at); } } } cout << RES << endl; } void _main() { ll t; cin >> t; while (t--) solve(); }