#include <bits/stdc++.h>
const double eps = 1e-10;
using namespace std;
int n, a, b, c, d, i, j, m, r, to;
int s[770], t[770], k;
int road[770 / 10][770 / 10];
vector<int> gr[770];
int car[770][770];
int dp[770 / 10][770 / 10];
int f[770 / 10][770 / 10][770 / 10];
int over;
int main() {
  scanf("%d %d %d", &n, &m, &r);
  for (c = 1; c <= m; c++) {
    for (j = 1; j <= n; j++)
      for (d = 1; d <= n; d++) scanf("%d", &car[j][d]);
    for (i = 1; i <= n; i++)
      for (j = 1; j <= n; j++)
        for (k = 1; k <= n; k++)
          car[j][k] = min(car[j][k], car[j][i] + car[i][k]);
    for (i = 1; i <= n; i++)
      for (j = 1; j <= n; j++)
        if (c == 1 || dp[i][j] > car[i][j]) dp[i][j] = car[i][j];
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++)
      for (k = 0; k < n + 1; k++) f[i][k][j] = dp[i][j];
    for (k = 0; k < n; k++)
      for (j = 1; j <= n; j++)
        for (d = 1; d <= n; d++)
          f[i][k + 1][d] = min(f[i][k + 1][d], f[i][k][j] + dp[j][d]);
  }
  for (i = 0; i < r; i++) {
    scanf("%d %d %d", &a, &b, &c);
    printf("%d\n", f[a][min(c, n)][b]);
  }
  return 0;
  system("pause");
}
