#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.);
const int MAXN = 100000 + 13, INF = 1000000013;
struct str {
  int m[100][100];
} a[100];
int n, m, r, s, t, k, d[100][100][100];
void calc(str &m, int n) {
  for (int k = 0; k < n; ++k)
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j)
        m.m[i][j] = min(m.m[i][j], m.m[i][k] + m.m[k][j]);
}
void calc2(int s, int t, int k) {
  int r = INF;
  d[s][t][k] = d[s][t][k - 1];
  for (int j = 0; j < n; ++j)
    d[s][t][k] = min(d[s][t][k], d[s][j][k - 1] + d[j][t][0]);
  r = min(r, d[s][t][k]);
  d[s][t][k] = r;
}
int main() {
  cin >> n >> m >> r;
  for (int k = 0; k < m; ++k)
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j) scanf("%d", &a[k].m[i][j]);
  for (int i = 0; i < m; ++i) calc(a[i], n);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) {
      d[i][j][0] = a[0].m[i][j];
      for (int l = 1; l < m; ++l) d[i][j][0] = min(d[i][j][0], a[l].m[i][j]);
    }
  for (int k = 1; k <= n; ++k)
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j) calc2(i, j, k);
  while (r--) {
    cin >> s >> t >> k;
    if (k > n) k = n;
    s--, t--;
    cout << d[s][t][k] << endl;
  }
  return 0;
}
