#include <bits/stdc++.h>
using namespace std;
int dist[65][65][65], dp[65][65][65], best[65][65];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, r;
  cin >> n >> m >> r;
  swap(m, r);
  for (int k = 0; k < r; k++)
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        cin >> dist[k][i][j];
        best[i][j] = 1000000000;
      }
  for (int k = 0; k < r; k++)
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        for (int p = 0; p < n; p++) {
          dist[k][j][p] = min(dist[k][j][p], dist[k][j][i] + dist[k][i][p]);
          best[j][p] = min(best[j][p], dist[k][j][p]);
          dp[0][j][p] = best[j][p];
        }
  for (int k = 1; k < n; k++)
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        dp[k][i][j] = 1000000000;
        for (int p = 0; p < n; p++)
          dp[k][i][j] = min(dp[k - 1][i][p] + best[p][j], dp[k][i][j]);
      }
  while (m--) {
    int a, b, c;
    cin >> a >> b >> c;
    int ans = 1000000000;
    for (int i = 0; i <= min(c, n - 1); i++)
      ans = min(ans, dp[i][a - 1][b - 1]);
    cout << ans << "\n";
  }
}
