// #{{{
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define ll long long
#define LDB long double
#define MS0(I) memset(I, 0, sizeof(I));
#define MS1(I) memset(I, -1, sizeof(I));
#define F(n) FI(i,n)
#define FI(i,n) FL(i,0,n)
#define FL(i,l,n) for(int i=l;i<n;++i)
#define RDI(A) int A; scanf("%d", &A);
#define RDII(A, B) int A, B; scanf("%d %d", &A, &B);
#define RDIII(A, B, C) int A, B, C; scanf("%d %d %d", &A, &B, &C);
#define RDL(A) ll A; scanf("%lld", &A);
#define RDLL(A, B) ll A, B; scanf("%lld %lld", &A, &B);
#define RDLLL(A, B, C) ll A, B, C; scanf("%lld %lld %lld", &A, &B, &C);
#define SZ(X) ((int)X.size() )
#define ALL(I) (I).begin(), (I).end()
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define X first
#define Y second
// #}}}
const double pi = acos(-1.0);
const double eps = 1e-9;
#define MAXN 200005
#define mod 1000000007
#define inf (ll)1e15
#define type int
//#{{{
typedef pair<type,type> Pt;
typedef pair<Pt,Pt> Line;
Pt operator+(const Pt& p1,const Pt& p2){
	return Pt(p1.X+p2.X,p1.Y+p2.Y);
}
Pt operator-(const Pt& p1,const Pt& p2){
	return Pt(p1.X-p2.X,p1.Y-p2.Y);
}
type operator*(const Pt& p1,const Pt& p2){
	return (p1.X*p2.X+p1.Y*p2.Y);
}
type operator^(const Pt& p1,const Pt& p2){
	return (p1.X*p2.Y-p1.Y*p2.X);
}
//#}}}
int n,m,_t;
ll TM[61][61][61]; // car-i-j;
ll dp[1001][61][61];
ll mn[61][61];
void solve(){
	FI(i,n) FI(j,n) mn[i][j] = inf;
	FI(i,1001) FI(j,61) FI(k,61) dp[i][j][k] = inf;
	FI(s,m){
		FI(k,n) FI(i,k-1) FI(j,k-1){
			TM[s][i][j] = min(TM[s][i][j],TM[s][i][k]+TM[s][k][j]);
			mn[i][j] = min(mn[i][j],TM[s][i][j]);
		}
	}
	FI(i,n) FI(j,n){
		dp[0][i][j] = mn[i][j];
	}
	for(int r = 1 ; r <= 1000 ; r++){
		FI(k,n) FI(i,k-1) FI(j,k-1){
			dp[r][i][j] = min(dp[r][i][j], dp[r-1][i][k]+mn[k][j]);
		}
	}
}
int main(){
	cin >>n >>m >>_t;
// city, cars, rounds
	FI(i,m) FI(j,n) FI(k,n){
		scanf("%lld",&TM[i][j][k]);
	}
	solve();
	while(_t--){
		RDIII(s,t,k);
		s--;t--;
		printf("%lld\n",dp[k][s][t]);
	}
}


// #{{{
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define ll long long
#define LDB long double
#define MS0(I) memset(I, 0, sizeof(I));
#define MS1(I) memset(I, -1, sizeof(I));
#define F(n) FI(i,n)
#define FI(i,n) FL(i,0,n)
#define FL(i,l,n) for(int i=l;i<n;++i)
#define RDI(A) int A; scanf("%d", &A);
#define RDII(A, B) int A, B; scanf("%d %d", &A, &B);
#define RDIII(A, B, C) int A, B, C; scanf("%d %d %d", &A, &B, &C);
#define RDL(A) ll A; scanf("%lld", &A);
#define RDLL(A, B) ll A, B; scanf("%lld %lld", &A, &B);
#define RDLLL(A, B, C) ll A, B, C; scanf("%lld %lld %lld", &A, &B, &C);
#define SZ(X) ((int)X.size() )
#define ALL(I) (I).begin(), (I).end()
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define X first
#define Y second
// #}}}
const double pi = acos(-1.0);
const double eps = 1e-9;
#define MAXN 200005
#define mod 1000000007
#define inf (ll)1e15
#define type int
//#{{{
typedef pair<type,type> Pt;
typedef pair<Pt,Pt> Line;
Pt operator+(const Pt& p1,const Pt& p2){
	return Pt(p1.X+p2.X,p1.Y+p2.Y);
}
Pt operator-(const Pt& p1,const Pt& p2){
	return Pt(p1.X-p2.X,p1.Y-p2.Y);
}
type operator*(const Pt& p1,const Pt& p2){
	return (p1.X*p2.X+p1.Y*p2.Y);
}
type operator^(const Pt& p1,const Pt& p2){
	return (p1.X*p2.Y-p1.Y*p2.X);
}
//#}}}
int n,m,_t;
ll TM[61][61][61]; // car-i-j;
ll dp[1001][61][61];
ll mn[61][61];
void solve(){
	FI(i,n) FI(j,n) mn[i][j] = inf;
	FI(i,1001) FI(j,61) FI(k,61) dp[i][j][k] = inf;
	FI(s,m){
		FI(k,n) FI(i,n) FI(j,n){
			TM[s][i][j] = min(TM[s][i][j],TM[s][i][k]+TM[s][k][j]);
			mn[i][j] = min(mn[i][j],TM[s][i][j]);
		}
	}
	FI(i,n) FI(j,n){
		dp[0][i][j] = mn[i][j];
	}
	for(int r = 1 ; r <= 1000 ; r++){
		FI(k,n) FI(i,n) FI(j,n){
			dp[r][i][j] = min(dp[r][i][j], dp[r-1][i][k]+mn[k][j]);
		}
	}
}
int main(){
	cin >>n >>m >>_t;
// city, cars, rounds
	FI(i,m) FI(j,n) FI(k,n){
		scanf("%lld",&TM[i][j][k]);
	}
	solve();
	while(_t--){
		RDIII(s,t,k);
		s--;t--;
		printf("%lld\n",dp[k][s][t]);
	}
}


// #{{{
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define ll long long
#define LDB long double
#define MS0(I) memset(I, 0, sizeof(I));
#define MS1(I) memset(I, -1, sizeof(I));
#define F(n) FI(i,n)
#define FI(i,n) FL(i,0,n)
#define FL(i,l,n) for(int i=l;i<n;++i)
#define RDI(A) int A; scanf("%d", &A);
#define RDII(A, B) int A, B; scanf("%d %d", &A, &B);
#define RDIII(A, B, C) int A, B, C; scanf("%d %d %d", &A, &B, &C);
#define RDL(A) ll A; scanf("%lld", &A);
#define RDLL(A, B) ll A, B; scanf("%lld %lld", &A, &B);
#define RDLLL(A, B, C) ll A, B, C; scanf("%lld %lld %lld", &A, &B, &C);
#define SZ(X) ((int)X.size() )
#define ALL(I) (I).begin(), (I).end()
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define X first
#define Y second
// #}}}
const double pi = acos(-1.0);
const double eps = 1e-9;
#define MAXN 200005
#define mod 1000000007
#define inf (ll)1e15
#define type int
//#{{{
typedef pair<type,type> Pt;
typedef pair<Pt,Pt> Line;
Pt operator+(const Pt& p1,const Pt& p2){
	return Pt(p1.X+p2.X,p1.Y+p2.Y);
}
Pt operator-(const Pt& p1,const Pt& p2){
	return Pt(p1.X-p2.X,p1.Y-p2.Y);
}
type operator*(const Pt& p1,const Pt& p2){
	return (p1.X*p2.X+p1.Y*p2.Y);
}
type operator^(const Pt& p1,const Pt& p2){
	return (p1.X*p2.Y-p1.Y*p2.X);
}
//#}}}
int n,m,_t;
ll tm[61][61][61]; // car-i-j;
ll dp[1001][61][61];
ll mn[61][61];
void solve(){
	FI(i,n) FI(j,n) mn[i][j] = inf;
	FI(i,1001) FI(j,61) FI(k,61) dp[i][j][k] = inf;
	FI(s,m){
		FI(i,n) FI(j,n) FI(k,n){
			tm[s][i][j] = min(tm[s][i][j],tm[s][i][k]+tm[s][k][j]);
			mn[i][j] = min(mn[i][j],tm[s][i][j]);
		}
	}
	FI(i,n) FI(j,n){
		dp[0][i][j] = mn[i][j];
	}
	for(int r = 1 ; r < 1001 ; r++){
		FI(i,n) FI(j,n) FI(k,n){
			dp[r][i][j] = min(dp[r][i][j], dp[r-1][i][k]+mn[k][j]);
		}
	}
}
int main(){
	cin >>n >>m >>_t;
// city, cars, rounds
	FI(i,m) FI(j,n) FI(k,n){
		scanf("%lld",&tm[i][j][k]);
	}
	solve();
	while(_t--){
		RDIII(s,t,k);
		s--;t--;
		printf("%lld\n",dp[k][s][t]);
	}
}


#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")
using namespace std;

#define int long long
#define sync ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define endl "\n"
#define ends " "
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define debug(x) cerr << #x << ": " << x << endl
#define debugP(p) cerr << #p << ": {" << p.first << ", " << p.second << '}' << endl
#define debugV(v) cerr << #v << ": [ "; for(int __ = 0; __ < v.size(); __++) cerr << v[__] << ((__ != v.size() - 1) ? ", " : ends); cerr << ']' << endl
#define debugVP(v) cerr << #v << ": [ "; for(int ___ = 0; ___ < v.size(); ___++) cerr << '{' << v[___].first << ", " << v[___].second << '}' << ((___ != v.size() - 1) ? ", " : ends); cerr << ']' << endl
#define debugA(a, n) cerr << #a << ": [ "; for(int ____ = 0; ____ < n; ____++) cerr << a[____] << ((____ != n - 1) ? ", " : ends); cerr << ']' << endl
#define print(args...) fprintf (st                                  derr, args)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef map<pii, int> mpii;
typedef set<int> sti;
const int INF = 1e9, MOD = INF + 7 /* INF - 1755647 */;
int gcd(int a, int b) { return (b ? gcd(b, a % b) : a); }
int power(int a, int b) { return (!b ? 1 : power(a, b / 2) * power(a, b / 2) * (b % 2 ? a : 1)); }
int mod(int a) { return (a % MOD + MOD) % MOD; }

/////////////////////////////////////////////////////////////////////

const int N = 1e2 + 20, K = 1e3 + 20;

int a[N][N][N];
int dp[K][N][N];

int32_t main() {

    sync;

    int n, m, r; cin >> n >> m >> r;

    memset(a, 63, sizeof a);
    memset(dp, 63, sizeof dp);
    for(int t = 1; t <= m; t++) for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) cin >> a[t][i][j];
    for(int t = 1; t <= m; t++) for(int k = 1; k <= n; k++) for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) a[t][i][j] = min(a[t][i][j], a[t][i][k] + a[t][k][j]);
    for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) for(int t = 1; t <= m; t++) dp[0][i][j] = min(dp[0][i][j], a[t][i][j]);
    for(int c = 1; c < K; c++) for(int k = 1; k <= n; k++) for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) dp[c][i][j] = min(dp[c][i][j], dp[c - 1][i][k] + dp[0][k][j]);

    while(r--) {
        int s, t, k; cin >> s >> t >> k;
        cout << dp[k][s][t] << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")
using namespace std;

#define int long long
#define sync ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define endl "\n"
#define ends " "
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define debug(x) cerr << #x << ": " << x << endl
#define debugP(p) cerr << #p << ": {" << p.first << ", " << p.second << '}' << endl
#define debugV(v) cerr << #v << ": [ "; for(int __ = 0; __ < v.size(); __++) cerr << v[__] << ((__ != v.size() - 1) ? ", " : ends); cerr << ']' << endl
#define debugVP(v) cerr << #v << ": [ "; for(int ___ = 0; ___ < v.size(); ___++) cerr << '{' << v[___].first << ", " << v[___].second << '}' << ((___ != v.size() - 1) ? ", " : ends); cerr << ']' << endl
#define debugA(a, n) cerr << #a << ": [ "; for(int ____ = 0; ____ < n; ____++) cerr << a[____] << ((____ != n - 1) ? ", " : ends); cerr << ']' << endl
#define print(args...) fprintf (st                                  derr, args)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef map<pii, int> mpii;
typedef set<int> sti;
const int INF = 1e9, MOD = INF + 7 /* INF - 1755647 */;
int gcd(int a, int b) { return (b ? gcd(b, a % b) : a); }
int power(int a, int b) { return (!b ? 1 : power(a, b / 2) * power(a, b / 2) * (b % 2 ? a : 1)); }
int mod(int a) { return (a % MOD + MOD) % MOD; }

/////////////////////////////////////////////////////////////////////

const int N = 1e2 + 20, K = 1e3 + 20;

int a[K][N][N];
int dp[K][N][N];
// dp[k][i][j] = Shortest path from i to j with maximum k changes

int32_t main() {

    sync;

    int n, m, r; cin >> n >> m >> r;

    memset(a, 63, sizeof a);
    memset(dp, 63, sizeof dp);
    for(int t = 1; t <= m; t++) for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) cin >> a[t][i][j];
    for(int t = 1; t <= m; t++) for(int k = 1; k <= n; k++) for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) a[t][i][j] = min(a[t][i][j], a[t][i][k] + a[t][k][j]);
    for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) for(int t = 1; t <= m; t++) dp[0][i][j] = min(dp[0][i][j], a[t][i][j]);
    for(int c = 1; c < K; c++) for(int k = 1; k <= n; k++) for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) dp[c][i][j] = min(dp[c][i][j], dp[c - 1][i][k] + dp[0][k][j]);

    while(r--) {
        int s, t, k; cin >> s >> t >> k;
        cout << dp[k][s][t] << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")
using namespace std;

#define int long long
#define sync ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define endl "\n"
#define ends " "
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define debug(x) cerr << #x << ": " << x << endl
#define debugP(p) cerr << #p << ": {" << p.first << ", " << p.second << '}' << endl
#define debugV(v) cerr << #v << ": [ "; for(int __ = 0; __ < v.size(); __++) cerr << v[__] << ((__ != v.size() - 1) ? ", " : ends); cerr << ']' << endl
#define debugVP(v) cerr << #v << ": [ "; for(int ___ = 0; ___ < v.size(); ___++) cerr << '{' << v[___].first << ", " << v[___].second << '}' << ((___ != v.size() - 1) ? ", " : ends); cerr << ']' << endl
#define debugA(a, n) cerr << #a << ": [ "; for(int ____ = 0; ____ < n; ____++) cerr << a[____] << ((____ != n - 1) ? ", " : ends); cerr << ']' << endl
#define print(args...) fprintf (st                                  derr, args)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef map<pii, int> mpii;
typedef set<int> sti;
const int INF = 1e9, MOD = INF + 7 /* INF - 1755647 */;
int gcd(int a, int b) { return (b ? gcd(b, a % b) : a); }
int power(int a, int b) { return (!b ? 1 : power(a, b / 2) * power(a, b / 2) * (b % 2 ? a : 1)); }
int mod(int a) { return (a % MOD + MOD) % MOD; }

/////////////////////////////////////////////////////////////////////

const int N = 1e2 + 20, K = 1e3 + 20;

int a[N][N][N];
int dp[K][N][N];

inline int input() {
    int x; cin >> x;
    return x;
}

int32_t main() {

    sync;

    int n, m, r; cin >> n >> m >> r;

    memset(a, 63, sizeof a);
    memset(dp, 63, sizeof dp);
    for(int t = 1; t <= m; t++) for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) cin >> a[t][i][j];
    for(int t = 1; t <= m; t++) for(int k = 1; k <= n; k++) for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) a[t][i][j] = min(a[t][i][j], a[t][i][k] + a[t][k][j]);
    for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) for(int t = 1; t <= m; t++) dp[0][i][j] = min(dp[0][i][j], a[t][i][j]);
    for(int c = 1; c < K; c++) for(int k = 1; k <= n; k++) for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) dp[c][i][j] = min(dp[c][i][j], dp[c - 1][i][k] + dp[0][k][j]);

    while(r--) {
        int s, t, k; cin >> s >> t >> k;
        cout << dp[k][s][t] << endl;
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int N = 80;

#define F first
#define S second
#define PB push_back
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define ios ios_base::sync_with_stdio(0);   cin.tie();  cout.tie();
#define dbg(x)  cerr << x << "\n"

ll n, m, u, v, w, dis[N][N][N], k, sum, r, dp[N][N][N];
pair<pll, ll> e[250000 + 10];
vpll g[N];

void Floyd()
{
    for (int l = 1; l <= m; l++)
    {
        for (int k = 1; k <= n; k++)
        {
            for (int i = 1; i<= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    dis[l][i][j] = min(dis[l][i][j], dis[l][i][k] + dis[l][k][j]);
                }
            }
        }
    }
}

