#include <bits/stdc++.h>
using namespace std;
int g[60][60][60];
int res[1001][60][60];
int main() {
  int n, m, r;
  memset(res, 124, sizeof(res));
  cin >> n >> m >> r;
  for (int c = 0; c < m; c++) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) cin >> g[c][i][j];
    for (int k = 0; k < n; k++)
      for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
          g[c][i][j] = min(g[c][i][j], g[c][i][k] + g[c][k][j]);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) res[0][i][j] = min(res[0][i][j], g[c][i][j]);
  }
  for (int p = 1; p <= 1000; p++)
    for (int k = 0; k < n; k++)
      for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
          res[p][i][j] = min(res[p][i][j], res[p - 1][i][k] + res[0][k][j]);
  for (int i = 0; i < r; i++) {
    int s, t, k;
    cin >> s >> t >> k;
    int r = 1000000000;
    for (int i = 0; i <= k; i++) r = min(r, res[i][s - 1][t - 1]);
    cout << r << endl;
  }
  return 0;
}
