#include <bits/stdc++.h>
using namespace std;
int n, m, r;
int timer[1001][61][61], each[61][61];
int main() {
  cin >> n >> m >> r;
  for (int i = 0; i <= 60; i++)
    for (int j = 0; j <= 60; j++) timer[0][i][j] = 99999999;
  for (int k = 0; k < m; k++) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) cin >> each[i][j];
    for (int t = 1; t <= n; t++)
      for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
          each[i][j] = min(each[i][j], each[i][t] + each[t][j]);
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        timer[0][i][j] = min(timer[0][i][j], each[i][j]);
  }
  for (int i = 1; i <= n; i++) {
    for (int u = 1; u <= n; u++)
      for (int v = 1; v <= n; v++) timer[i][u][v] = timer[i - 1][u][v];
    for (int k = 1; k <= n; k++)
      for (int u = 1; u <= n; u++)
        for (int v = 1; v <= n; v++)
          timer[i][u][v] =
              min(timer[i][u][v], timer[i - 1][u][k] + timer[0][k][v]);
  }
  while (r--) {
    int s, t, k;
    cin >> s >> t >> k;
    if (k > n) k = n;
    cout << timer[k][s][t] << endl;
  }
  return 0;
}
