#include <bits/stdc++.h>
using namespace std;
const double pi = 3.141592653589793;
const int INF = 1000000000;
const long long mod = 200907;
const long long N = 1100;
int d[65][65][65];
int w[65][65][65];
int w1[65][65][65];
int main() {
  int n, m, r;
  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)++) cin >> d[k][i][j];
  for (int(t) = (0); (t) < (65); (t)++)
    for (int(i) = (0); (i) < (65); (i)++)
      for (int(j) = (0); (j) < (65); (j)++) w[t][i][j] = INF;
  for (int(t) = (0); (t) < (m); (t)++) {
    for (int k = 0; k < n; ++k)
      for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
          d[t][i][j] = min(d[t][i][j], d[t][i][k] + d[t][k][j]);
    for (int(i) = (0); (i) < (n); (i)++)
      for (int(j) = (0); (j) < (n); (j)++)
        w[0][i][j] = min(w[0][i][j], d[t][i][j]);
  }
  for (int(t) = (1); (t) < (61); (t)++)
    for (int(i) = (0); (i) < (n); (i)++)
      for (int(j) = (0); (j) < (n); (j)++) {
        w[t][i][j] = w[t - 1][i][j];
        for (int(k) = (0); (k) < (n); (k)++) {
          w[t][i][j] = min(w[t][i][j], w[t - 1][i][k] + w[0][k][j]);
          w[t][i][j] = min(w[t][i][j], w[0][i][k] + w[t - 1][k][j]);
        }
      }
  for (int(i) = (0); (i) < (r); (i)++) {
    int a, b, t;
    cin >> a >> b >> t;
    a--;
    b--;
    if (t > 60) t = 60;
    cout << w[t][a][b] << endl;
  }
  return 0;
}
