#include <bits/stdc++.h>
using namespace std;
long long a[66][66][66], dp[62][62][62], n, m, r;
int main() {
  for (long long i = 0; i < 62; ++i) {
    for (long long j = 0; j < 62; ++j) {
      for (long long z = 0; z < 62; ++z) dp[i][j][z] = INT_MAX;
    }
  }
  cin >> n >> m >> r;
  for (long long z = 1; z <= m; ++z) {
    for (long long i = 1; i <= n; ++i) {
      for (long long j = 1; j <= n; ++j) cin >> a[z][i][j];
    }
    for (long long k = 1; k <= n; ++k) {
      for (long long i = 1; i <= n; ++i) {
        for (long long j = 1; j <= n; ++j) {
          a[z][i][j] = min(a[z][i][j], a[z][i][k] + a[z][k][j]);
          dp[0][i][j] = min(dp[0][i][j], a[z][i][j]);
        }
      }
    }
  }
  for (long long z = 1; z <= n; ++z) {
    for (long long i = 1; i <= n; ++i) {
      for (long long j = 1; j <= n; ++j) {
        for (long long k = 1; k <= n; ++k) {
          dp[z][i][j] = min(dp[z][i][j], dp[z - 1][i][k] + dp[0][k][j]);
        }
      }
    }
  }
  while (r--) {
    long long x, y, z;
    cin >> x >> y >> z;
    cout << dp[min(n, z)][x][y] << "\n";
  }
}