int main()
{
        ios
        cin >> n >> m >> r;
        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                for (int k = 1; k <= n; k++)
                {
                    cin >> dis[i][j][k];
                }
            }
        }
        Floyd();
        memset(dp, 63, sizeof dp);
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                for (int k = 1; k <= m; k++)
                {
                    dp[0][i][j] = min(dp[0][i][j], dis[k][i][j]);

                }
            }
        }
        for (int k = 1; k <= m; k++)
        {
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    for (int l = 1; l <= n; l++)
                    {
                        dp[k][i][j] = min(dp[k][i][j], dp[k - 1][i][l] + dp[0][l][j]);
                    }
                }
            }
        }
        for (int i = 0; i < r; i++)
        {
            cin >> u >> v >> w;
            ll ans = 1e9;
            for (int j = 0; j <= w; j++)
                ans = min(ans, dp[j][u][v]);
            cout << ans << endl;
        }
}

/*
 ███▄ ▄███▓ ██▀███   ██▓███   ██▀███   ██▓ ███▄    █  ▄████▄  ▓█████
▓██▒▀█▀ ██▒▓██ ▒ ██▒▓██░  ██▒▓██ ▒ ██▒▓██▒ ██ ▀█   █ ▒██▀ ▀█  ▓█   ▀
▓██    ▓██░▓██ ░▄█ ▒▓██░ ██▓▒▓██ ░▄█ ▒▒██▒▓██  ▀█ ██▒▒▓█    ▄ ▒███
▒██    ▒██ ▒██▀▀█▄  ▒██▄█▓▒ ▒▒██▀▀█▄  ░██░▓██▒  ▐▌██▒▒▓▓▄ ▄██▒▒▓█  ▄
▒██▒   ░██▒░██▓ ▒██▒▒██▒ ░  ░░██▓ ▒██▒░██░▒██░   ▓██░▒ ▓███▀ ░░▒████▒
░ ▒░   ░  ░░ ▒▓ ░▒▓░▒▓▒░ ░  ░░ ▒▓ ░▒▓░░▓  ░ ▒░   ▒ ▒ ░ ░▒ ▒  ░░░ ▒░ ░
░  ░      ░  ░▒ ░ ▒░░▒ ░       ░▒ ░ ▒░ ▒ ░░ ░░   ░ ▒░  ░  ▒    ░ ░  ░
░      ░     ░░   ░ ░░         ░░   ░  ▒ ░   ░   ░ ░ ░           ░
       ░      ░                 ░      ░           ░ ░ ░         ░  ░
*/


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int N = 80;

#define F first
#define S second
#define PB push_back
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define ios ios_base::sync_with_stdio(0);   cin.tie();  cout.tie();
#define dbg(x)  cerr << x << "\n"

ll n, m, u, v, w, dis[N][N][N], k, sum, r, dp[N][N][N];
pair<pll, ll> e[250000 + 10];
vpll g[N];

void Floyd()
{
    for (int l = 1; l <= m; l++)
    {
        for (int k = 1; k <= n; k++)
        {
            for (int i = 1; i<= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    dis[l][i][j] = min(dis[l][i][j], dis[l][i][k] + dis[l][k][j]);
                }
            }
        }
    }
}

int main()
{
        ios
        cin >> n >> m >> r;
        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                for (int k = 1; k <= n; k++)
                {
                    cin >> dis[i][j][k];
                }
            }
        }
        Floyd();
        memset(dp, 63, sizeof dp);
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                for (int k = 1; k <= m; k++)
                {
                    dp[0][i][j] = min(dp[0][i][j], dis[k][i][j]);

                }
            }
        }
        for (int k = 1; k <= m + 1; k++)
        {
            for (int i = 1; i <= n + 1; i++)
            {
                for (int j = 1; j <= n + 1; j++)
                {
                    for (int l = 1; l <= n + 1; l++)
                    {
                        dp[k][i][j] = min(dp[k][i][j], dp[k - 1][i][l] + dp[0][l][j]);
                    }
                }
            }
        }
        for (int i = 0; i < r; i++)
        {
            cin >> u >> v >> w;
            ll ans = 1e9;
            w = min(w, n + 1);
            for (int j = 0; j <= w; j++)
                ans = min(ans, dp[j][u][v]);
            cout << ans << endl;
        }
}

/*
 ███▄ ▄███▓ ██▀███   ██▓███   ██▀███   ██▓ ███▄    █  ▄████▄  ▓█████
▓██▒▀█▀ ██▒▓██ ▒ ██▒▓██░  ██▒▓██ ▒ ██▒▓██▒ ██ ▀█   █ ▒██▀ ▀█  ▓█   ▀
▓██    ▓██░▓██ ░▄█ ▒▓██░ ██▓▒▓██ ░▄█ ▒▒██▒▓██  ▀█ ██▒▒▓█    ▄ ▒███
▒██    ▒██ ▒██▀▀█▄  ▒██▄█▓▒ ▒▒██▀▀█▄  ░██░▓██▒  ▐▌██▒▒▓▓▄ ▄██▒▒▓█  ▄
▒██▒   ░██▒░██▓ ▒██▒▒██▒ ░  ░░██▓ ▒██▒░██░▒██░   ▓██░▒ ▓███▀ ░░▒████▒
░ ▒░   ░  ░░ ▒▓ ░▒▓░▒▓▒░ ░  ░░ ▒▓ ░▒▓░░▓  ░ ▒░   ▒ ▒ ░ ░▒ ▒  ░░░ ▒░ ░
░  ░      ░  ░▒ ░ ▒░░▒ ░       ░▒ ░ ▒░ ▒ ░░ ░░   ░ ▒░  ░  ▒    ░ ░  ░
░      ░     ░░   ░ ░░         ░░   ░  ▒ ░   ░   ░ ░ ░           ░
       ░      ░                 ░      ░           ░ ░ ░         ░  ░
*/


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int N = 63;

#define F first
#define S second
#define PB push_back
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define ios ios_base::sync_with_stdio(0);   cin.tie();  cout.tie();
#define dbg(x)  cerr << x << "\n"

ll n, m, u, v, w, dis[N][N][N], k, sum, r, dp[N][N][N];
pair<pll, ll> e[250000 + 10];
vpll g[N];

void Floyd()
{
    for (int l = 1; l <= m; l++)
    {
        for (int k = 1; k <= n; k++)
        {
            for (int i = 1; i<= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    dis[l][i][j] = min(dis[l][i][j], dis[l][i][k] + dis[l][k][j]);
                }
            }
        }
    }
}

int main()
{
        ios
        cin >> n >> m >> r;
        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                for (int k = 1; k <= n; k++)
                {
                    cin >> dis[i][j][k];
                }
            }
        }
        Floyd();
        memset(dp, 63, sizeof dp);
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                for (int k = 1; k <= m; k++)
                {
                    dp[0][i][j] = min(dp[0][i][j], dis[k][i][j]);

                }
            }
        }
        for (int k = 1; k <= m; k++)
        {
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    for (int l = 1; l <= n; l++)
                    {
                        dp[k][i][j] = min(dp[k][i][j], dp[k - 1][i][l] + dp[0][l][j]);
                    }
                }
            }
        }
        for (int i = 0; i < r; i++)
        {
            cin >> u >> v >> w;
            ll ans = 1e9;
            for (int i = 0; i <= w; i++)
                ans = min(ans, dp[i][u][v]);
            cout << ans << endl;
        }
}

/*
 ███▄ ▄███▓ ██▀███   ██▓███   ██▀███   ██▓ ███▄    █  ▄████▄  ▓█████
▓██▒▀█▀ ██▒▓██ ▒ ██▒▓██░  ██▒▓██ ▒ ██▒▓██▒ ██ ▀█   █ ▒██▀ ▀█  ▓█   ▀
▓██    ▓██░▓██ ░▄█ ▒▓██░ ██▓▒▓██ ░▄█ ▒▒██▒▓██  ▀█ ██▒▒▓█    ▄ ▒███
▒██    ▒██ ▒██▀▀█▄  ▒██▄█▓▒ ▒▒██▀▀█▄  ░██░▓██▒  ▐▌██▒▒▓▓▄ ▄██▒▒▓█  ▄
▒██▒   ░██▒░██▓ ▒██▒▒██▒ ░  ░░██▓ ▒██▒░██░▒██░   ▓██░▒ ▓███▀ ░░▒████▒
░ ▒░   ░  ░░ ▒▓ ░▒▓░▒▓▒░ ░  ░░ ▒▓ ░▒▓░░▓  ░ ▒░   ▒ ▒ ░ ░▒ ▒  ░░░ ▒░ ░
░  ░      ░  ░▒ ░ ▒░░▒ ░       ░▒ ░ ▒░ ▒ ░░ ░░   ░ ▒░  ░  ▒    ░ ░  ░
░      ░     ░░   ░ ░░         ░░   ░  ▒ ░   ░   ░ ░ ░           ░
       ░      ░                 ░      ░           ░ ░ ░         ░  ░
*/


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int N = 63;

#define F first
#define S second
#define PB push_back
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define ios ios_base::sync_with_stdio(0);   cin.tie();  cout.tie();
#define dbg(x)  cerr << x << "\n"

ll n, m, u, v, w, dis[N][N][N], k, sum, r, dp[N][N][N];
pair<pll, ll> e[250000 + 10];
vpll g[N];

void Floyd()
{
    for (int l = 1; l <= m; l++)
    {
        for (int k = 1; k <= n; k++)
        {
            for (int i = 1; i<= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    dis[l][i][j] = min(dis[l][i][j], dis[l][i][k] + dis[l][k][j]);
                }
            }
        }
    }
}

int main()
{
        ios
        cin >> n >> m >> r;
        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                for (int k = 1; k <= n; k++)
                {
                    cin >> dis[i][j][k];
                }
            }
        }
        Floyd();
        memset(dp, 63, sizeof dp);
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                for (int k = 1; k <= m; k++)
                {
                    dp[0][i][j] = min(dp[0][i][j], dis[k][i][j]);

                }
            }
        }
        for (int k = 1; k <= m; k++)
        {
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    for (int l = 1; l <= n; l++)
                    {
                        dp[k][i][j] = min(dp[k][i][j], dp[k - 1][i][l] + dp[0][l][j]);
                    }
                }
            }
        }
        for (int i = 0; i < r; i++)
        {
            cin >> u >> v >> w;
            ll ans = 1e9;
            for (int j = 0; j <= w; j++)
                ans = min(ans, dp[j][u][v]);
            cout << ans << endl;
        }
}

/*
 ███▄ ▄███▓ ██▀███   ██▓███   ██▀███   ██▓ ███▄    █  ▄████▄  ▓█████
▓██▒▀█▀ ██▒▓██ ▒ ██▒▓██░  ██▒▓██ ▒ ██▒▓██▒ ██ ▀█   █ ▒██▀ ▀█  ▓█   ▀
▓██    ▓██░▓██ ░▄█ ▒▓██░ ██▓▒▓██ ░▄█ ▒▒██▒▓██  ▀█ ██▒▒▓█    ▄ ▒███
▒██    ▒██ ▒██▀▀█▄  ▒██▄█▓▒ ▒▒██▀▀█▄  ░██░▓██▒  ▐▌██▒▒▓▓▄ ▄██▒▒▓█  ▄
▒██▒   ░██▒░██▓ ▒██▒▒██▒ ░  ░░██▓ ▒██▒░██░▒██░   ▓██░▒ ▓███▀ ░░▒████▒
░ ▒░   ░  ░░ ▒▓ ░▒▓░▒▓▒░ ░  ░░ ▒▓ ░▒▓░░▓  ░ ▒░   ▒ ▒ ░ ░▒ ▒  ░░░ ▒░ ░
░  ░      ░  ░▒ ░ ▒░░▒ ░       ░▒ ░ ▒░ ▒ ░░ ░░   ░ ▒░  ░  ▒    ░ ░  ░
░      ░     ░░   ░ ░░         ░░   ░  ▒ ░   ░   ░ ░ ░           ░
       ░      ░                 ░      ░           ░ ░ ░         ░  ░
*/


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int N = 53;

