#include<iostream>
#include<vector>
using namespace std;
typedef long long ll;
int main(){
int t;
cin >> t;
while(t--){
int n, m, k;
cin >> n >> m >> k;
vector<vector<ll> > v(n, vector<ll>(m));
vector<vector<ll> > dp(n, vector<ll>(m));
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> v[i][j];
dp[i][j] = v[i][j];
if(i > 0 && j > 0) dp[i][j] += min(dp[i - 1][j], dp[i][j - 1]);
else if(i > 0) dp[i][j] += dp[i - 1][j];
else if(j > 0) dp[i][j] += dp[i][j - 1];
}
for(int l = 0; l < m; l++){
vector<ll> temp(m, 1e17);
for(int j = 0; j < m; j++){
if(j > 0) temp[j] = min(v[i][(j + l)%m] + temp[j - 1], temp[j]);
if(i > 0) temp[j] = min(v[i][(j + l)%m] + dp[i - 1][j] + k*l, temp[j]);
else temp[j] = v[i][l] + k*l;
dp[i][j] = min(temp[j], dp[i][j]);
}
}
}
cout<<dp[n-1][m-1]<<endl;
}
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPHZlY3Rvcj4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdHlwZWRlZiBsb25nIGxvbmcgbGw7CgppbnQgbWFpbigpewogICAgaW50IHQ7CiAgICBjaW4gPj4gdDsKICAgIHdoaWxlKHQtLSl7CiAgICAgICAgaW50IG4sIG0sIGs7CiAgICAgICAgY2luID4+IG4gPj4gbSA+PiBrOwoKICAgICAgICB2ZWN0b3I8dmVjdG9yPGxsPiA+IHYobiwgdmVjdG9yPGxsPihtKSk7CiAgICAgICAgdmVjdG9yPHZlY3RvcjxsbD4gPiBkcChuLCB2ZWN0b3I8bGw+KG0pKTsKCiAgICAgICAgZm9yKGludCBpID0gMDsgaSA8IG47IGkrKyl7CiAgICAgICAgICAgIGZvcihpbnQgaiA9IDA7IGogPCBtOyBqKyspewogICAgICAgICAgICAgICAgY2luID4+IHZbaV1bal07CiAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgIGRwW2ldW2pdID0gdltpXVtqXTsKICAgICAgICAgICAgICAgIGlmKGkgPiAwICYmIGogPiAwKSBkcFtpXVtqXSArPSBtaW4oZHBbaSAtIDFdW2pdLCBkcFtpXVtqIC0gMV0pOwogICAgICAgICAgICAgICAgZWxzZSBpZihpID4gMCkgZHBbaV1bal0gKz0gZHBbaSAtIDFdW2pdOwogICAgICAgICAgICAgICAgZWxzZSBpZihqID4gMCkgZHBbaV1bal0gKz0gZHBbaV1baiAtIDFdOwogICAgICAgICAgICB9CiAgICAgICAgICAgIAogICAgICAgICAgICBmb3IoaW50IGwgPSAwOyBsIDwgbTsgbCsrKXsKICAgICAgICAgICAgICAgIHZlY3RvcjxsbD4gdGVtcChtLCAxZTE3KTsKICAgICAgICAgICAgICAgIGZvcihpbnQgaiA9IDA7IGogPCBtOyBqKyspewogICAgICAgICAgICAgICAgICAgIGlmKGogPiAwKSB0ZW1wW2pdID0gbWluKHZbaV1bKGogKyBsKSVtXSArIHRlbXBbaiAtIDFdLCB0ZW1wW2pdKTsKICAgICAgICAgICAgICAgICAgICBpZihpID4gMCkgdGVtcFtqXSA9IG1pbih2W2ldWyhqICsgbCklbV0gKyBkcFtpIC0gMV1bal0gKyBrKmwsIHRlbXBbal0pOwogICAgICAgICAgICAgICAgICAgIGVsc2UgdGVtcFtqXSA9IHZbaV1bbF0gKyBrKmw7CiAgICAgICAgICAgICAgICAgICAgZHBbaV1bal0gPSBtaW4odGVtcFtqXSwgZHBbaV1bal0pOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQoJCWNvdXQ8PGRwW24tMV1bbS0xXTw8ZW5kbDsKICAgIH0KfQ==
NQozIDMgMTAwCjMgNCA5CjUgMiA0CjAgMTAxIDEwMQozIDQgMQoxMCAwIDAgMTAKMCAwIDEwIDAKMTAgMTAgMCAxMAoxIDEgMwo0CjMgMiAzCjEgMgozIDYKNSA0CjEwIDEwIDE0CjU4IDQ5IDI1IDEyIDg5IDY5IDggNDkgNzEgMjMKNDUgMjcgNjUgNTkgMzYgMTAwIDczIDIzIDUgODQKODIgOTEgNTQgOTIgNTMgMTUgNDMgNDYgMTEgNjUKNjEgNjkgNzEgODcgNjcgNzIgNTEgNDIgNTUgODAKMSA2NCA4IDU0IDYxIDcwIDQ3IDEwMCA4NCA1MAo4NiA5MyA0MyA1MSA0NyAzNSA1NiAyMCAzMyA2MQoxMDAgNTkgNSA2OCAxNSA1NSA2OSA4IDggNjAKMzMgNjEgMjAgNzkgNjkgNTEgMjMgMjQgNTYgMjgKNjcgNzYgMyA2OSA1OCA3OSA3NSAxMCA2NSA2Mwo2IDY0IDczIDc5IDE3IDYyIDU1IDUzIDYxIDU4Cgo=
5
3 3 100
3 4 9
5 2 4
0 101 101
3 4 1
10 0 0 10
0 0 10 0
10 10 0 10
1 1 3
4
3 2 3
1 2
3 6
5 4
10 10 14
58 49 25 12 89 69 8 49 71 23
45 27 65 59 36 100 73 23 5 84
82 91 54 92 53 15 43 46 11 65
61 69 71 87 67 72 51 42 55 80
1 64 8 54 61 70 47 100 84 50
86 93 43 51 47 35 56 20 33 61
100 59 5 68 15 55 69 8 8 60
33 61 20 79 69 51 23 24 56 28
67 76 3 69 58 79 75 10 65 63
6 64 73 79 17 62 55 53 61 58