#include <bits/stdc++.h>
using namespace std;
const int INFTY = 1 << 29;
int main() {
  for (int n, m, r; ~scanf("%d%d%d", &n, &m, &r) && n | m | r;) {
    vector<vector<vector<int> > > gs(m,
                                     vector<vector<int> >(n, vector<int>(n)));
    for (int k = int(0); k < int(m); k++)
      for (int i = int(0); i < int(n); i++)
        for (int j = int(0); j < int(n); j++) scanf("%d", &gs[k][i][j]);
    for (vector<vector<int> >& g : gs)
      for (int k = int(0); k < int(n); k++)
        for (int i = int(0); i < int(n); i++)
          for (int j = int(0); j < int(n); j++)
            g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
    vector<vector<vector<int> > > dp(
        n, vector<vector<int> >(n, vector<int>(n, INFTY)));
    for (int i = int(0); i < int(n); i++)
      for (int j = int(0); j < int(n); j++)
        for (int k = int(0); k < int(m); k++)
          dp[i][j][0] = min(dp[i][j][0], gs[k][i][j]);
    for (int l = int(1); l < int(n); l++)
      for (int k = int(0); k < int(n); k++)
        for (int i = int(0); i < int(n); i++)
          for (int j = int(0); j < int(n); j++)
            dp[i][j][l] = min(dp[i][j][l], dp[i][k][l - 1] + dp[k][j][0]);
    while (r--) {
      int s, t, k;
      scanf("%d%d%d", &s, &t, &k);
      printf("%d\n", dp[s - 1][t - 1][min(k, n - 1)]);
    }
  }
  return 0;
}