#define F first
#define S second
#define PB push_back
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define ios ios_base::sync_with_stdio(0);   cin.tie();  cout.tie();
#define dbg(x)  cerr << x << "\n"

ll n, m, u, v, w, dis[N][N][N], k, sum, r, dp[N][N][N];
pair<pll, ll> e[250000 + 10];
vpll g[N];

void Floyd()
{
    for (int l = 1; l <= m; l++)
    {
        for (int k = 1; k <= n; k++)
        {
            for (int i = 1; i<= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    dis[l][i][j] = min(dis[l][i][j], dis[l][i][k] + dis[l][k][j]);
                }
            }
        }
    }
}

int main()
{
        ios
        cin >> n >> m >> r;
        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                for (int k = 1; k <= n; k++)
                {
                    cin >> dis[i][j][k];
                }
            }
        }
        cout << n << endl;
        Floyd();
        memset(dp, 63, sizeof dp);
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                for (int k = 1; k <= m; k++)
                {
                    dp[0][i][j] = min(dp[0][i][j], dis[k][i][j]);

                }
            }
        }
        for (int k = 1; k <= n; k++)
        {
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    for (int l = 1; l <= n; l++)
                    {
                        dp[k][i][j] = min(dp[k][i][j], dp[k - 1][i][l] + dp[0][l][j]);
                    }
                }
            }
        }
        for (int i = 0; i < r; i++)
        {
            cin >> u >> v >> w;
            cout << dp[w][u][v] << endl;
        }
}

/*
 ███▄ ▄███▓ ██▀███   ██▓███   ██▀███   ██▓ ███▄    █  ▄████▄  ▓█████
▓██▒▀█▀ ██▒▓██ ▒ ██▒▓██░  ██▒▓██ ▒ ██▒▓██▒ ██ ▀█   █ ▒██▀ ▀█  ▓█   ▀
▓██    ▓██░▓██ ░▄█ ▒▓██░ ██▓▒▓██ ░▄█ ▒▒██▒▓██  ▀█ ██▒▒▓█    ▄ ▒███
▒██    ▒██ ▒██▀▀█▄  ▒██▄█▓▒ ▒▒██▀▀█▄  ░██░▓██▒  ▐▌██▒▒▓▓▄ ▄██▒▒▓█  ▄
▒██▒   ░██▒░██▓ ▒██▒▒██▒ ░  ░░██▓ ▒██▒░██░▒██░   ▓██░▒ ▓███▀ ░░▒████▒
░ ▒░   ░  ░░ ▒▓ ░▒▓░▒▓▒░ ░  ░░ ▒▓ ░▒▓░░▓  ░ ▒░   ▒ ▒ ░ ░▒ ▒  ░░░ ▒░ ░
░  ░      ░  ░▒ ░ ▒░░▒ ░       ░▒ ░ ▒░ ▒ ░░ ░░   ░ ▒░  ░  ▒    ░ ░  ░
░      ░     ░░   ░ ░░         ░░   ░  ▒ ░   ░   ░ ░ ░           ░
       ░      ░                 ░      ░           ░ ░ ░         ░  ░
*/


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int N = 63;

#define F first
#define S second
#define PB push_back
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define ios ios_base::sync_with_stdio(0);   cin.tie();  cout.tie();
#define dbg(x)  cerr << x << "\n"

ll n, m, u, v, w, dis[N][N][N], k, sum, r, dp[N][N][N];
pair<pll, ll> e[250000 + 10];
vpll g[N];

void Floyd()
{
    for (int l = 1; l <= m; l++)
    {
        for (int k = 1; k <= n; k++)
        {
            for (int i = 1; i<= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    dis[l][i][j] = min(dis[l][i][j], dis[l][i][k] + dis[l][k][j]);
                }
            }
        }
    }
}

int main()
{
        ios
        cin >> n >> m >> r;
        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                for (int k = 1; k <= n; k++)
                {
                    cin >> dis[i][j][k];
                }
            }
        }
        Floyd();
        memset(dp, 63, sizeof dp);
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                for (int k = 1; k <= m; k++)
                {
                    dp[0][i][j] = min(dp[0][i][j], dis[k][i][j]);

                }
            }
        }
        for (int k = 1; k <= n; k++)
        {
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    for (int l = 1; l <= n; l++)
                    {
                        dp[k][i][j] = min(dp[k][i][j], dp[k - 1][i][l] + dp[0][l][j]);
                    }
                }
            }
        }
        for (int i = 0; i < r; i++)
        {
            cin >> u >> v >> w;
            cout << dp[w][u][v] << endl;
        }
}

/*
 ███▄ ▄███▓ ██▀███   ██▓███   ██▀███   ██▓ ███▄    █  ▄████▄  ▓█████
▓██▒▀█▀ ██▒▓██ ▒ ██▒▓██░  ██▒▓██ ▒ ██▒▓██▒ ██ ▀█   █ ▒██▀ ▀█  ▓█   ▀
▓██    ▓██░▓██ ░▄█ ▒▓██░ ██▓▒▓██ ░▄█ ▒▒██▒▓██  ▀█ ██▒▒▓█    ▄ ▒███
▒██    ▒██ ▒██▀▀█▄  ▒██▄█▓▒ ▒▒██▀▀█▄  ░██░▓██▒  ▐▌██▒▒▓▓▄ ▄██▒▒▓█  ▄
▒██▒   ░██▒░██▓ ▒██▒▒██▒ ░  ░░██▓ ▒██▒░██░▒██░   ▓██░▒ ▓███▀ ░░▒████▒
░ ▒░   ░  ░░ ▒▓ ░▒▓░▒▓▒░ ░  ░░ ▒▓ ░▒▓░░▓  ░ ▒░   ▒ ▒ ░ ░▒ ▒  ░░░ ▒░ ░
░  ░      ░  ░▒ ░ ▒░░▒ ░       ░▒ ░ ▒░ ▒ ░░ ░░   ░ ▒░  ░  ▒    ░ ░  ░
░      ░     ░░   ░ ░░         ░░   ░  ▒ ░   ░   ░ ░ ░           ░
       ░      ░                 ░      ░           ░ ░ ░         ░  ░
*/


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int N = 80;

#define F first
#define S second
#define PB push_back
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define ios ios_base::sync_with_stdio(0);   cin.tie();  cout.tie();
#define dbg(x)  cerr << x << "\n"

ll n, m, u, v, w, dis[N][N][N], k, sum, r, dp[N][N][N];
pair<pll, ll> e[250000 + 10];
vpll g[N];

void Floyd()
{
    for (int l = 1; l <= m; l++)
    {
        for (int k = 1; k <= n; k++)
        {
            for (int i = 1; i<= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    dis[l][i][j] = min(dis[l][i][j], dis[l][i][k] + dis[l][k][j]);
                }
            }
        }
    }
}

int main()
{
        ios
        cin >> n >> m >> r;
        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                for (int k = 1; k <= n; k++)
                {
                    cin >> dis[i][j][k];
                }
            }
        }
        Floyd();
        memset(dp, 63, sizeof dp);
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                for (int k = 1; k <= m; k++)
                {
                    dp[0][i][j] = min(dp[0][i][j], dis[k][i][j]);

                }
            }
        }
        for (int k = 1; k <= m; k++)
        {
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    for (int l = 1; l <= n; l++)
                    {
                        dp[k][i][j] = min(dp[k][i][j], dp[k - 1][i][l] + dp[0][l][j]);
                    }
                }
            }
        }
        for (int i = 0; i < r; i++)
        {
            cin >> u >> v >> w;
            ll ans = 1e9;
            w = min(w, n);
            for (int j = 0; j <= w; j++)
                ans = min(ans, dp[j][u][v]);
            cout << ans << endl;
        }
}

/*
 ███▄ ▄███▓ ██▀███   ██▓███   ██▀███   ██▓ ███▄    █  ▄████▄  ▓█████
▓██▒▀█▀ ██▒▓██ ▒ ██▒▓██░  ██▒▓██ ▒ ██▒▓██▒ ██ ▀█   █ ▒██▀ ▀█  ▓█   ▀
▓██    ▓██░▓██ ░▄█ ▒▓██░ ██▓▒▓██ ░▄█ ▒▒██▒▓██  ▀█ ██▒▒▓█    ▄ ▒███
▒██    ▒██ ▒██▀▀█▄  ▒██▄█▓▒ ▒▒██▀▀█▄  ░██░▓██▒  ▐▌██▒▒▓▓▄ ▄██▒▒▓█  ▄
▒██▒   ░██▒░██▓ ▒██▒▒██▒ ░  ░░██▓ ▒██▒░██░▒██░   ▓██░▒ ▓███▀ ░░▒████▒
░ ▒░   ░  ░░ ▒▓ ░▒▓░▒▓▒░ ░  ░░ ▒▓ ░▒▓░░▓  ░ ▒░   ▒ ▒ ░ ░▒ ▒  ░░░ ▒░ ░
░  ░      ░  ░▒ ░ ▒░░▒ ░       ░▒ ░ ▒░ ▒ ░░ ░░   ░ ▒░  ░  ▒    ░ ░  ░
░      ░     ░░   ░ ░░         ░░   ░  ▒ ░   ░   ░ ░ ░           ░
       ░      ░                 ░      ░           ░ ░ ░         ░  ░
*/


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int N = 100;

#define F first
#define S second
#define PB push_back
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define ios ios_base::sync_with_stdio(0);   cin.tie();  cout.tie();
#define dbg(x)  cerr << x << "\n"

ll n, m, u, v, w, dis[N][N][N], k, sum, r, dp[N][N][N];
pair<pll, ll> e[250000 + 10];
vpll g[N];

void Floyd()
{
    for (int l = 1; l <= m; l++)
    {
        for (int k = 1; k <= n; k++)
        {
            for (int i = 1; i<= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    dis[l][i][j] = min(dis[l][i][j], dis[l][i][k] + dis[l][k][j]);
                }
            }
        }
    }
}

int main()
{
        ios
        cin >> n >> m >> r;
        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                for (int k = 1; k <= n; k++)
                {
                    cin >> dis[i][j][k];
                }
            }
        }
        Floyd();
        memset(dp, 63, sizeof dp);
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                for (int k = 1; k <= m; k++)
                {
                    dp[0][i][j] = min(dp[0][i][j], dis[k][i][j]);

                }
            }
        }
        for (int k = 1; k <= m; k++)
        {
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    for (int l = 1; l <= n; l++)
                    {
                        dp[k][i][j] = min(dp[k][i][j], dp[k - 1][i][l] + dp[0][l][j]);
                    }
                }
            }
        }
        for (int i = 0; i < r; i++)
        {
            cin >> u >> v >> w;
            ll ans = 1e9;
            for (int j = 0; j <= w; j++)
                ans = min(ans, dp[j][u][v]);
            cout << ans << endl;
        }
}

/*
 ███▄ ▄███▓ ██▀███   ██▓███   ██▀███   ██▓ ███▄    █  ▄████▄  ▓█████
▓██▒▀█▀ ██▒▓██ ▒ ██▒▓██░  ██▒▓██ ▒ ██▒▓██▒ ██ ▀█   █ ▒██▀ ▀█  ▓█   ▀
▓██    ▓██░▓██ ░▄█ ▒▓██░ ██▓▒▓██ ░▄█ ▒▒██▒▓██  ▀█ ██▒▒▓█    ▄ ▒███
▒██    ▒██ ▒██▀▀█▄  ▒██▄█▓▒ ▒▒██▀▀█▄  ░██░▓██▒  ▐▌██▒▒▓▓▄ ▄██▒▒▓█  ▄
▒██▒   ░██▒░██▓ ▒██▒▒██▒ ░  ░░██▓ ▒██▒░██░▒██░   ▓██░▒ ▓███▀ ░░▒████▒
░ ▒░   ░  ░░ ▒▓ ░▒▓░▒▓▒░ ░  ░░ ▒▓ ░▒▓░░▓  ░ ▒░   ▒ ▒ ░ ░▒ ▒  ░░░ ▒░ ░
░  ░      ░  ░▒ ░ ▒░░▒ ░       ░▒ ░ ▒░ ▒ ░░ ░░   ░ ▒░  ░  ▒    ░ ░  ░
░      ░     ░░   ░ ░░         ░░   ░  ▒ ░   ░   ░ ░ ░           ░
       ░      ░                 ░      ░           ░ ░ ░         ░  ░
*/


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int N = 63;

#define F first
#define S second
#define PB push_back
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define ios ios_base::sync_with_stdio(0);   cin.tie();  cout.tie();
#define dbg(x)  cerr << x << "\n"

ll n, m, u, v, w, dis[N][N][N], k, sum, r, dp[N][N][N];
pair<pll, ll> e[250000 + 10];
vpll g[N];

void Floyd()
{
    for (int l = 1; l <= m; l++)
    {
        for (int k = 1; k <= n; k++)
        {
            for (int i = 1; i<= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    dis[l][i][j] = min(dis[l][i][j], dis[l][i][k] + dis[l][k][j]);
                }
            }
        }
    }
}

int main()
{
        ios
        cin >> n >> m >> r;
        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                for (int k = 1; k <= n; k++)
                {
                    cin >> dis[i][j][k];
                }
            }
        }
        Floyd();
        memset(dp, 63, sizeof dp);
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                for (int k = 1; k <= m; k++)
                {
                    dp[0][i][j] = min(dp[0][i][j], dis[k][i][j]);

                }
            }
        }
        for (int k = 1; k <= n; k++)
        {
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    for (int l = 1; l <= n; l++)
                    {
                        dp[k][i][j] = min(dp[k][i][j], dp[k - 1][i][l] + dp[0][l][j]);
                    }
                }
            }
        }
        for (int i = 0; i < r; i++)
        {
            cin >> u >> v >> w;
            ll ans = 1e9;
            for (int i = 0; i <= w; i++)
                ans = min(ans, dp[w][u][v]);
            cout << ans << endl;
        }
}

