#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+7;
const int N = 3000;
const int M = 2e5;
int head[N], deep[N], cur[N];
int w[M], to[M], nx[M];
int tot;
void add(int u, int v, int val){
w[tot] = val; to[tot] = v;
nx[tot] = head[u]; head[u] = tot++;
w[tot] = 0; to[tot] = u;
nx[tot] = head[v]; head[v] = tot++;
}
int bfs(int s, int t){
queue<int> q;
memset(deep, 0, sizeof(deep));
// for(int i = s; i <= t; ++i) deep[i]=0;
q.push(s);
deep[s] = 1;
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = nx[i]){
if(w[i] > 0 && deep[to[i]] == 0){
deep[to[i]] = deep[u] + 1;
q.push(to[i]);
}
}
}
return deep[t] > 0;
}
LL Dfs(int u, int t, LL flow){
if(u == t) return flow;
for(int &i = cur[u]; ~i; i = nx[i]){
if(deep[u]+1 == deep[to[i]] && w[i] > 0){
LL di = Dfs(to[i], t, min(1ll*w[i], flow));
if(di > 0){
w[i] -= di, w[i^1] += di;
return di;
}
}
}
return 0;
}
LL Dinic(int s, int t){
LL ans = 0, tmp;
while(bfs(s, t)){
for(int i = 0; i <= t; i++) cur[i] = head[i];
while(tmp = Dfs(s, t, inf)) ans += tmp;
}
return ans;
}
void init(){
memset(head, -1, sizeof(head));
tot = 0;
}
int main(){
int T;
scanf("%d", &T);
for(int _cas = 1; _cas <= T; ++_cas){
init();
int n, m, k, s, t;
scanf("%d%d%d", &n, &m, &k);
s = 0, t = n * m + 1;
// assert(t > 300);
for(int i = 0, v; i < n; ++i){
add(s, i*m+1, inf);
for(int j = 1; j <= m; ++j){
scanf("%d", &v);
if(j == m) add(i*m+j, t, 3000-v);
else add(i*m+j, i*m+j+1, 3000-v);
}
}
for(int i = 1, x,y,z; i <= k; ++i){
scanf("%d%d%d", &x, &y, &z);
for(int j = 1; j <= m; ++j){
if(j-z < 1) add(x*m-m+j, s, inf);
else if(j-z > m) add(x*m-m+j, t, inf);
else add((x-1)*m+j, (y-1)*m+j-z, inf);
}
}
LL ans = Dinic(s, t);
if(ans >= inf) puts("-1");
else printf("%lld\n", 3000*n-ans);
}
return 0;
}