#include <bits/stdc++.h>
using namespace std;
int sol[1003][61][61], aux[61][61];
int main() {
  int n, m, r;
  scanf("%d %d %d", &n, &m, &r);
  for (int i = 0; i < 1003; i++)
    for (int j = 0; j < n; j++)
      for (int k = 0; k < n; k++) sol[i][j][k] = 1000000009;
  for (int t = 0; t < m; t++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        scanf("%d", &aux[i][j]);
      }
    }
    for (int k = 0; k < n; k++) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          aux[i][j] = min(aux[i][j], aux[i][k] + aux[k][j]);
        }
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        sol[0][i][j] = min(sol[0][i][j], aux[i][j]);
      }
    }
  }
  for (int t = 0; t < 1002; t++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          sol[t + 1][i][j] = min(sol[t + 1][i][j], sol[t][i][k] + sol[0][k][j]);
        }
      }
    }
  }
  int s, t, k;
  while (r--) {
    scanf("%d %d %d", &s, &t, &k);
    printf("%d\n", sol[k][s - 1][t - 1]);
  }
  return 0;
}