/*
 ███▄ ▄███▓ ██▀███   ██▓███   ██▀███   ██▓ ███▄    █  ▄████▄  ▓█████
▓██▒▀█▀ ██▒▓██ ▒ ██▒▓██░  ██▒▓██ ▒ ██▒▓██▒ ██ ▀█   █ ▒██▀ ▀█  ▓█   ▀
▓██    ▓██░▓██ ░▄█ ▒▓██░ ██▓▒▓██ ░▄█ ▒▒██▒▓██  ▀█ ██▒▒▓█    ▄ ▒███
▒██    ▒██ ▒██▀▀█▄  ▒██▄█▓▒ ▒▒██▀▀█▄  ░██░▓██▒  ▐▌██▒▒▓▓▄ ▄██▒▒▓█  ▄
▒██▒   ░██▒░██▓ ▒██▒▒██▒ ░  ░░██▓ ▒██▒░██░▒██░   ▓██░▒ ▓███▀ ░░▒████▒
░ ▒░   ░  ░░ ▒▓ ░▒▓░▒▓▒░ ░  ░░ ▒▓ ░▒▓░░▓  ░ ▒░   ▒ ▒ ░ ░▒ ▒  ░░░ ▒░ ░
░  ░      ░  ░▒ ░ ▒░░▒ ░       ░▒ ░ ▒░ ▒ ░░ ░░   ░ ▒░  ░  ▒    ░ ░  ░
░      ░     ░░   ░ ░░         ░░   ░  ▒ ░   ░   ░ ░ ░           ░
       ░      ░                 ░      ░           ░ ░ ░         ░  ░
*/


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int N = 63;

#define F first
#define S second
#define PB push_back
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define ios ios_base::sync_with_stdio(0);   cin.tie();  cout.tie();
#define dbg(x)  cerr << x << "\n"

ll n, m, u, v, w, dis[N][N][N], k, sum, r, dp[N][N][N];
pair<pll, ll> e[250000 + 10];
vpll g[N];

void Floyd()
{
    for (int l = 1; l <= m; l++)
    {
        for (int k = 1; k <= n; k++)
        {
            for (int i = 1; i<= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    dis[l][i][j] = min(dis[l][i][j], dis[l][i][k] + dis[l][k][j]);
                }
            }
        }
    }
}

int main()
{
        ios
        cin >> n >> m >> r;
        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                for (int k = 1; k <= n; k++)
                {
                    cin >> dis[i][j][k];
                }
            }
        }
        Floyd();
        memset(dp, 63, sizeof dp);
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                for (int k = 1; k <= m; k++)
                {
                    dp[0][i][j] = min(dp[0][i][j], dis[k][i][j]);

                }
            }
        }
        for (int k = 1; k <= m; k++)
        {
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    for (int l = 1; l <= n; l++)
                    {
                        dp[k][i][j] = min(dp[k][i][j], dp[k - 1][i][l] + dp[0][l][j]);
                    }
                }
            }
        }
        for (int i = 0; i < r; i++)
        {
            cin >> u >> v >> w;
            cout << dp[w][u][v] << endl;
        }
}

/*
 ███▄ ▄███▓ ██▀███   ██▓███   ██▀███   ██▓ ███▄    █  ▄████▄  ▓█████
▓██▒▀█▀ ██▒▓██ ▒ ██▒▓██░  ██▒▓██ ▒ ██▒▓██▒ ██ ▀█   █ ▒██▀ ▀█  ▓█   ▀
▓██    ▓██░▓██ ░▄█ ▒▓██░ ██▓▒▓██ ░▄█ ▒▒██▒▓██  ▀█ ██▒▒▓█    ▄ ▒███
▒██    ▒██ ▒██▀▀█▄  ▒██▄█▓▒ ▒▒██▀▀█▄  ░██░▓██▒  ▐▌██▒▒▓▓▄ ▄██▒▒▓█  ▄
▒██▒   ░██▒░██▓ ▒██▒▒██▒ ░  ░░██▓ ▒██▒░██░▒██░   ▓██░▒ ▓███▀ ░░▒████▒
░ ▒░   ░  ░░ ▒▓ ░▒▓░▒▓▒░ ░  ░░ ▒▓ ░▒▓░░▓  ░ ▒░   ▒ ▒ ░ ░▒ ▒  ░░░ ▒░ ░
░  ░      ░  ░▒ ░ ▒░░▒ ░       ░▒ ░ ▒░ ▒ ░░ ░░   ░ ▒░  ░  ▒    ░ ░  ░
░      ░     ░░   ░ ░░         ░░   ░  ▒ ░   ░   ░ ░ ░           ░
       ░      ░                 ░      ░           ░ ░ ░         ░  ░
*/


//In The Name of Allah
#include <bits/stdc++.h>

using namespace std;

const int N = 70;

int w[N][N][N], dis[N][N][N], W[N][N], ans[N][N][N];

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n, m, q;
    cin >> n >> m >> q;
    for (int i = 1; i <= m; i ++)
        for (int j = 1; j <= n; j ++)
            for (int k = 1; k <= n; k ++)
                cin >> w[i][j][k], dis[i][j][k] = w[i][j][k];
    for (int t = 1; t <= m; t ++)
        for (int k = 1; k <= n; k ++)
            for (int i = 1; i <= n; i ++)
                for (int j = 1; j <= n; j ++)
                    dis[t][i][j] = min(dis[t][i][j], dis[t][i][k] + dis[t][k][j]);
    memset(W, 63, sizeof W);
    for (int t = 1; t <= m; t ++)
        for (int i = 1; i <= n; i ++)
            for (int j = 1; j <= n; j ++)
                W[i][j] = min(W[i][j], dis[t][i][j]);
    memset(ans, 63, sizeof ans);
    for (int i = 1; i <= n; i ++)
        for (int j = 1; j <= n; j ++)
            ans[0][i][j] = W[i][j];
    for (int t = 1; t <= n; t ++)
        for (int k = 1; k <= n; k ++)
            for (int i = 1; i <= n; i ++)
                for (int j = 1; j <= n; j ++)
                    ans[t][i][j] = min(ans[t][i][j], ans[t - 1][i][k] + W[k][j]);
    for (int t = 1; t <= n; t ++)
        for (int i = 1; i <= n; i ++)
            for (int j = 1; j <= n; j ++)
                ans[t][i][j] = min(ans[t][i][j], ans[t - 1][i][j]);
    while (q --){
        int x, y, z;
        cin >> x >> y >> z;
        cout << ans[min(z, n)][x][y] << '\n';
    }
    return 0;
}


//In The Name of Allah
#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second

typedef pair < int , int > pii;
typedef pair < int , pair < int , pii > > piii;

const int N = 70;

int n, m, q, w[N][N][N], dis[N][N][N][N], ans[N][N][N];

priority_queue < piii , vector < piii > , greater < piii > > pq;

void dij(int s){
    for (int i = 1; i <= m; i ++){
        dis[s][s][i][0] = 0;
        pq.push({0, {s, {i, 0}}});
    }
    while (!pq.empty()){
        int d = pq.top().X, v = pq.top().Y.X, p = pq.top().Y.Y.X, t = pq.top().Y.Y.Y;
        pq.pop();
        if (d != dis[s][v][p][t])
            continue;
        for (int i = 1; i <= n; i ++){
            for (int j = 1; j <= m; j ++){
                if (n < t + (j != p))
                    continue;
                if (d + w[j][v][i] < dis[s][i][j][t + (j != p)]){
                    dis[s][i][j][t + (j != p)] = d + w[j][v][i];
                    pq.push({d + w[j][v][i], {i, {j, t + (j != p)}}});
                }
            }
        }
    }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> m >> q;
    for (int i = 1; i <= m; i ++)
        for (int j = 1; j <= n; j ++)
            for (int k = 1; k <= n; k ++)
                cin >> w[i][j][k];
    memset(dis, 31, sizeof dis);
    memset(ans, 31, sizeof ans);
    for (int i = 1; i <= n; i ++)
        dij(i);
    for (int i = 1; i <= n; i ++)
        for (int j = 1; j <= n; j ++)
            for (int k = 1; k <= m; k ++)
                for (int t = 1; t <= n; t ++)
                    dis[i][j][k][t] = min(dis[i][j][k][t], dis[i][j][k][t - 1]);
        for (int i = 1; i <= n; i ++)
            for (int j = 1; j <= n; j ++)
                for (int k = 0; k <= n; k ++)
                    for (int t = 1; t <= m; t ++)
                        ans[i][j][k] = min(ans[i][j][k], dis[i][j][t][k]);
    while (q --){
        int x, y, z;
        cin >> x >> y >> z;
        cout << ans[x][y][min(n, z)] << '\n';
    }
    return 0;
}


//In The Name of Allah
#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second

typedef pair < int , int > pii;
typedef pair < int , pair < int , pii > > piii;

const int N = 70;

int n, m, q, w[N][N][N], dis[N][N][N][N], ans[N][N][N];

priority_queue < piii , vector < piii > , greater < piii > > pq;

void dij(int s){
    for (int i = 1; i <= m; i ++){
        dis[s][s][i][0] = 0;
        pq.push({0, {s, {i, 0}}});
    }
    while (!pq.empty()){
        int d = pq.top().X, v = pq.top().Y.X, p = pq.top().Y.Y.X, t = pq.top().Y.Y.Y;
        pq.pop();
        if (d != dis[s][v][p][t])
            continue;
        for (int i = 1; i <= n; i ++){
            for (int j = 1; j <= m; j ++){
                if (n < t + (j != p))
                    continue;
                if (d + w[j][v][i] < dis[s][i][j][t + (j != p)]){
                    dis[s][i][j][t + (j != p)] = d + w[j][v][i];
                    pq.push({d + w[j][v][i], {i, {j, t + (j != p)}}});
                }
            }
        }
    }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> m >> q;
    for (int i = 1; i <= m; i ++)
        for (int j = 1; j <= n; j ++)
            for (int k = 1; k <= n; k ++)
                cin >> w[i][j][k];
    memset(dis, 31, sizeof dis);
    memset(ans, 31, sizeof ans);
    for (int i = 1; i <= n; i ++)
        dij(i);
    for (int i = 1; i <= n; i ++)
        for (int j = 1; j <= n; j ++)
            for (int k = 1; k <= m; k ++)
                for (int t = 1; t <= n; t ++)
                    dis[i][j][k][t] = min(dis[i][j][k][t], dis[i][j][k][t - 1]);
        for (int i = 1; i <= n; i ++)
            for (int j = 1; j <= n; j ++)
                for (int k = 1; k <= n; k ++)
                    for (int t = 1; t <= m; t ++)
                        ans[i][j][k] = min(ans[i][j][k], dis[i][j][t][k]);
    while (q --){
        int x, y, z;
        cin >> x >> y >> z;
        cout << ans[x][y][min(n, z)] << '\n';
    }
    return 0;
}


//In The Name of Allah
#include <bits/stdc++.h>

using namespace std;

const int N = 70;

int w[N][N][N], dis[N][N][N], W[N][N], ans[N][N][N];

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n, m, q;
    cin >> n >> m >> q;
    for (int i = 1; i <= m; i ++)
        for (int j = 1; j <= n; j ++)
            for (int k = 1; k <= n; k ++)
                cin >> w[i][j][k], dis[i][j][k] = w[i][j][k];
    for (int t = 1; t <= m; t ++)
        for (int k = 1; k <= n; k ++)
            for (int i = 1; i <= n; i ++)
                for (int j = 1; j <= n; j ++)
                    dis[t][i][j] = min(dis[t][i][j], dis[t][i][k] + dis[t][k][j]);
    memset(W, 63, sizeof W);
    for (int t = 1; t <= m; t ++)
        for (int i = 1; i <= n; i ++)
            for (int j = 1; j <= n; j ++)
                W[i][j] = min(W[i][j], dis[t][i][j]);
    memset(ans, 63, sizeof ans);
    for (int i = 1; i <= n; i ++)
        for (int j = 1; j <= n; j ++)
            ans[0][i][j] = W[i][j];
    for (int t = 1; t <= n; t ++)
        for (int k = 1; k <= n; k ++)
            for (int i = 1; i <= n; i ++)
                for (int j = 1; j <= n; j ++)
                    ans[t][i][j] = min(ans[t][i][j], ans[t - 1][i][k] + ans[t - 1][k][j]);
    for (int t = 1; t <= n; t ++)
        for (int i = 1; i <= n; i ++)
            for (int j = 1; j <= n; j ++)
                ans[t][i][j] = min(ans[t][i][j], ans[t - 1][i][j]);
    while (q --){
        int x, y, z;
        cin >> x >> y >> z;
        cout << ans[min(z, n)][x][y] << '\n';
    }
    return 0;
}


#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
#define F first
#define S second
#define fast_io ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
const int N=1e5+10;
const int MOD=1000000007;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<pair<int,int>, null_type,less<pair<int,int> >, rb_tree_tag,tree_order_statistics_node_update>
ll pow(ll x, ll y, int mod){
    if(y==0) return 1;
    ll res=pow(x,y/2);
    return (y%2==0 ? res*res%mod : (res*res%mod)*x%mod);
}
int n,m,r;
int main() {
    fast_io;
    cin >> n >> m >> r;
    ll a[n+2][n+2][m+2][n+2];
    memset(a,63,sizeof a);
    for(int i=1; i<=m; i++){
        for(int j=1; j<=n; j++){
            for(int k=1; k<=n; k++) cin >> a[j][k][i][0];
        }
    }
    for(int p=1; p<=m; p++){
        for(int k=1; k<=n; k++){
            for(int i=1; i<=n; i++){
                for(int j=1; j<=n; j++){
                    a[i][j][p][0]=min(a[i][k][p][0]+a[k][j][p][0],a[i][j][p][0]);
                }
            }
        }
    }
    for(int K=1; K<=n; K++){
        for(int k=1; k<=n; k++){
            for(int i=1; i<=n; i++){
                for(int j=1; j<=n; j++){
                    ll mn=1e12;
                    for(int p=1; p<=m; p++){
                        mn=min(mn,a[i][k][p][K-1]);
                    }
                    for(int p=1; p<=m; p++){
                        a[i][j][p][K]=min(a[i][j][p][K],mn+a[k][j][p][0]);
                    }
                }
            }
        }
    }
    for(int i=0; i<r; i++){
        int x,y,z;
        cin >> x >> y >> z;
        z=min(z,n);
        ll mn=1e12;
        for(int p=1; p<=m; p++){
            mn=min(mn,a[x][y][p][z]);
        }
        cout << mn << '\n';
    }
    return 0;
}


