#include <bits/stdc++.h>
using namespace std;
const int N = 61;
const int maxmax = N * 1000000;
int n, m, r, a[N][N][N], best[N][N], j, k, i, l, d[N][N][N];
void work() {
  for (l = 1; l <= m; l++)
    for (j = 1; j <= n; j++)
      for (k = 1; k <= n; k++)
        for (i = 1; i <= n; i++)
          a[l][k][i] = min(a[l][k][i], a[l][k][j] + a[l][j][i]);
  for (k = 1; k <= n; k++)
    for (i = 1; i <= n; i++) best[k][i] = maxmax;
  for (k = 1; k <= n; k++)
    for (i = 1; i <= n; i++)
      for (j = 1; j <= m; j++) best[k][i] = min(best[k][i], a[j][k][i]);
  for (k = 1; k <= n; k++)
    for (i = 1; i <= n; i++) {
      d[0][k][i] = best[k][i];
      for (l = 1; l <= n; l++) d[l][k][i] = maxmax;
    }
  for (l = 0; l < n; l++)
    for (k = 1; k <= n; k++)
      for (i = 1; i <= n; i++)
        for (j = 1; j <= n; j++)
          d[l + 1][k][i] = min(d[l + 1][k][i], d[l][k][j] + best[j][i]);
}
int main() {
  scanf("%d%d%d", &n, &m, &r);
  for (j = 1; j <= m; j++)
    for (k = 1; k <= n; k++)
      for (i = 1; i <= n; i++) scanf("%d", &a[j][k][i]);
  work();
  for (j = 1; j <= r; j++) {
    scanf("%d%d%d", &k, &i, &l);
    if (l > n) l = n;
    printf("%d\n", d[l][k][i]);
  }
}
