#include <bits/stdc++.h>
int value[61][61][61];
int dp[61][61][1010];
int mn[61][61];
int main() {
  int n, m, r, i, j, k, kk;
  scanf("%d%d%d", &n, &m, &r);
  for (k = 0; k < m; k++) {
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        scanf("%d", &value[k][i][j]);
      }
    }
  }
  for (k = 0; k < m; k++) {
    for (kk = 0; kk < n; kk++) {
      for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
          value[k][i][j] =
              ((value[k][i][j]) < (value[k][i][kk] + value[k][kk][j])
                   ? (value[k][i][j])
                   : (value[k][i][kk] + value[k][kk][j]));
        }
      }
    }
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      mn[i][j] = value[0][i][j];
      for (k = 0; k < m; k++) {
        mn[i][j] =
            ((mn[i][j]) < (value[k][i][j]) ? (mn[i][j]) : (value[k][i][j]));
      }
    }
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      dp[i][j][0] = mn[i][j];
    }
  }
  for (k = 1; k <= 1000; k++) {
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        dp[i][j][k] = dp[i][j][k - 1];
        for (kk = 0; kk < n; kk++) {
          dp[i][j][k] = ((dp[i][j][k]) < (dp[i][kk][k - 1] + mn[kk][j])
                             ? (dp[i][j][k])
                             : (dp[i][kk][k - 1] + mn[kk][j]));
        }
      }
    }
  }
  while (r--) {
    scanf("%d%d%d", &i, &j, &k);
    i--;
    j--;
    printf("%d\n", dp[i][j][k]);
  }
  return 0;
}