#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
#define F first
#define S second
#define fast_io ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
const int N=1e5+10;
const int MOD=1000000007;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<pair<int,int>, null_type,less<pair<int,int> >, rb_tree_tag,tree_order_statistics_node_update>
ll pow(ll x, ll y, int mod){
    if(y==0) return 1;
    ll res=pow(x,y/2);
    return (y%2==0 ? res*res%mod : (res*res%mod)*x%mod);
}
int n,m,r;
int main() {
    fast_io;
    cin >> n >> m >> r;
    ll a[n+2][n+2][m+2];
    memset(a,63,sizeof a);
    for(int i=1; i<=m; i++){
        for(int j=1; j<=n; j++){
            for(int k=1; k<=n; k++) cin >> a[j][k][i];
        }
    }
    for(int p=1; p<=m; p++){
        for(int k=1; k<=n; k++){
            for(int i=1; i<=n; i++){
                for(int j=1; j<=n; j++){
                    a[i][j][p]=min(a[i][k][p]+a[k][j][p],a[i][j][p]);
                }
            }
        }
    }
    ll dp[n+2][n+2][n+2];
    memset(dp,63,sizeof dp);
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            for(int p=1; p<=m; p++){
                dp[i][j][0]=min(dp[i][j][0],a[i][j][p]);
            }
        }
    }
    for(int K=1; K<=n; K++){
        for(int k=1; k<=n; k++){
            for(int i=1; i<=n; i++){
                for(int j=1; j<=n; j++){
                    dp[i][j][K]=min(dp[i][j][K],dp[i][k][K-1]+dp[k][j][0]);
                }
            }
        }
    }
    for(int i=0; i<r; i++){
        int x,y,z;
        cin >> x >> y >> z;
        z=min(z,n);
        cout << dp[x][y][z] << '\n';
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int MAXN = 62, MAXQ = 1000 + 1;
int numberOfVertices, numberOfCars;
int mindistance[MAXQ][MAXN][MAXN];
int cars[MAXN][MAXN][MAXN];

void floydWarshal () {
	for (int car = 0; car < numberOfCars; ++car) {
		for (int i = 0; i < numberOfVertices; ++i) {
			for (int v = 0; v < numberOfVertices; ++v) {
				for (int u = 0; u < numberOfVertices; ++u) {
					if (cars[car][v][u] > cars[car][v][i] + cars[car][i][u]) {
						cars[car][v][u] = cars[car][v][i] + cars[car][i][u];
					}

					if (mindistance[0][v][u] > cars[car][v][u]) {
						mindistance[0][v][u] = cars[car][v][u];
					}
				}
			}
		}
	}

	for (int i = 1; i < MAXQ; ++i) {
		for (int j = 0; j < numberOfVertices; ++j) {
			for (int k = 0; k < numberOfVertices; ++k) {
				for (int l = 0; l < numberOfVertices; ++l) {
					if (mindistance[i - 1][j][l] + mindistance[0][l][k] < mindistance[i][j][k]) {
						mindistance[i][j][k] = mindistance[i - 1][j][l] + mindistance[0][l][k];
					}
				}
			}
		}
	}

	return;
}

int main() {
	int numberOfQueries;
	scanf("%d%d%d", &numberOfVertices, &numberOfCars, &numberOfQueries);
	for (int car = 0; car < numberOfCars; ++car) {
		for (int i = 0; i < numberOfVertices; ++i) {
			for (int j = 0; j < numberOfVertices; ++j) {
				scanf("%d", &cars[car][i][j]);
			}
		}
	}

	memset(mindistance, 63, sizeof mindistance);
	floydWarshal();

	int startingCity, finishingCity, carChange;
	for (int i = 0; i < numberOfQueries; ++i) {
		scanf("%d%d%d", &startingCity, &finishingCity, &carChange);
		printf("%d\n", mindistance[carChange][startingCity - 1][finishingCity - 1]);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int maxn = 60 + 5;

const long long inf = 1e18;

long long dp[maxn][maxn][maxn];

int main()
{
	int n, m, r;
	cin >> n >> m >> r;
	long long a[m][n][n];
	for(int i = 0; i < m; i++)
        for(int j = 0; j < n; j++)
			for(int k = 0; k < n; k++)
					cin >> a[i][j][k];

    for(int i = 0; i < maxn; i++)
		for (int j = 0; j < maxn; j++)
            fill(dp[i][j], dp[i][j] + maxn, inf);

	for (int i = 0; i < m; i++)
		for (int j = 0; j < n; j++)
			for (int k = 0; k < n; k++)
				for (int l = 0; l < n; l++){
                    a[i][k][l] = min(a[i][k][l], a[i][k][j] + a[i][j][l]);
                    dp[0][k][l] = min(dp[0][k][l], a[i][k][l]);
				}

	for (int i = 1; i < n; i++)
		for (int j = 0; j < n; j++)
			for (int k = 0; k < n; k++)
				for (int l = 0; l < n; l++)
					dp[i][k][l] = min(dp[i - 1][k][j] + dp[0][j][l], dp[i][k][l]);

	for (int i = 0; i < r; i++){
        int s, t, k;
		cin >> s >> t >> k;
		s--;
		t--;
        k = min(k, n - 2);
		cout << dp[k][s][t] << endl;
	}

	return 0;
}


// Problem : B. AlgoRace
// Contest : Codeforces - Codeforces Round #119 (Div. 1)
// URL : https://codeforces.com/problemset/problem/187/B
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define db long double
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define mi map<int,int>
#define ml map<ll,ll>
#define mii map<pii,int>
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()
#define sz(x) (int)x.size()
#define mod 1000000007
#define MAX 4294967295
#define EPS 1e-9
#define io ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define MAXN 15000005
#define inf 1e8
#define PI acos(-1.0)
#define N 65

int n,m,r,d[N][N],dp[N][N][N];

void solve(){
    cin>>n>>m>>r;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++) dp[0][i][j] = inf;
    }
    for(int l=0;l<m;l++){
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++) cin>>d[i][j];
        }
        for(int k=0;k<n;k++){
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++)
                    d[i][j] = min(d[i][j], d[i][k]+d[k][j]);
            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++)
                dp[0][i][j] = min(dp[0][i][j], d[i][j]);
        }
    }
    for(int l=1;l<n;l++){
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                dp[l][i][j] = dp[l-1][i][j];
                for(int k=0;k<n;k++)
                    dp[l][i][j] = min(dp[l][i][j], dp[l-1][i][k]+dp[0][k][j]);
            }
        }
    }
    while(r--){
        int x,y,k;
        cin>>x>>y>>k;
        x--, y--;
        k = min(k,n-1);
        cout<<dp[k][x][y]<<endl;
    }
}

