#include <bits/stdc++.h>
void initf() {}
using namespace std;
int a[60][60][60];
int b[1001][60][60];
int n, m, q;
void solve() {
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) {
      for (int k = 0; k < n; ++k) {
        scanf("%d", &a[i][j][k]);
      }
    }
    for (int k = 0; k < n; ++k) {
      for (int ii = 0; ii < n; ++ii) {
        for (int j = 0; j < n; ++j) {
          if (a[i][ii][j] > a[i][ii][k] + a[i][k][j]) {
            a[i][ii][j] = a[i][ii][k] + a[i][k][j];
          }
        }
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      b[0][i][j] = a[0][i][j];
    }
  }
  for (int item = 1; item < m; ++item) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        b[0][i][j] = min(b[0][i][j], a[item][i][j]);
      }
    }
  }
  for (int K = 1; K <= 1000; ++K) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        b[K][i][j] = b[K - 1][i][j];
      }
    }
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        for (int k = 0; k < n; ++k) {
          b[K][i][j] = min(b[K][i][j], b[K - 1][i][k] + b[0][k][j]);
        }
      }
    }
  }
  while (q--) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    --x;
    --y;
    printf("%d\n", b[z][x][y]);
  }
}
int main() {
  initf();
  solve();
  return 0;
}
