#include <bits/stdc++.h>
using namespace std;
long long int A[61][61][1001], B[61][61], n, m, r, s, t, k1, temp1, temp2;
int main() {
  cin >> n >> m >> r;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      A[i][j][0] = 1000001;
    }
  for (int i = 1; i <= m; i++) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) {
        cin >> B[i][j];
      }
    for (int k = 1; k <= n; k++)
      for (int x = 1; x <= n; x++)
        for (int y = 1; y <= n; y++) B[x][y] = min(B[x][y], B[x][k] + B[k][y]);
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) {
        if (B[i][j] < A[i][j][0]) A[i][j][0] = B[i][j];
      }
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) B[i][j] = A[i][j][0];
  for (int k = 1; k <= 1000; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        temp1 = A[i][j][k - 1];
        for (int l = 1; l <= n; l++) {
          temp2 = A[i][l][k - 1] + B[l][j];
          if (temp2 < temp1) temp1 = temp2;
        }
        A[i][j][k] = temp1;
      }
    }
  }
  for (int i = 1; i <= r; i++) {
    cin >> s >> t >> k1;
    cout << A[s][t][k1] << endl;
  }
}