signed main(){
    io;
    //freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    int t;
    //cin>>t;
    t = 1;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>

#define ff first
#define ss second
#define pb push_back
#define FOR(i,j,k) for(int i = j; i < (int)(k); i++)
#define FORV(i, v) FOR(i, 0, ((v).size()))
#define REP(i,j,k) for(int i = j; i >= (int)(k); i--)
#define setmax(i) const int maxn = (int) i;
#define setmod(i) const int MOD = (int) i;
#define all(a) a.begin(),a.end()
#define autodef(x,v) typeof(v) x = (v)
#define autoref(x,v) typeof(v)& x = (v)
#define forit(it, c) for (autodef(it, ((c).begin())); it != ((c).end()); ++it)

//typedef complex<double> Point;
//#define X real()
//#define Y imag()

using namespace std;

//ifstream fin("");
//ofstream fout("");
//#define cin fin
//#define cout fout

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
typedef pair<pii,int> ppi;
typedef pair<pll,ll> ppl;
typedef pair<int,pii> pip;
typedef pair<ll,pll> plp;
typedef pair<pii,pii> ppp;

const int INF = (int) 2e9;
const ll INFL = (ll) 9e18;
const int MAXINT = ((~0) ^ (1 << 31));
const ll MAXLL = ((~0) ^ ((ll)1 << 63));

template<class T> inline T pow2(T a) { return a*a;}
template<class T> inline bool mineq(T& a, T b){return (a > b) ? (a=b, true) : false;}
template<class T> inline bool maxeq(T& a, T b){return (a < b) ? (a=b, true) : false;}

//srand (time(NULL));

const int maxn = 1e2;

ll n, m, dp[maxn][maxn][2000], v, u, w, r, s, t, c, a[maxn][maxn];

int main(){
    ios_base::sync_with_stdio(0);
    cin >> n >> m >> r;
    for(int i = 1; i <= n; i ++){
        for(int j = 1; j <= n; j ++)
            memset(dp[i][j], 63, sizeof dp[i][j]);
    }
    for(int z = 0; z < m; z ++){
        for(int i = 1; i <= n; i ++){
            for(int j = 1; j <= n; j ++){
                cin >> a[i][j];
            }
        }
        for(int k = 1; k <= n; k ++){
            for(int i = 1; i <= n; i ++){
                for(int j = 1; j <= n; j ++)
                    a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
            }
        }
        for(int i = 1; i <= n; i ++){
            for(int j = 1; j <= n; j ++)
                dp[i][j][0] = min(dp[i][j][0], a[i][j]);
        }
    }
    for(int i = 1; i <= n; i ++){
        for(int j = 1; j <= n; j ++){
            for(int t = 1; t < 1100; t ++){
                dp[i][j][t] = dp[i][j][0];
            }
        }
    }
    for(int t = 1; t <= 1100; t ++){
        for(int k = 1; k <= n; k ++){
            for(int i = 1; i <= n; i ++){
                for(int j = 1; j <= n; j ++){
                    dp[i][j][t] = min(dp[i][j][t], dp[i][k][t - 1] + dp[k][j][1]);
                }
            }
        }
    }
    for(int i = 0; i < r; i ++){
        cin >> s >> t >> c;
        printf("%lld\n", dp[s][t][c]);
    }
}


#include <bits/stdc++.h>

#define ff first
#define ss second
#define pb push_back
#define FOR(i,j,k) for(int i = j; i < (int)(k); i++)
#define FORV(i, v) FOR(i, 0, ((v).size()))
#define REP(i,j,k) for(int i = j; i >= (int)(k); i--)
#define setmax(i) const int maxn = (int) i;
#define setmod(i) const int MOD = (int) i;
#define all(a) a.begin(),a.end()
#define autodef(x,v) typeof(v) x = (v)
#define autoref(x,v) typeof(v)& x = (v)
#define forit(it, c) for (autodef(it, ((c).begin())); it != ((c).end()); ++it)

//typedef complex<double> Point;
//#define X real()
//#define Y imag()

using namespace std;

//ifstream fin("");
//ofstream fout("");
//#define cin fin
//#define cout fout

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
typedef pair<pii,int> ppi;
typedef pair<pll,ll> ppl;
typedef pair<int,pii> pip;
typedef pair<ll,pll> plp;
typedef pair<pii,pii> ppp;

const int INF = (int) 2e9;
const ll INFL = (ll) 9e18;
const int MAXINT = ((~0) ^ (1 << 31));
const ll MAXLL = ((~0) ^ ((ll)1 << 63));

template<class T> inline T pow2(T a) { return a*a;}
template<class T> inline bool mineq(T& a, T b){return (a > b) ? (a=b, true) : false;}
template<class T> inline bool maxeq(T& a, T b){return (a < b) ? (a=b, true) : false;}

//srand (time(NULL));

const int maxn = 1e2;

ll n, m, dp[maxn][maxn][maxn], r, s, t, c, a[maxn][maxn];

int main(){
    ios_base::sync_with_stdio(0);
    cin >> n >> m >> r;
    for(int i = 1; i <= n; i ++){
        for(int j = 1; j <= n; j ++)
            memset(dp[i][j], 63, sizeof dp[i][j]);
    }
    for(int z = 0; z < m; z ++){
        for(int i = 1; i <= n; i ++){
            for(int j = 1; j <= n; j ++){
                cin >> a[i][j];
            }
        }
        for(int k = 1; k <= n; k ++){
            for(int i = 1; i <= n; i ++){
                for(int j = 1; j <= n; j ++)
                    a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
            }
        }
        for(int i = 1; i <= n; i ++){
            for(int j = 1; j <= n; j ++)
                dp[i][j][0] = min(dp[i][j][0], a[i][j]);
        }
    }
    for(int i = 1; i <= n; i ++){
        for(int j = 1; j <= n; j ++){
            for(int t = 1; t <= n; t ++){
                dp[i][j][t] = dp[i][j][0];
            }
        }
    }
    for(int t = 1; t <= n; t ++){
        for(int k = 1; k <= n; k ++){
            for(int i = 1; i <= n; i ++){
                for(int j = 1; j <= n; j ++){
                    dp[i][j][t] = min(dp[i][j][t], dp[i][k][t - 1] + dp[k][j][0]);
                }
            }
        }
    }
    for(int i = 0; i < r; i ++){
        cin >> s >> t >> c;
        cout << dp[s][t][min(c, n)] << endl;
    }
}


#include <bits/stdc++.h>

#define ff first
#define ss second
#define pb push_back
#define FOR(i,j,k) for(int i = j; i < (int)(k); i++)
#define FORV(i, v) FOR(i, 0, ((v).size()))
#define REP(i,j,k) for(int i = j; i >= (int)(k); i--)
#define setmax(i) const int maxn = (int) i;
#define setmod(i) const int MOD = (int) i;
#define all(a) a.begin(),a.end()
#define autodef(x,v) typeof(v) x = (v)
#define autoref(x,v) typeof(v)& x = (v)
#define forit(it, c) for (autodef(it, ((c).begin())); it != ((c).end()); ++it)

//typedef complex<double> Point;
//#define X real()
//#define Y imag()

using namespace std;

//ifstream fin("");
//ofstream fout("");
//#define cin fin
//#define cout fout

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
typedef pair<pii,int> ppi;
typedef pair<pll,ll> ppl;
typedef pair<int,pii> pip;
typedef pair<ll,pll> plp;
typedef pair<pii,pii> ppp;

const int INF = (int) 2e9;
const ll INFL = (ll) 9e18;
const int MAXINT = ((~0) ^ (1 << 31));
const ll MAXLL = ((~0) ^ ((ll)1 << 63));

template<class T> inline T pow2(T a) { return a*a;}
template<class T> inline bool mineq(T& a, T b){return (a > b) ? (a=b, true) : false;}
template<class T> inline bool maxeq(T& a, T b){return (a < b) ? (a=b, true) : false;}

//srand (time(NULL));

const int maxn = 1e2;

ll n, m, dp[maxn][maxn][maxn], v, u, w, r, s, t, c, a[maxn][maxn];

int main(){
    ios_base::sync_with_stdio(0);
    cin >> n >> m >> r;
    for(int i = 1; i <= n; i ++){
        for(int j = 1; j <= n; j ++)
            memset(dp[i][j], 63, sizeof dp[i][j]);
    }
    for(int z = 0; z < m; z ++){
        for(int i = 1; i <= n; i ++){
            for(int j = 1; j <= n; j ++){
                cin >> a[i][j];
            }
        }
        for(int k = 1; k <= n; k ++){
            for(int i = 1; i <= n; i ++){
                for(int j = 1; j <= n; j ++)
                    a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
            }
        }
        for(int i = 1; i <= n; i ++){
            for(int j = 1; j <= n; j ++)
                dp[i][j][0] = min(dp[i][j][0], a[i][j]);
        }
    }
    for(int i = 1; i <= n; i ++){
        for(int j = 1; j <= n; j ++){
            for(int t = 1; t <= n; t ++){
                dp[i][j][t] = dp[i][j][0];
            }
        }
    }
    for(int t = 1; t <= n; t ++){
        for(int k = 1; k <= n; k ++){
            for(int i = 1; i <= n; i ++){
                for(int j = 1; j <= n; j ++){
                    dp[i][j][t] = min(dp[i][j][t], dp[i][k][t - 1] + dp[k][j][1]);
                }
            }
        }
    }
    /*cout << "--------" << endl;
    for(int i = 1; i <= n; i ++){
        for(int j = i + 1; j <= n; j ++){
            cout << i << " " << j <<" : " << endl;
            for(int k = 1; k <= n; k ++)
                cout << dp[i][j][k] << endl;
        }
    }*/
    for(int i = 0; i < r; i ++){
        cin >> s >> t >> c;
        cout << dp[s][t][min(c, n)] << endl;
    }
}


#include <bits/stdc++.h>

#define ff first
#define ss second
#define pb push_back
#define FOR(i,j,k) for(int i = j; i < (int)(k); i++)
#define FORV(i, v) FOR(i, 0, ((v).size()))
#define REP(i,j,k) for(int i = j; i >= (int)(k); i--)
#define setmax(i) const int maxn = (int) i;
#define setmod(i) const int MOD = (int) i;
#define all(a) a.begin(),a.end()
#define autodef(x,v) typeof(v) x = (v)
#define autoref(x,v) typeof(v)& x = (v)
#define forit(it, c) for (autodef(it, ((c).begin())); it != ((c).end()); ++it)

//typedef complex<double> Point;
//#define X real()
//#define Y imag()

using namespace std;

//ifstream fin("");
//ofstream fout("");
//#define cin fin
//#define cout fout

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
typedef pair<pii,int> ppi;
typedef pair<pll,ll> ppl;
typedef pair<int,pii> pip;
typedef pair<ll,pll> plp;
typedef pair<pii,pii> ppp;

const int INF = (int) 2e9;
const ll INFL = (ll) 9e18;
const int MAXINT = ((~0) ^ (1 << 31));
const ll MAXLL = ((~0) ^ ((ll)1 << 63));

template<class T> inline T pow2(T a) { return a*a;}
template<class T> inline bool mineq(T& a, T b){return (a > b) ? (a=b, true) : false;}
template<class T> inline bool maxeq(T& a, T b){return (a < b) ? (a=b, true) : false;}

//srand (time(NULL));

const int maxn = 1e2;

ll n, m, dp[maxn][maxn][2000], v, u, w, r, s, t, c, a[maxn][maxn];

int main(){
    ios_base::sync_with_stdio(0);
    cin >> n >> m >> r;
    for(int i = 1; i <= n; i ++){
        for(int j = 1; j <= n; j ++)
            memset(dp[i][j], 63, sizeof dp[i][j]);
    }
    for(int z = 0; z < m; z ++){
        for(int i = 1; i <= n; i ++){
            for(int j = 1; j <= n; j ++){
                cin >> a[i][j];
            }
        }
        for(int k = 1; k <= n; k ++){
            for(int i = 1; i <= n; i ++){
                for(int j = 1; j <= n; j ++)
                    a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
            }
        }
        for(int i = 1; i <= n; i ++){
            for(int j = 1; j <= n; j ++)
                dp[i][j][0] = min(dp[i][j][0], a[i][j]);
        }
    }
    for(int i = 1; i <= n; i ++){
        for(int j = 1; j <= n; j ++){
            for(int t = 1; t < 1100; t ++){
                dp[i][j][t] = dp[i][j][0];
            }
        }
    }
    for(int t = 1; t <= 1100; t ++){
        for(int k = 1; k <= n; k ++){
            for(int i = 1; i <= n; i ++){
                for(int j = 1; j <= n; j ++){
                    dp[i][j][t] = min(dp[i][j][t], dp[i][k][t - 1] + dp[k][j][1]);
                }
            }
        }
    }
    for(int i = 0; i < r; i ++){
        cin >> s >> t >> c;
        cout << dp[s][t][c] << endl;
    }
}


#include <bits/stdc++.h>

#define ff first
#define ss second
#define pb push_back
#define FOR(i,j,k) for(int i = j; i < (int)(k); i++)
#define FORV(i, v) FOR(i, 0, ((v).size()))
#define REP(i,j,k) for(int i = j; i >= (int)(k); i--)
#define setmax(i) const int maxn = (int) i;
#define setmod(i) const int MOD = (int) i;
#define all(a) a.begin(),a.end()
#define autodef(x,v) typeof(v) x = (v)
#define autoref(x,v) typeof(v)& x = (v)
#define forit(it, c) for (autodef(it, ((c).begin())); it != ((c).end()); ++it)

//typedef complex<double> Point;
//#define X real()
//#define Y imag()

using namespace std;

//ifstream fin("");
//ofstream fout("");
//#define cin fin
//#define cout fout

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
typedef pair<pii,int> ppi;
typedef pair<pll,ll> ppl;
typedef pair<int,pii> pip;
typedef pair<ll,pll> plp;
typedef pair<pii,pii> ppp;

const int INF = (int) 2e9;
const ll INFL = (ll) 9e18;
const int MAXINT = ((~0) ^ (1 << 31));
const ll MAXLL = ((~0) ^ ((ll)1 << 63));

template<class T> inline T pow2(T a) { return a*a;}
template<class T> inline bool mineq(T& a, T b){return (a > b) ? (a=b, true) : false;}
template<class T> inline bool maxeq(T& a, T b){return (a < b) ? (a=b, true) : false;}

//srand (time(NULL));

const int maxn = 1e2;

ll n, m, dp[maxn][maxn][2000], v, u, w, r, s, t, c, a[maxn][maxn];

int main(){
    ios_base::sync_with_stdio(0);
    cin >> n >> m >> r;
    for(int i = 1; i <= n; i ++){
        for(int j = 1; j <= n; j ++)
            memset(dp[i][j], 63, sizeof dp[i][j]);
    }
    for(int z = 0; z < m; z ++){
        for(int i = 1; i <= n; i ++){
            for(int j = 1; j <= n; j ++){
                cin >> a[i][j];
            }
        }
        for(int k = 1; k <= n; k ++){
            for(int i = 1; i <= n; i ++){
                for(int j = 1; j <= n; j ++)
                    a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
            }
        }
        for(int i = 1; i <= n; i ++){
            for(int j = 1; j <= n; j ++)
                dp[i][j][0] = min(dp[i][j][0], a[i][j]);
        }
    }
    for(int i = 1; i <= n; i ++){
        for(int j = 1; j <= n; j ++){
            for(int t = 1; t < 1100; t ++){
                dp[i][j][t] = dp[i][j][0];
            }
        }
    }
    for(int t = 1; t <= 1100; t ++){
        for(int k = 1; k <= n; k ++){
            for(int i = 1; i <= n; i ++){
                for(int j = 1; j <= n; j ++){
                    dp[i][j][t] = min(dp[i][j][t], dp[i][k][t - 1] + dp[k][j][1]);
                }
            }
        }
    }
    for(int i = 0; i < r; i ++){
        scanf("%lld", &s);
        scanf("%lld", &t);
        scanf("%lld", &c);
        printf("%lld\n", dp[s][t][c]);
    }
}


// 187B
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1000 * 1000 * 1000;
const ll LINF = (ll)INF * INF;

#define dbg(x) cout << #x << " = " << (x) << endl;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define dbg_arr(x, a, b) cout << "x: "; FOR(i, (a), (b)) { cout << (x)[i] << ' '; } cout << endl;
#define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define pb push_back

#define MAXN 70

int adj[MAXN][MAXN][MAXN];
int dp[MAXN][MAXN][MAXN];
int dist[MAXN][MAXN][MAXN];
int n;

void fld(int c)
{
	// base
	FOR(i, 1, n + 1)
	{
		FOR(j, 1, n + 1)
		{
			dist[c][i][j] = adj[c][i][j];
		}
	}
	// dp
	FOR(k, 1, n + 1)
	{
		FOR(i, 1, n + 1)
		{
			FOR(j, 1, n + 1)
			{
				dist[c][i][j] = min(dist[c][i][j], dist[c][i][k] + dist[c][k][j]);
			}
		}
	}
}

int main(void)
{
	int m, r;
	cin >> n >> m >> r;
	FOR(i, 1, m + 1)
	{
		FOR(j, 1, n + 1)
		{
			FOR(k, 1, n + 1)
			{
				cin >> adj[i][j][k];
			}
		}
	}

	// find dp from dist
	FOR(i, 1, m + 1)
	{
		fld(i);
	}
	FOR(i, 1, n + 1)
	{
		FOR(j, 1, n + 1)
		{
			fill(dp[i][j], dp[i][j] + MAXN, INF);
			FOR(c, 1, m + 1)
			{
				dp[i][j][0] = min(dp[i][j][0], dist[c][i][j]);
			}
		}
	}

	// dp
	FOR(k, 1, n + 1)
	{
		FOR(i, 1, n + 1)
		{
			FOR(j, 1, n + 1)
			{
				FOR(l, 1, n + 1)
				{
					dp[i][j][k] = min(dp[i][j][k], dp[i][l][0] + dp[l][j][k - 1]);
				}
			}
		}
	}

	// answer queries
	FOR(i, 0, r)
	{
		int si, ti, ki;
		cin >> si >> ti >> ki;
		ki = min(ki, n);
		cout << dp[si][ti][ki] << endl;
	}

	return 0;
}


#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
typedef long long                   ll;
typedef long double                 ld;
typedef pair<int,int>               pii;
typedef pair<ll,ll>                 pll;
typedef pair<double,double>         pdd;
typedef pair<ld,ld>                 pld;
typedef pair<string , string>       pss;
#define ts(x)                       to_string(x)
#define ti(x)                       stoi(x)
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second
#define Mp                          make_pair
#define tab                         '\t'
#define sep                         ' '
#define mine(v)                     *min_element(all(v))
#define maxe(v)                     *max_element(all(v))
#define error(x)                    cerr << #x << " = " << x << endl
#define fast_io                     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io                     freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
#define SIZE(x)                     (int)x.size()
#define endl                        "\n"

//template<class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

inline ll input(){ll n; cin >> n ; return n;}

//void add(int I, int value){seg[I += MAXN] = value;for (I >>= 1; I; I >>= 1){seg[I] = __gcd(seg[I << 1], seg[I << 1 | 1]);}}

//int query(int l, int r){int ans = seg[l + MAXN];for (l += MAXN, r += MAXN + 1; l < r; l >>= 1, r >>= 1){if(l & 1) ans = __gcd(ans, seg[l ++]);if(r & 1) ans = __gcd(ans, seg[-- r]);}return ans;}

string base2(ll n){string a = "";while(n >= 2){a += (char)(n % 2 + '0');n /= 2;}a += (char)(n + '0');reverse(all(a));return a;}

const int MAXN = 60 + 2;
const int MOD = 1e9 + 7;
const int MOD2 = 998244353;
const ll INF = 8e18;

ll pw(ll a , ll b, ll mod)  { return (!b ? 1 : (b & 1 ? (a * pw(a * a % mod, b / 2, mod)) % mod : pw(a * a % mod, b / 2, mod))); }

// dp[k][i][j] : start = i, end = j, moves = k

ll dp[MAXN][MAXN][MAXN], M[MAXN][MAXN][MAXN], dis[MAXN][MAXN][MAXN], Mdis[MAXN][MAXN];

int main(){
    fast_io;
    //cout << fixed << setprecision(15);
    //scanf("%d %d %f %lld %s %c", &x);
    //printf("%d %d %f %lld %s %c", x);
    int n, m, q;
    cin >> n >> m >> q;
    for(int i = 1; i <= m; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            for(int t = 1; t <= n; t++)
            {
                cin >> M[i][j][t];
            }
        }
    }
    for(int i = 1; i <= n; i++)for(int j = 1; j <= n; j++)Mdis[i][j] = 1e18;
    for(int t = 1; t <= m; t++)
    {
        for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) dis[t][i][j] = M[t][i][j];
        for(int k = 1; k <= n; k++)
        {
            for(int i = 1; i <= n; i++)
            {
                for(int j = 1; j <= n; j++)
                {
                    dis[t][i][j] = min(dis[t][i][j], dis[t][i][k] + dis[t][k][j]);
                }
            }
        }
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                Mdis[i][j] = min(Mdis[i][j], dis[t][i][j]);
            }
        }
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            dp[1][i][j] = Mdis[i][j];
        }
    }
    for(int k = 2; k <= n; k++)
    {
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                dp[k][i][j] = 1e18;
            }
        }
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                for(int cu = 1; cu <= n; cu++)
                {
                    dp[k][i][j] = min({dp[k - 1][i][j], dp[k][i][j], dp[k - 1][i][cu] + Mdis[cu][j]});
                }
            }
        }
    }
    /*
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            for(int k = 1; k <= n; k++)
            {
                cout << dp[k][i][j] << " ";
            }
            cout << endl;
        }
        cout << endl;
    }
    */
    while(q--)
    {
        int a, b, c;
        cin >> a >> b >> c;
        cout << dp[c + 1][a][b] << endl;
    }
    return 0;
}


#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
typedef long long                   ll;
typedef long double                 ld;
typedef pair<int,int>               pii;
typedef pair<ll,ll>                 pll;
typedef pair<double,double>         pdd;
typedef pair<ld,ld>                 pld;
typedef pair<string , string>       pss;
#define ts(x)                       to_string(x)
#define ti(x)                       stoi(x)
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second
#define Mp                          make_pair
#define tab                         '\t'
#define sep                         ' '
#define mine(v)                     *min_element(all(v))
#define maxe(v)                     *max_element(all(v))
#define error(x)                    cerr << #x << " = " << x << endl
#define fast_io                     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io                     freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
#define SIZE(x)                     (int)x.size()
#define endl                        "\n"

