#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll INF = 1e16;
int T, n, m, k, a[5];
ll sum[(1<<5)];
int main() {
input(T);
while (T--) {
input(n); input(m); input(k);
for (int i = 0; i < (1<<k); i++) sum[i] = 0;
for (int i = 0; i < n; i++) {
int s; input(s);
for (int j = 0; j < k; j++) input(a[j]);
for (int x = 1; x < (1<<k); x++) {
ll t = s;
for (int y = 0; y < k; y++) {
if ((x>>y)&1) t += a[y];
else t -= a[y];
}
sum[x] = max(sum[x], t);
}
}
ll ans = -INF;
for (int i = 0; i < m; i++) {
int s; input(s);
for (int j = 0; j < k; j++) input(a[j]);
for (int x = 1; x < (1<<k); x++) {
ll t = s;
for (int y = 0; y < k; y++) {
if ((x>>y)&1) t -= a[y];
else t += a[y];
}
ans = max(ans, sum[x] + t);
}
}
printf("%lld\n", ans);
}
return 0;
}