#include <bits/stdc++.h>
using namespace std;
int a[62][62];
int dp[62][62][62];
int main() {
  int n, m, r;
  for (int i = 0; i < 62; i++)
    for (int j = 0; j < 62; j++)
      for (int k = 0; k < 62; k++) dp[i][j][k] = 1e9;
  cin >> n >> m >> r;
  while (m--) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        cin >> a[i][j];
      }
    for (int k = 0; k < n; k++)
      for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) {
          a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
          dp[1][i][j] = min(dp[1][i][j], a[i][j]);
        }
  }
  for (int t = 2; t < n + 1; t++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          dp[t][i][j] = min(dp[t][i][j], dp[t - 1][i][k] + dp[1][k][j]);
        }
      }
    }
  }
  int s, t, k;
  while (r--) {
    cin >> s >> t >> k;
    s--, t--;
    k++;
    if (k > n) k = n;
    cout << dp[k][s][t] << endl;
  }
  return 0;
}
