#include <bits/stdc++.h>
using namespace std;
long long poww(long long a, long long b, long long md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}
constexpr int N = 70;
const int MOD = 1e9 + 7;
const long long INF = 2e9;
int ans[1002][N][N], d[N][N], n, m, r;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> n >> m >> r;
  for (int i = 1; i < N; ++i)
    for (int j = 1; j < N; ++j) ans[0][i][j] = INF;
  for (int c = 0; c < m; ++c) {
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j) cin >> d[i][j];
    for (int k = 1; k <= n; ++k)
      for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j) ans[0][i][j] = min(ans[0][i][j], d[i][j]);
  }
  for (int lim = 1; lim < 1001; ++lim) {
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j) {
        ans[lim][i][j] = ans[lim - 1][i][j];
        for (int k = 1; k <= n; ++k)
          ans[lim][i][j] =
              min(ans[lim][i][j], ans[lim - 1][i][k] + ans[0][k][j]);
      }
    }
  }
  while (r--) {
    int u, v, k;
    cin >> u >> v >> k;
    cout << ans[k][u][v] << endl;
  }
  return 0;
}
