#include <bits/stdc++.h>
int map[61][61][61];
int dis[61][61][61];
int n, m, r;
int s, t, k;
int main() {
  scanf("%d%d%d", &n, &m, &r);
  for (int i = 0; i < m; i++) {
    for (int s = 1; s <= n; s++) {
      for (int t = 1; t <= n; t++) {
        scanf("%d", &map[i][s][t]);
      }
    }
    for (int k = 1; k <= n; k++) {
      for (int s = 1; s <= n; s++) {
        for (int t = 1; t <= n; t++) {
          map[i][s][t] = ((map[i][s][k] + map[i][k][t]) < (map[i][s][t])
                              ? (map[i][s][k] + map[i][k][t])
                              : (map[i][s][t]));
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dis[0][i][j] = 999999999;
      for (int k = 0; k < m; k++) {
        dis[0][i][j] =
            ((dis[0][i][j]) < (map[k][i][j]) ? (dis[0][i][j]) : (map[k][i][j]));
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int s = 1; s <= n; s++) {
      for (int t = 1; t <= n; t++) {
        dis[i][s][t] = dis[i - 1][s][t];
      }
    }
    for (int k = 1; k <= n; k++) {
      for (int s = 1; s <= n; s++) {
        for (int t = 1; t <= n; t++) {
          dis[i][s][t] = ((dis[i][s][t]) < (dis[i - 1][s][k] + dis[0][k][t])
                              ? (dis[i][s][t])
                              : (dis[i - 1][s][k] + dis[0][k][t]));
        }
      }
    }
  }
  while (r--) {
    scanf("%d%d%d", &s, &t, &k);
    if (k > n) {
      k = n;
    }
    printf("%d\n", dis[k][s][t]);
  }
}