//template<class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

inline ll input(){ll n; cin >> n ; return n;}

//void add(int I, int value){seg[I += MAXN] = value;for (I >>= 1; I; I >>= 1){seg[I] = __gcd(seg[I << 1], seg[I << 1 | 1]);}}

//int query(int l, int r){int ans = seg[l + MAXN];for (l += MAXN, r += MAXN + 1; l < r; l >>= 1, r >>= 1){if(l & 1) ans = __gcd(ans, seg[l ++]);if(r & 1) ans = __gcd(ans, seg[-- r]);}return ans;}

string base2(ll n){string a = "";while(n >= 2){a += (char)(n % 2 + '0');n /= 2;}a += (char)(n + '0');reverse(all(a));return a;}

const int MAXN = 60 + 2;
const int MOD = 1e9 + 7;
const int MOD2 = 998244353;
const ll INF = 8e18;

ll pw(ll a , ll b, ll mod)  { return (!b ? 1 : (b & 1 ? (a * pw(a * a % mod, b / 2, mod)) % mod : pw(a * a % mod, b / 2, mod))); }

// dp[k][i][j] : start = i, end = j, moves = k

ll dp[1100][MAXN][MAXN], M[MAXN][MAXN][MAXN], dis[MAXN][MAXN][MAXN], Mdis[MAXN][MAXN];

int main(){
    fast_io;
    //cout << fixed << setprecision(15);
    //scanf("%d %d %f %lld %s %c", &x);
    //printf("%d %d %f %lld %s %c", x);
    int n, m, q;
    cin >> n >> m >> q;
    for(int i = 1; i <= m; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            for(int t = 1; t <= n; t++)
            {
                cin >> M[i][j][t];
            }
        }
    }
    for(int i = 1; i <= n; i++)for(int j = 1; j <= n; j++)Mdis[i][j] = 1e18;
    for(int t = 1; t <= m; t++)
    {
        for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) dis[t][i][j] = M[t][i][j];
        for(int k = 1; k <= n; k++)
        {
            for(int i = 1; i <= n; i++)
            {
                for(int j = 1; j <= n; j++)
                {
                    dis[t][i][j] = min(dis[t][i][j], dis[t][i][k] + dis[t][k][j]);
                }
            }
        }
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                Mdis[i][j] = min(Mdis[i][j], dis[t][i][j]);
            }
        }
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            dp[1][i][j] = Mdis[i][j];
        }
    }
    for(int k = 2; k <= 1001; k++)
    {
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                dp[k][i][j] = 1e18;
            }
        }
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                for(int cu = 1; cu <= n; cu++)
                {
                    if(cu == j)continue;
                    dp[k][i][j] = min({dp[k - 1][i][j], dp[k][i][j], dp[k - 1][i][cu] + Mdis[cu][j]});
                }
            }
        }
    }
    /*
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            for(int k = 1; k <= n; k++)
            {
                cout << dp[k][i][j] << " ";
            }
            cout << endl;
        }
        cout << endl;
    }
    */
    while(q--)
    {
        int a, b, c;
        cin >> a >> b >> c;
        cout << dp[c + 1][a][b] << endl;
    }
    return 0;
}


#include <iostream>

using namespace std ;

typedef long long ll ;
const int N=70 , INF=1e9+7;
int n , m , r , ans[N][N][N] , oneCAR[N][N];

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> n >> m >> r ;

	for(int i=0 ; i<=n ; i++)
		for(int j=0 ; j<=n ; j++)
		{
			oneCAR[i][j] = INF ;

			for(int k=0 ; k<=n ; k++)
				ans[i][j][k] = INF ;
		}

	for(int i=0 ; i<m ; i++)
	{
		for(int j=1 ; j<=n ; j++)
			for(int k=1 ; k<=n ; k++)
				cin >> oneCAR[j][k];

		for(int l=1; l<=n ; l++)
			for(int j=1 ; j<=n ; j++)
				for(int k=1 ; k<=n ; k++)
				{
					oneCAR[j][k] = min(oneCAR[j][k] , oneCAR[j][l]+oneCAR[l][k]);

					ans[j][k][0] = min(ans[j][k][0] , oneCAR[j][k]);
				}
	}

	for(int k=1 ; k<=n ; k++)
		for(int i=1 ; i<=n ; i++)
			for(int j=1 ; j<=n ; j++)
				for(int l=1 ; l<=n ; l++)
					ans[i][j][k]=min(ans[i][j][k] , ans[i][l][k-1]+ans[l][j][0]);

	while(r--)
	{
		int s , t , k ;
		cin >> s >> t >> k ;

		k = min(n , k);

		cout << ans[s][t][k] << endl ;
	}

	return 0;
}


#include <iostream>

using namespace std ;

typedef long long ll ;
const int N=60 , INF=1e9+7;
int n , m , r , ans[N][N][N] , oneCAR[N][N];

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> n >> m >> r ;

	for(int i=0 ; i<=n ; i++)
		for(int j=0 ; j<=n ; j++)
		{
			oneCAR[i][j] = INF ;

			for(int k=0 ; k<=n ; k++)
				ans[i][j][k] = INF ;
		}

	for(int i=0 ; i<m ; i++)
	{
		for(int j=1 ; j<=n ; j++)
			for(int k=1 ; k<=n ; k++)
				cin >> oneCAR[j][k];

		for(int l=1; l<=n ; l++)
			for(int j=1 ; j<=n ; j++)
				for(int k=1 ; k<=n ; k++)
				{
					oneCAR[j][k] = min(oneCAR[j][k] , oneCAR[j][l]+oneCAR[l][k]);

					ans[j][k][0] = min(ans[j][k][0] , oneCAR[j][k]);
				}
	}

	for(int k=1 ; k<=n ; k++)
		for(int i=1 ; i<=n ; i++)
			for(int j=1 ; j<=n ; j++)
				for(int l=1 ; l<=n ; l++)
					ans[i][j][k]=min(ans[i][j][k] , ans[i][l][k-1]+ans[l][j][0]);

	while(r--)
	{
		int s , t , k ;
		cin >> s >> t >> k ;

		k = min(n , k);

		cout << ans[s][t][k] << endl ;
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;
const int MAXN = 65, MAXK = 1010;
int n, m, r, c[MAXN][MAXN][MAXN], dist[MAXN], dp[MAXN][MAXN][MAXK];

void dij(int r, int comp)
{
	set <pair<int,int>> s;
	for(int i = 0; i < n; i++)
	{
		if(i != r)
		{
			dist[i] = INT_MAX;
			s.insert({dist[i], i});
		}
	}
	dist[r] = 0;
	s.insert({0, r});
	for(int i = 0; i < n; i++)
	{
		int mn = s.begin()->second;
		s.erase(*s.begin());
		for(int j = 0; j < n; j++)
		{
			if(j != mn && dist[j] > dist[mn] + c[mn][j][comp])
			{
				s.erase({dist[j], j});
				dist[j] = dist[mn] + c[mn][j][comp];
				s.insert({dist[j], j});
			}
		}
	}
	/*cout << "RRR" << r << endl;
	for(int i = 0; i < n; i++)
		cout << dist[i] << " ";
	cout << endl;*/
	for(int i = 0; i < n; i++)
	{
		dp[r][i][0] = min(dp[r][i][0], dist[i]);
	}
	return;
}

int main()
{
	//ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	//cin >> n >> m >> r;
	scanf("%d", &n);
	scanf("%d", &m);
	scanf("%d", &r);
	for(int j = 0; j < m; j++)
	{
		for(int i = 0; i < n; i++)
		{
			for(int i2 = 0; i2 < n; i2++)
				scanf("%d", &c[i][i2][j]);
			//	cin >> c[i][i2][j];
		}
	}
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n; j++)
		{
			if(i != j)
				dp[i][j][0] = INT_MAX;
		}
	}
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < m; j++)
			dij(i, j);
	}
	for(int k = 1; k < MAXK; k++)
	{
		for(int i = 0; i < n; i++)
		{
			for(int j = 0; j < n; j++)
			{
				dp[i][j][k] = dp[i][j][k - 1];
				for(int h = 0; h < n; h++)
				{
					dp[i][j][k] = min(dp[i][j][k], dp[i][h][k - 1] + dp[h][j][0]);
				}
			}
		}
	}
	/*for(int k = 0; k < 5; k++)
	{
		cout << "KKK " << k << endl;
		for(int i= 0 ; i < n; i++)
		{
			for(int j = 0; j < n; j++)
				cout << dp[i][j][k] << " ";
			cout << endl;
		}
		cout << endl << endl;
	}*/
	//cout << "AANNNSS" << endl;
	for(int i = 0; i < r; i++)
	{
		int s, t, k;
		scanf("%d", &s);
		scanf("%d", &t);
		scanf("%d", &k);
//		cin >> s >> t >> k;
		s--, t--;
		printf("%d", dp[s][t][k]);
		printf("\n");
		//cout << dp[s][t][k] << '\n';
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;
const int MAXN = 65, MAXK = 1005;
int n, m, r, c[MAXN][MAXN][MAXN], dist[MAXN], dp[MAXN][MAXN][MAXK];
set <pair<int,int>> s;

void dij(int r, int comp)
{
	for(int i = 0; i < n; i++)
	{
		if(i != r)
		{
			dist[i] = INT_MAX;
			s.insert({dist[i], i});
		}
	}
	dist[r] = 0;
	s.insert({0, r});
	dp[r][r][0] = 0;
	for(int i = 0; i < n; i++)
	{
		int mn = s.begin()->second;
		s.erase(*s.begin());
		for(int j = 0; j < n; j++)
		{
			if(j != mn && dist[j] > dist[mn] + c[mn][j][comp])
			{
				s.erase({dist[j], j});
				dist[j] = dist[mn] + c[mn][j][comp];
				dp[r][j][0] = min(dp[r][j][0], dist[j]);
				s.insert({dist[j], j});
			}
		}
	}
	/*cout << "RRR" << r << endl;
	for(int i = 0; i < n; i++)
		cout << dist[i] << " ";
	cout << endl;*/
	/*for(int i = 0; i < n; i++)
	{
		dp[r][i][0] = min(dp[r][i][0], dist[i]);
	}*/
	return;
}

int main()
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> m >> r;
	//scanf("%d", &n);
	//scanf("%d", &m);
	//scanf("%d", &r);
	for(int j = 0; j < m; j++)
	{
		for(int i = 0; i < n; i++)
		{
			for(int i2 = 0; i2 < n; i2++)
				cin >> c[i][i2][j];
//				scanf("%d", &c[i][i2][j]);
		}
	}
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n; j++)
		{
			if(i != j)
				dp[i][j][0] = INT_MAX;
		}
	}
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < m; j++)
			dij(i, j);
	}
	for(int k = 1; k < MAXK; k++)
	{
		for(int i = 0; i < n; i++)
		{
			for(int j = 0; j < n; j++)
			{
				dp[i][j][k] = dp[i][j][k - 1];
				for(int h = 0; h < n; h++)
					dp[i][j][k] = min(dp[i][j][k], dp[i][h][k - 1] + dp[h][j][0]);
			}
		}
	}
	/*for(int k = 0; k < 5; k++)
	{
		cout << "KKK " << k << endl;
		for(int i= 0 ; i < n; i++)
		{
			for(int j = 0; j < n; j++)
				cout << dp[i][j][k] << " ";
			cout << endl;
		}
		cout << endl << endl;
	}*/
	//cout << "AANNNSS" << endl;
	for(int i = 0; i < r; i++)
	{
		int s, t, k;
//		scanf("%d", &s);
//		scanf("%d", &t);
//		scanf("%d", &k);
		cin >> s >> t >> k;
		s--, t--;
		//printf("%d", dp[s][t][k]);
		//printf("\n");
		cout << dp[s][t][k] << '\n';
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;
const int MAXN = 65, MAXK = 1005;
int n, m, r, c[MAXN][MAXN][MAXN], dist[MAXN], dp[MAXN][MAXN][MAXN];
set <pair<int,int>> s;

void dij(int r, int comp)
{
	for(int i = 0; i < n; i++)
	{
		if(i != r)
		{
			dist[i] = INT_MAX;
			s.insert({dist[i], i});
		}
	}
	dist[r] = 0;
	s.insert({0, r});
	dp[r][r][0] = 0;
	for(int i = 0; i < n; i++)
	{
		int mn = s.begin()->second;
		s.erase(*s.begin());
		for(int j = 0; j < n; j++)
		{
			if(j != mn && dist[j] > dist[mn] + c[mn][j][comp])
			{
				s.erase({dist[j], j});
				dist[j] = dist[mn] + c[mn][j][comp];
				dp[r][j][0] = min(dp[r][j][0], dist[j]);
				s.insert({dist[j], j});
			}
		}
	}
	/*cout << "RRR" << r << endl;
	for(int i = 0; i < n; i++)
		cout << dist[i] << " ";
	cout << endl;*/
	/*for(int i = 0; i < n; i++)
	{
		dp[r][i][0] = min(dp[r][i][0], dist[i]);
	}*/
	return;
}

int main()
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> m >> r;
	//scanf("%d", &n);
	//scanf("%d", &m);
	//scanf("%d", &r);
	for(int j = 0; j < m; j++)
	{
		for(int i = 0; i < n; i++)
		{
			for(int i2 = 0; i2 < n; i2++)
				cin >> c[i][i2][j];
//				scanf("%d", &c[i][i2][j]);
		}
	}
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n; j++)
		{
			if(i != j)
				dp[i][j][0] = INT_MAX;
		}
	}
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < m; j++)
			dij(i, j);
	}
	for(int k = 1; k < MAXN; k++)
	{
		for(int i = 0; i < n; i++)
		{
			for(int j = 0; j < n; j++)
			{
				dp[i][j][k] = dp[i][j][k - 1];
				for(int h = 0; h < n; h++)
					dp[i][j][k] = min(dp[i][j][k], dp[i][h][k - 1] + dp[h][j][0]);
			}
		}
	}
	/*for(int k = 0; k < 5; k++)
	{
		cout << "KKK " << k << endl;
		for(int i= 0 ; i < n; i++)
		{
			for(int j = 0; j < n; j++)
				cout << dp[i][j][k] << " ";
			cout << endl;
		}
		cout << endl << endl;
	}*/
	//cout << "AANNNSS" << endl;
	for(int i = 0; i < r; i++)
	{
		int s, t, k;
//		scanf("%d", &s);
//		scanf("%d", &t);
//		scanf("%d", &k);
		cin >> s >> t >> k;
		s--, t--;
		//printf("%d", dp[s][t][k]);
		//printf("\n");
		if(k < MAXN)
			cout << dp[s][t][k] << '\n';
		else
			cout << dp[s][t][MAXN] << '\n';
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;
const int MAXN = 65, MAXK = 1010;
int n, m, r, c[MAXN][MAXN][MAXN], dist[MAXN], dp[MAXN][MAXN][MAXK];

void dij(int r, int comp)
{
	set <pair<int,int>> s;
	for(int i = 0; i < n; i++)
	{
		if(i != r)
		{
			dist[i] = INT_MAX;
			s.insert({dist[i], i});
		}
	}
	dist[r] = 0;
	s.insert({0, r});
	for(int i = 0; i < n; i++)
	{
		int mn = s.begin()->second;
		s.erase(*s.begin());
		for(int j = 0; j < n; j++)
		{
			if(j != mn && dist[j] > dist[mn] + c[mn][j][comp])
			{
				s.erase({dist[j], j});
				dist[j] = dist[mn] + c[mn][j][comp];
				s.insert({dist[j], j});
			}
		}
	}
	/*cout << "RRR" << r << endl;
	for(int i = 0; i < n; i++)
		cout << dist[i] << " ";
	cout << endl;*/
	for(int i = 0; i < n; i++)
	{
		dp[r][i][0] = min(dp[r][i][0], dist[i]);
	}
	return;
}

int main()
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> m >> r;
	for(int j = 0; j < m; j++)
	{
		for(int i = 0; i < n; i++)
		{
			for(int i2 = 0; i2 < n; i2++)
				cin >> c[i][i2][j];
		}
	}
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n; j++)
		{
			if(i != j)
				dp[i][j][0] = INT_MAX;
		}
	}
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < m; j++)
			dij(i, j);
	}
	for(int k = 1; k < MAXK; k++)
	{
		for(int i = 0; i < n; i++)
		{
			for(int j = 0; j < n; j++)
			{
				dp[i][j][k] = dp[i][j][k - 1];
				for(int h = 0; h < n; h++)
				{
					dp[i][j][k] = min(dp[i][j][k], dp[i][h][k - 1] + dp[h][j][0]);
				}
			}
		}
	}
	/*for(int k = 0; k < 5; k++)
	{
		cout << "KKK " << k << endl;
		for(int i= 0 ; i < n; i++)
		{
			for(int j = 0; j < n; j++)
				cout << dp[i][j][k] << " ";
			cout << endl;
		}
		cout << endl << endl;
	}*/
	//cout << "AANNNSS" << endl;
	for(int i = 0; i < r; i++)
	{
		int s, t, k;
		cin >> s >> t >> k;
		s--, t--;

		cout << dp[s][t][k] << '\n';
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;
const int MAXN = 65, MAXK = 1010;
int n, m, r, c[MAXN][MAXN][MAXN], dist[MAXN], dp[MAXN][MAXN][MAXK];

void dij(int r, int comp)
{
	set <pair<int,int>> s;
	for(int i = 0; i < n; i++)
	{
		if(i != r)
		{
			dist[i] = INT_MAX;
			s.insert({dist[i], i});
		}
	}
	dist[r] = 0;
	s.insert({0, r});
	for(int i = 0; i < n; i++)
	{
		int mn = s.begin()->second;
		s.erase(*s.begin());
		for(int j = 0; j < n; j++)
		{
			if(j != mn && dist[j] > dist[mn] + c[mn][j][comp])
			{
				s.erase({dist[j], j});
				dist[j] = dist[mn] + c[mn][j][comp];
				s.insert({dist[j], j});
			}
		}
	}
	/*cout << "RRR" << r << endl;
	for(int i = 0; i < n; i++)
		cout << dist[i] << " ";
	cout << endl;*/
	for(int i = 0; i < n; i++)
	{
		dp[r][i][0] = min(dp[r][i][0], dist[i]);
	}
	return;
}

int main()
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> m >> r;
	for(int j = 0; j < m; j++)
	{
		for(int i = 0; i < n; i++)
		{
			for(int i2 = 0; i2 < n; i2++)
				cin >> c[i][i2][j];
		}
	}
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n; j++)
		{
			if(i != j)
				dp[i][j][0] = INT_MAX;
		}
	}
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < m; j++)
			dij(i, j);
	}
	for(int k = 1; k < MAXK; k++)
	{
		for(int i = 0; i < n; i++)
		{
			for(int j = 0; j < n; j++)
			{
				dp[i][j][k] = dp[i][j][k - 1];
				for(int h = 0; h < n; h++)
				{
					dp[i][j][k] = min(dp[i][j][k], dp[i][h][k - 1] + dp[h][j][0]);
				}
			}
		}
	}
	/*for(int k = 0; k < 5; k++)
	{
		cout << "KKK " << k << endl;
		for(int i= 0 ; i < n; i++)
		{
			for(int j = 0; j < n; j++)
				cout << dp[i][j][k] << " ";
			cout << endl;
		}
		cout << endl << endl;
	}*/
	//cout << "AANNNSS" << endl;
	for(int i = 0; i < r; i++)
	{
		int s, t, k;
		cin >> s >> t >> k;
		s--, t--;
		cout << dp[s][t][k] << endl;
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;
const int MAXN = 65, MAXK = 1005;
int n, m, r, c[MAXN][MAXN][MAXN], dist[MAXN], dp[MAXN][MAXN][MAXK];
set <pair<int,int>> s;

void dij(int r, int comp)
{
	for(int i = 0; i < n; i++)
	{
		if(i != r)
		{
			dist[i] = INT_MAX;
			s.insert({dist[i], i});
		}
	}
	dist[r] = 0;
	s.insert({0, r});
	dp[r][r][0] = 0;
	for(int i = 0; i < n; i++)
	{
		int mn = s.begin()->second;
		s.erase(*s.begin());
		for(int j = 0; j < n; j++)
		{
			if(j != mn && dist[j] > dist[mn] + c[mn][j][comp])
			{
				s.erase({dist[j], j});
				dist[j] = dist[mn] + c[mn][j][comp];
				dp[r][j][0] = min(dp[r][j][0], dist[j]);
				s.insert({dist[j], j});
			}
		}
	}
	/*cout << "RRR" << r << endl;
	for(int i = 0; i < n; i++)
		cout << dist[i] << " ";
	cout << endl;*/
	/*for(int i = 0; i < n; i++)
	{
		dp[r][i][0] = min(dp[r][i][0], dist[i]);
	}*/
	return;
}

int main()
{
	//ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	//cin >> n >> m >> r;
	scanf("%d", &n);
	scanf("%d", &m);
	scanf("%d", &r);
	for(int j = 0; j < m; j++)
	{
		for(int i = 0; i < n; i++)
		{
			for(int i2 = 0; i2 < n; i2++)
				scanf("%d", &c[i][i2][j]);
			//	cin >> c[i][i2][j];
		}
	}
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n; j++)
		{
			if(i != j)
				dp[i][j][0] = INT_MAX;
		}
	}
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < m; j++)
			dij(i, j);
	}
	for(int k = 1; k < MAXK; k++)
	{
		for(int i = 0; i < n; i++)
		{
			for(int j = 0; j < n; j++)
			{
				dp[i][j][k] = dp[i][j][k - 1];
				for(int h = 0; h < n; h++)
					dp[i][j][k] = min(dp[i][j][k], dp[i][h][k - 1] + dp[h][j][0]);
			}
		}
	}
	/*for(int k = 0; k < 5; k++)
	{
		cout << "KKK " << k << endl;
		for(int i= 0 ; i < n; i++)
		{
			for(int j = 0; j < n; j++)
				cout << dp[i][j][k] << " ";
			cout << endl;
		}
		cout << endl << endl;
	}*/
	//cout << "AANNNSS" << endl;
	for(int i = 0; i < r; i++)
	{
		int s, t, k;
		scanf("%d", &s);
		scanf("%d", &t);
		scanf("%d", &k);
//		cin >> s >> t >> k;
		s--, t--;
		printf("%d", dp[s][t][k]);
		printf("\n");
		//cout << dp[s][t][k] << '\n';
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;
const int MAXN = 65, MAXK = 1005;
int n, m, r, c[MAXN][MAXN][MAXN], dist[MAXN], dp[MAXN][MAXN][MAXK];
set <pair<int,int>> s;

void dij(int r, int comp)
{
	for(int i = 0; i < n; i++)
	{
		if(i != r)
		{
			dist[i] = INT_MAX;
			s.insert({dist[i], i});
		}
	}
	dist[r] = 0;
	s.insert({0, r});
	dp[r][r][0] = 0;
	for(int i = 0; i < n; i++)
	{
		int mn = s.begin()->second;
		s.erase(s.begin());	//* ??
		for(int j = 0; j < n; j++)
		{
			if(j != mn && dist[j] > dist[mn] + c[mn][j][comp])
			{
				s.erase({dist[j], j});
				dist[j] = dist[mn] + c[mn][j][comp];
				dp[r][j][0] = min(dp[r][j][0], dist[j]);
				s.insert({dist[j], j});
			}
		}
	}
	/*cout << "RRR" << r << endl;
	for(int i = 0; i < n; i++)
		cout << dist[i] << " ";
	cout << endl;*/
	/*for(int i = 0; i < n; i++)
	{
		dp[r][i][0] = min(dp[r][i][0], dist[i]);
	}*/
	return;
}

int main()
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> m >> r;
	//scanf("%d", &n);
	//scanf("%d", &m);
	//scanf("%d", &r);
	for(int j = 0; j < m; j++)
	{
		for(int i = 0; i < n; i++)
		{
			for(int i2 = 0; i2 < n; i2++)
			{
				cin >> c[i][i2][j];
//				scanf("%d", &c[i][i2][j]);
				dp[i][i2][0] = INT_MAX;
			}
		}
	}
	/*for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n; j++)
		{
			if(i != j)
				dp[i][j][0] = INT_MAX;
		}
	}*/
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < m; j++)
			dij(i, j);
	}
	for(int k = 1; k < MAXK; k++)
	{
		for(int i = 0; i < n; i++)
		{
			for(int j = 0; j < n; j++)
			{
				dp[i][j][k] = dp[i][j][k - 1];
				for(int h = 0; h < n; h++)
					dp[i][j][k] = min(dp[i][j][k], dp[i][h][k - 1] + dp[h][j][0]);
			}
		}
	}
	/*for(int k = 0; k < 5; k++)
	{
		cout << "KKK " << k << endl;
		for(int i= 0 ; i < n; i++)
		{
			for(int j = 0; j < n; j++)
				cout << dp[i][j][k] << " ";
			cout << endl;
		}
		cout << endl << endl;
	}*/
	//cout << "AANNNSS" << endl;
	for(int i = 0; i < r; i++)
	{
		int s, t, k;
//		scanf("%d", &s);
//		scanf("%d", &t);
//		scanf("%d", &k);
		cin >> s >> t >> k;
		s--, t--;
		//printf("%d", dp[s][t][k]);
		//printf("\n");
		cout << dp[s][t][k] << '\n';
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;
const int MAXN = 65, MAXK = 1005;
int n, m, r, c[MAXN][MAXN][MAXN], dist[MAXN], dp[MAXN][MAXN][MAXN];
set <pair<int,int>> s;

void dij(int r, int comp)
{
	for(int i = 0; i < n; i++)
	{
		if(i != r)
		{
			dist[i] = INT_MAX;
			s.insert({dist[i], i});
		}
	}
	dist[r] = 0;
	s.insert({0, r});
	dp[r][r][0] = 0;
	for(int i = 0; i < n; i++)
	{
		int mn = s.begin()->second;
		s.erase(*s.begin());
		for(int j = 0; j < n; j++)
		{
			if(j != mn && dist[j] > dist[mn] + c[mn][j][comp])
			{
				s.erase({dist[j], j});
				dist[j] = dist[mn] + c[mn][j][comp];
				dp[r][j][0] = min(dp[r][j][0], dist[j]);
				s.insert({dist[j], j});
			}
		}
	}
	/*cout << "RRR" << r << endl;
	for(int i = 0; i < n; i++)
		cout << dist[i] << " ";
	cout << endl;*/
	/*for(int i = 0; i < n; i++)
	{
		dp[r][i][0] = min(dp[r][i][0], dist[i]);
	}*/
	return;
}

int main()
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> m >> r;
	//scanf("%d", &n);
	//scanf("%d", &m);
	//scanf("%d", &r);
	for(int j = 0; j < m; j++)
	{
		for(int i = 0; i < n; i++)
		{
			for(int i2 = 0; i2 < n; i2++)
				cin >> c[i][i2][j];
//				scanf("%d", &c[i][i2][j]);
		}
	}
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n; j++)
		{
			if(i != j)
				dp[i][j][0] = INT_MAX;
		}
	}
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < m; j++)
			dij(i, j);
	}
	for(int k = 1; k < MAXN; k++)
	{
		for(int i = 0; i < n; i++)
		{
			for(int j = 0; j < n; j++)
			{
				dp[i][j][k] = dp[i][j][k - 1];
				for(int h = 0; h < n; h++)
					dp[i][j][k] = min(dp[i][j][k], dp[i][h][k - 1] + dp[h][j][0]);
			}
		}
	}
	/*for(int k = 0; k < 5; k++)
	{
		cout << "KKK " << k << endl;
		for(int i= 0 ; i < n; i++)
		{
			for(int j = 0; j < n; j++)
				cout << dp[i][j][k] << " ";
			cout << endl;
		}
		cout << endl << endl;
	}*/
	//cout << "AANNNSS" << endl;
	for(int i = 0; i < r; i++)
	{
		int s, t, k;
//		scanf("%d", &s);
//		scanf("%d", &t);
//		scanf("%d", &k);
		cin >> s >> t >> k;
		s--, t--;
		//printf("%d", dp[s][t][k]);
		//printf("\n");
		if(k < MAXN)
			cout << dp[s][t][k] << '\n';
		else
			cout << dp[s][t][MAXN - 1] << '\n';
	}
	return 0;
}