#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define pb push_back
#define pii pair < ll , ll >
#define F first
#define S second
#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define err(A) cout << #A << " = " << (A) << endl
using namespace std;
/// khodaya komak kon
/// ya navid
const int N=62,K=1002;
ll dp[N][N][K];
ll a[N][N][N];
ll b[N][N];
int32_t main(){
    sync;
    ll n,m,r;
    memset(b,69,sizeof b);
    cin >> n >> m >> r;
    for (int i=1;i<=m;i++){
        for (int j=1;j<=n;j++){
            for (int p=1;p<=n;p++){
                cin >> a[i][j][p];
            }
        }
    }
    for (int i=1;i<=m;i++){
        for (int j=1;j<=n;j++){
            for (int p=1;p<=n;p++){
                for (int k=1;k<=n;k++){
                    a[i][j][p]=min(a[i][j][p],a[i][j][k]+a[i][k][p]);
                }
            }
        }
    }
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n;j++){
            for (int k=1;k<=m;k++){
                b[i][j]=min(b[i][j],a[k][i][j]);
            }
        }
    }
memset(dp,69,sizeof dp);
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n;j++){
            //memset(dp[i][j],b[i][j]);
            dp[i][j][0]=b[i][j];
        }
    }

    for (int k=1;k<K;k++){
        for (int i=1;i<=n;i++){
            for (int j=1;j<=n;j++){
                for (int p=1;p<=n;p++){
                    dp[i][j][k]=min(dp[i][j][k],dp[i][p][0]+dp[p][j][k-1]);
                   // if (k==1){
                     //   cout << "fghnjmknhg   " << dp[i][p][0] << " " << i << " " << j << endl;
                    //}
                }
            }
        }
    }
    for (int i=0;i<r;i++){
        ll s,t,k;
        cin >> s >> t >> k;
        cout << dp[s][t][k] << endl;
    }

}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define pb push_back
#define pii pair < ll , ll >
#define F first
#define S second
#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define err(A) cout << #A << " = " << (A) << endl
using namespace std;
/// khodaya komak kon
/// ya navid
const int N=67,K=1002;
ll dp[N][N][K];
ll a[N][N][N];
ll b[N][N];
int32_t main(){
    sync;
    ll n,m,r;
    memset(b,69,sizeof b);
    cin >> n >> m >> r;
    for (int i=1;i<=m;i++){
        for (int j=1;j<=n;j++){
            for (int p=1;p<=n;p++){
                cin >> a[i][j][p];
            }
        }
    }
    for (int o=0;o<10;o++){
    for (int i=1;i<=m;i++){
        for (int j=1;j<=n;j++){
            for (int p=1;p<=n;p++){
                for (int k=1;k<=n;k++){
                    ll q=a[i][j][p];
                    a[i][j][p]=min(a[i][j][p],a[i][j][k]+a[i][k][p]);
                    if (q!=a[i][j][p]){
                        if (o>0){
                            cout << 1/0;
                        }
                    }
                }
            }
        }
    }
    }
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n;j++){
            for (int k=1;k<=m;k++){
                b[i][j]=min(b[i][j],a[k][i][j]);
            }
        }
    }
memset(dp,69,sizeof dp);
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n;j++){
            //memset(dp[i][j],b[i][j]);
            dp[i][j][0]=b[i][j];
        }
    }

    for (int k=1;k<K;k++){
        for (int i=1;i<=n;i++){
            for (int j=1;j<=n;j++){
                for (int p=1;p<=n;p++){
                    dp[i][j][k]=min(dp[i][j][k],dp[i][p][k-1]+dp[p][j][0]);
                   // if (k==1){
                     //   cout << "fghnjmknhg   " << dp[i][p][0] << " " << i << " " << j << endl;
                    //}
                }
            }
        }
    }
    for (int i=0;i<r;i++){
        ll s,t,k;
        cin >> s >> t >> k;
        cout << dp[s][t][k] << endl;
    }

}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define pb push_back
#define pii pair < ll , ll >
#define F first
#define S second
#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define err(A) cout << #A << " = " << (A) << endl
using namespace std;
/// khodaya komak kon
/// ya navid
const int N=67,K=1002;
ll dp[N][N][K];
ll a[N][N][N];
ll b[N][N];
int32_t main(){
    sync;
    ll n,m,r;
    memset(b,69,sizeof b);
    cin >> n >> m >> r;
    for (int i=1;i<=m;i++){
        for (int j=1;j<=n;j++){
            for (int p=1;p<=n;p++){
                cin >> a[i][j][p];
            }
        }
    }
   // for (int o=0;o<10;o++){
    for (int i=1;i<=m;i++){
        for (int j=1;j<=n;j++){
            for (int p=1;p<=n;p++){
                for (int k=1;k<=n;k++){
                    ll q=a[i][j][p];
                    a[i][p][k]=min(a[i][p][k],a[i][p][j]+a[i][j][k]);

                }
            }
        }
    }
    //}
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n;j++){
            for (int k=1;k<=m;k++){
                b[i][j]=min(b[i][j],a[k][i][j]);
            }
        }
    }
memset(dp,69,sizeof dp);
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n;j++){
            //memset(dp[i][j],b[i][j]);
            dp[i][j][0]=b[i][j];
        }
    }

    for (int k=1;k<K;k++){
        for (int i=1;i<=n;i++){
            for (int j=1;j<=n;j++){
                for (int p=1;p<=n;p++){
                    dp[i][j][k]=min(dp[i][j][k],dp[i][p][k-1]+dp[p][j][0]);
                   // if (k==1){
                     //   cout << "fghnjmknhg   " << dp[i][p][0] << " " << i << " " << j << endl;
                    //}
                }
            }
        }
    }
    for (int i=0;i<r;i++){
        ll s,t,k;
        cin >> s >> t >> k;
        cout << dp[s][t][k] << endl;
    }

}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define pb push_back
#define pii pair < ll , ll >
#define F first
#define S second
//#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define err(A) cout << #A << " = " << (A) << endl
using namespace std;
/// khodaya komak kon
/// ya navid
const int N=62,K=1002;
int dp[N][N][K];
int a[N][N][N];
int b[N][N];
int main(){
    sync;
    int n,m,r;
    memset(b,10,sizeof b);
    //cin >> n >> m >> r;
    cout << b[0][0] << endl;
    scanf("%d %d %d" , &n,&m,&r);
    for (int i=1;i<=m;i++){
        for (int j=1;j<=n;j++){
            for (int p=1;p<=n;p++){
                scanf("%d",&a[i][j][p]);
              //  cin >> a[i][j][p];
            }
        }
    }
   // for (int o=0;o<10;o++){
    for (int i=1;i<=m;i++){
        for (int j=1;j<=n;j++){
            for (int p=1;p<=n;p++){
                for (int k=1;k<=n;k++){
                    ll q=a[i][j][p];
                    a[i][p][k]=min(a[i][p][k],a[i][p][j]+a[i][j][k]);

                }
            }
        }
    }
    //}
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n;j++){
            for (int k=1;k<=m;k++){
                b[i][j]=min(b[i][j],a[k][i][j]);
            }
        }
    }
    memset(dp,10,sizeof dp);
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n;j++){
            //memset(dp[i][j],b[i][j]);
            dp[i][j][0]=b[i][j];
        }
    }

    for (int k=1;k<K;k++){
        for (int i=1;i<=n;i++){
            for (int j=1;j<=n;j++){
                for (int p=1;p<=n;p++){
                    dp[i][j][k]=min(dp[i][j][k],dp[i][p][k-1]+dp[p][j][0]);
                   // if (k==1){
                     //   cout << "fghnjmknhg   " << dp[i][p][0] << " " << i << " " << j << endl;
                    //}
                }
            }
        }
    }
    for (int i=0;i<r;i++){
        int s,t,k;

        //cin >> s >> t >> k;
        scanf("%d %d %d" , &s,&t,&k);
        printf("%d \n",dp[s][t][k]) ;//dp[s][t][k] << endl;
    }

}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define pb push_back
#define pii pair < ll , ll >
#define F first
#define S second
//#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define err(A) cout << #A << " = " << (A) << endl
using namespace std;
/// khodaya komak kon
/// ya navid
const int N=62,K=1002;
int dp[N][N][K];
int a[N][N][N];
int b[N][N];
int main(){
    sync;
    int n,m,r;
    memset(b,10,sizeof b);
    //cin >> n >> m >> r;
   // cout << b[0][0] << endl;
    scanf("%d %d %d" , &n,&m,&r);
    for (int i=1;i<=m;i++){
        for (int j=1;j<=n;j++){
            for (int p=1;p<=n;p++){
                scanf("%d",&a[i][j][p]);
              //  cin >> a[i][j][p];
            }
        }
    }
   // for (int o=0;o<10;o++){
    for (int i=1;i<=m;i++){
        for (int j=1;j<=n;j++){
            for (int p=1;p<=n;p++){
                for (int k=1;k<=n;k++){
                    ll q=a[i][j][p];
                    a[i][p][k]=min(a[i][p][k],a[i][p][j]+a[i][j][k]);

                }
            }
        }
    }
    //}
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n;j++){
            for (int k=1;k<=m;k++){
                b[i][j]=min(b[i][j],a[k][i][j]);
            }
        }
    }
    memset(dp,10,sizeof dp);
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n;j++){
            //memset(dp[i][j],b[i][j]);
            dp[i][j][0]=b[i][j];
        }
    }

    for (int k=1;k<K;k++){
        for (int i=1;i<=n;i++){
            for (int j=1;j<=n;j++){
                for (int p=1;p<=n;p++){
                    dp[i][j][k]=min(dp[i][j][k],dp[i][p][k-1]+dp[p][j][0]);
                   // if (k==1){
                     //   cout << "fghnjmknhg   " << dp[i][p][0] << " " << i << " " << j << endl;
                    //}
                }
            }
        }
    }
    for (int i=0;i<r;i++){
        int s,t,k;

        //cin >> s >> t >> k;
        scanf("%d %d %d" , &s,&t,&k);
        printf("%d \n",dp[s][t][k]) ;//dp[s][t][k] << endl;
    }

}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define pb push_back
#define pii pair < ll , ll >
#define F first
#define S second
#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define err(A) cout << #A << " = " << (A) << endl
using namespace std;
/// khodaya komak kon
/// ya navid
const int N=62,K=1002;
ll dp[N][N][K];
ll a[N][N][N];
ll b[N][N];
int32_t main(){
    sync;
    ll n,m,r;
    memset(b,69,sizeof b);
    cin >> n >> m >> r;
    for (int i=1;i<=m;i++){
        for (int j=1;j<=n;j++){
            for (int p=1;p<=n;p++){
                cin >> a[i][j][p];
            }
        }
    }
    for (int i=1;i<=m;i++){
        for (int j=1;j<=n;j++){
            for (int p=1;p<=n;p++){
                for (int k=1;k<=n;k++){
                    a[i][j][p]=min(a[i][j][p],a[i][j][k]+a[i][k][p]);
                }
            }
        }
    }
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n;j++){
            for (int k=1;k<=m;k++){
                b[i][j]=min(b[i][j],a[k][i][j]);
            }
        }
    }
memset(dp,69,sizeof dp);
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n;j++){
            //memset(dp[i][j],b[i][j]);
            dp[i][j][0]=b[i][j];
        }
    }

    for (int k=1;k<K;k++){
        for (int i=1;i<=n;i++){
            for (int j=1;j<=n;j++){
                for (int p=1;p<=n;p++){
                    dp[i][j][k]=min(dp[i][j][k],dp[i][p][k-1]+dp[p][j][0]);
                   // if (k==1){
                     //   cout << "fghnjmknhg   " << dp[i][p][0] << " " << i << " " << j << endl;
                    //}
                }
            }
        }
    }
    for (int i=0;i<r;i++){
        ll s,t,k;
        cin >> s >> t >> k;
        cout << dp[s][t][k] << endl;
    }

}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define pb push_back
#define pii pair < ll , ll >
#define F first
#define S second
#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define err(A) cout << #A << " = " << (A) << endl
using namespace std;
/// khodaya komak kon
/// ya navid
const int N=67,K=1002;
ll dp[N][N][K];
ll a[N][N][N];
ll b[N][N];
int32_t main(){
    sync;
    ll n,m,r;
    memset(b,69,sizeof b);
    //cin >> n >> m >> r;
    scanf("%lld %lld %lld" , &n,&m,&r);
    for (int i=1;i<=m;i++){
        for (int j=1;j<=n;j++){
            for (int p=1;p<=n;p++){
                scanf("%lld",&a[i][j][p]);
              //  cin >> a[i][j][p];
            }
        }
    }
   // for (int o=0;o<10;o++){
    for (int i=1;i<=m;i++){
        for (int j=1;j<=n;j++){
            for (int p=1;p<=n;p++){
                for (int k=1;k<=n;k++){
                    ll q=a[i][j][p];
                    a[i][p][k]=min(a[i][p][k],a[i][p][j]+a[i][j][k]);

                }
            }
        }
    }
    //}
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n;j++){
            for (int k=1;k<=m;k++){
                b[i][j]=min(b[i][j],a[k][i][j]);
            }
        }
    }
memset(dp,69,sizeof dp);
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n;j++){
            //memset(dp[i][j],b[i][j]);
            dp[i][j][0]=b[i][j];
        }
    }

    for (int k=1;k<K;k++){
        for (int i=1;i<=n;i++){
            for (int j=1;j<=n;j++){
                for (int p=1;p<=n;p++){
                    dp[i][j][k]=min(dp[i][j][k],dp[i][p][k-1]+dp[p][j][0]);
                   // if (k==1){
                     //   cout << "fghnjmknhg   " << dp[i][p][0] << " " << i << " " << j << endl;
                    //}
                }
            }
        }
    }
    for (int i=0;i<r;i++){
        ll s,t,k;

        //cin >> s >> t >> k;
        scanf("%lld %lld %lld" , &s,&t,&k);
        printf("%lld \n",dp[s][t][k]) ;//dp[s][t][k] << endl;
    }

}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define pb push_back
#define pii pair < ll , ll >
#define F first
#define S second
#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define err(A) cout << #A << " = " << (A) << endl
using namespace std;
/// khodaya komak kon
/// ya navid
const int N=62,K=1002;
ll dp[N][N][K];
ll a[N][N][N];
ll b[N][N];
int32_t main(){
    sync;
    ll n,m,r;
    memset(b,10,sizeof b);
    cin >> n >> m >> r;
    for (int i=1;i<=m;i++){
        for (int j=1;j<=n;j++){
            for (int p=1;p<=n;p++){
                cin >> a[i][j][p];
            }
        }
    }
    for (int i=1;i<=m;i++){
        for (int j=1;j<=n;j++){
            for (int p=1;p<=n;p++){
                for (int k=1;k<=n;k++){
                    a[i][j][p]=min(a[i][j][p],a[i][j][k]+a[i][k][p]);
                }
            }
        }
    }
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n;j++){
            for (int k=1;k<=m;k++){
                b[i][j]=min(b[i][j],a[k][i][j]);
            }
        }
    }
memset(dp,10,sizeof dp);
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n;j++){
            //memset(dp[i][j],b[i][j]);
            dp[i][j][0]=b[i][j];
        }
    }

    for (int k=1;k<K;k++){
        for (int i=1;i<=n;i++){
            for (int j=1;j<=n;j++){
                for (int p=1;p<=n;p++){
                    dp[i][j][k]=min(dp[i][j][k],dp[i][p][0]+dp[p][j][k-1]);
                   // if (k==1){
                     //   cout << "fghnjmknhg   " << dp[i][p][0] << " " << i << " " << j << endl;
                    //}
                }
            }
        }
    }
    for (int i=0;i<r;i++){
        ll s,t,k;
        cin >> s >> t >> k;
        cout << dp[s][t][k] << endl;
    }

}


#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define pb push_back
#define pii pair < ll , ll >
#define F first
#define S second
#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define err(A) cout << #A << " = " << (A) << endl
using namespace std;
/// khodaya komak kon
/// ya navid
const int N=67,K=1002;
ll dp[N][N][K];
ll a[N][N][N];
ll b[N][N];
int32_t main(){
    sync;
    ll n,m,r;
    memset(b,69,sizeof b);
    cin >> n >> m >> r;
    for (int i=1;i<=m;i++){
        for (int j=1;j<=n;j++){
            for (int p=1;p<=n;p++){
                cin >> a[i][j][p];
            }
        }
    }
    for (int o=0;o<10;o++){
    for (int i=1;i<=m;i++){
        for (int j=1;j<=n;j++){
            for (int p=1;p<=n;p++){
                for (int k=1;k<=n;k++){
                    ll q=a[i][j][p];
                    a[i][j][p]=min(a[i][j][p],a[i][j][k]+a[i][k][p]);

                }
            }
        }
    }
    }
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n;j++){
            for (int k=1;k<=m;k++){
                b[i][j]=min(b[i][j],a[k][i][j]);
            }
        }
    }
memset(dp,69,sizeof dp);
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n;j++){
            //memset(dp[i][j],b[i][j]);
            dp[i][j][0]=b[i][j];
        }
    }

    for (int k=1;k<K;k++){
        for (int i=1;i<=n;i++){
            for (int j=1;j<=n;j++){
                for (int p=1;p<=n;p++){
                    dp[i][j][k]=min(dp[i][j][k],dp[i][p][k-1]+dp[p][j][0]);
                   // if (k==1){
                     //   cout << "fghnjmknhg   " << dp[i][p][0] << " " << i << " " << j << endl;
                    //}
                }
            }
        }
    }
    for (int i=0;i<r;i++){
        ll s,t,k;
        cin >> s >> t >> k;
        cout << dp[s][t][k] << endl;
    }

}


#include <iostream>
#include <cstdio>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iterator>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define x0 abc
#define y0 ABC
#define x1 abcd
#define y1 ABCD
#define xn abcde
#define yn ABCDE

#define fbo find_by_order
#define ook order_of_key

#define l_b lower_bound
#define u_b upper_bound

#define in insert
#define er erase

#define fi first
#define se second
#define m_p make_pair

#define p_b push_back
#define po_b pop_back

#define uns unsigned

#define ld long double
#define ll long long

#define cont continue
#define re return

#define MAXLL 9000000000000000000LL
#define MAXINT 2000000000

#define MINLL -9000000000000000000LL
#define MININT -2000000000

#define pi acos(-1.)

#define endl '\n'

#define OUT cout << "-------" << endl;

using namespace std;

using namespace __gnu_pbds;

typedef tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

queue < pair < pair <int, int>, int > > que;

int dp[61][61][61][61], mx[61][61][61], n, m, i, j, z, p, q, s, f, nom, k, ans;

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m >> q;

    for (i = 1; i <= m; i++)
        for (j = 1; j <= n; j++)
            for (z = 1; z <= n; z++) cin >> mx[i][j][z];

    for (i = 1; i <= n; i++)
        for (j = 1; j <= n; j++)
            for (z = 1; z <= m; z++)
                for (p = 0; p <= n; p++) dp[i][j][z][p] = MAXINT;

    for (z = 1; z <= n; z++) {
        for (i = 1; i <= m; i++) {
            dp[z][z][i][0] = 0;

            que.push(m_p(m_p(z, i), 0));
        }

        while (!que.empty()) {
            f = que.front().fi.fi;

            nom = que.front().fi.se;

            k = que.front().se;

            que.pop();

            for (i = 1; i <= n; i++)
                for (j = 1; j <= m; j++)
                    if (k + (nom != j) <= n && dp[z][f][nom][k] + mx[j][f][i] < dp[z][i][j][k + (nom != j)]) {
                        dp[z][i][j][k + (nom != j)] = dp[z][f][nom][k] + mx[j][f][i];

                        que.push(m_p(m_p(i, j), k + (nom != j)));
                    }
        }

        for (k = 1; k <= n; k++)
            for (i = 1; i <= n; i++)
                for (j = 1; j <= m; j++) dp[z][i][j][k] = min(dp[z][i][j][k], dp[z][i][j][k - 1]);
    }

    while (q--) {
        cin >> s >> f >> k;

        k = min(k, n);

        ans = MAXINT;

        for (i = 1; i <= m; i++) ans = min(ans, dp[s][f][i][k]);

        cout << ans << endl;
    }

    re 0;
}


#include <iostream>
#include <cstdio>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iterator>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define x0 abc
#define y0 ABC
#define x1 abcd
#define y1 ABCD
#define xn abcde
#define yn ABCDE

#define l_b lower_bound
#define u_b upper_bound

#define in insert
#define er erase

#define fi first
#define se second
#define m_p make_pair

#define p_b push_back
#define po_b pop_back

#define uns unsigned

#define ld long double
#define ll long long

#define cont continue
#define re return

#define MAXLL 9000000000000000000LL
#define MAXINT 2000000000

#define MINLL -9000000000000000000LL
#define MININT -2000000000

#define pi acos(-1.)

#define endl '\n'

#define OUT cout << "-------" << endl;

using namespace std;

using namespace __gnu_pbds;

typedef tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

int a[181][181], mx[181][181][181], dp[181][181], ans[181][181][181], n, m, i, j, z, k, q, nom, s, f;

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m >> q;

    for (i = 1; i <= m; i++)
        for (j = 1; j <= n; j++)
            for (z = 1; z <= n; z++) cin >> mx[i][j][z];

    for (i = 1; i <= n; i++)
        for (j = 1; j <= n; j++) a[i][j] = MAXINT;

    for (nom = 1; nom <= m; nom++) {
        for (i = 1; i <= n; i++)
            for (j = 1; j <= n; j++) dp[i][j] = mx[nom][i][j];

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

        for (i = 1; i <= n; i++)
            for (j = 1; j <= n; j++) a[i][j] = min(a[i][j], dp[i][j]);
    }

    for (s = 1; s <= n; s++) {
        for (i = 0; i <= n; i++)
            for (j = 1; j <= n; j++) dp[i][j] = MAXINT;

        dp[0][s] = 0;

        for (i = 0; i <= n - 1; i++)
            for (j = 1; j <= n; j++)
                if (dp[i][j] != MAXINT) {
                    for (z = 1; z <= n; z++) dp[i + 1][z] = min(dp[i + 1][z], dp[i][j] + a[j][z]);
                }

        for (i = 1; i <= n; i++)
            for (j = 1; j <= n; j++) ans[s][j][i] = dp[i][j];
    }

    while (q--) {
        cin >> s >> f >> k;

        k++;

        cout << ans[s][f][min(k, n)] << endl;
    }

    re 0;
}


//In the name of GOD
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const ll maxn = 70;
const ll mod = 1e9 + 7;
const ll inf = 1e18;

#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt" , "r+" , stdin) ; freopen("output.txt" , "w+" , stdout);
#define pb push_back
#define pll pair<ll, ll>
#define F first
#define S second

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

int main(){
	fast_io;

	memset(b, 63, sizeof b);
	memset(c, 63, sizeof c);

	cin >> n >> m >> r;

	for(ll i = 0; i < m; i++){
		for(ll j = 0; j < n; j++){
			for(ll k = 0; k < n; k++){
				cin >> a[i][j][k];
				b[i][j][k] = a[i][j][k];
			}
			b[i][j][j] = 0;
		}
	}

	for(ll l = 0; l < m; l++){
		for(ll k = 0; k < n; k++){
			for(ll i = 0; i < n; i++){
				for(ll j = 0; j < n; j++){
					b[l][i][j] = min(b[l][i][j], b[l][i][k] + b[l][k][j]);
					c[0][i][j] = min(c[0][i][j], b[l][i][j]);
				}
			}
		}
	}

	for(ll k = 1; k < n; k++){
		for(ll i = 0; i < n; i++){
			for(ll j = 0; j < n; j++){
				for(ll l = 0; l < n; l++){
					c[k][i][j] = min(c[k][i][j], c[0][i][l] + c[k - 1][l][j]);
				}
			}
		}
	}

	for(ll i = 0; i < r; i++){
		cin >> s >> t >> x;
		cout << c[x][s - 1][t - 1] << '\n';
	}

	return 0;
}


//In the name of GOD
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const ll maxn = 70;
const ll mod = 1e9 + 7;
const ll inf = 1e18;

#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt" , "r+" , stdin) ; freopen("output.txt" , "w+" , stdout);
#define pb push_back
#define pll pair<ll, ll>
#define F first
#define S second

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

int main(){
	fast_io;

	memset(b, 63, sizeof b);
	memset(c, 63, sizeof c);

	cin >> n >> m >> r;

	for(ll i = 0; i < m; i++){
		for(ll j = 0; j < n; j++){
			for(ll k = 0; k < n; k++){
				cin >> a[i][j][k];
				b[i][j][k] = a[i][j][k];
			}
			b[i][j][j] = 0;
		}
	}

	for(ll l = 0; l < m; l++){
		for(ll k = 0; k < n; k++){
			for(ll i = 0; i < n; i++){
				for(ll j = 0; j < n; j++){
					b[l][i][j] = min(b[l][i][j], b[l][i][k] + b[l][k][j]);
					c[0][i][j] = min(c[0][i][j], b[l][i][j]);
				}
			}
		}
	}

	for(ll k = 1; k < n; k++){
		for(ll i = 0; i < n; i++){
			for(ll j = 0; j < n; j++){
				for(ll l = 0; l < n; l++){
					c[k][i][j] = min(c[k][i][j], c[0][i][l] + c[k - 1][l][j]);
				}
			}
		}
	}

	for(ll i = 0; i < r; i++){
		cin >> s >> t >> x;
		cout << c[min(n - 1, x)][s - 1][t - 1] << '\n';
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 60 + 3;

int n, m, r, a[MAXN][MAXN][MAXN], dp[MAXN][MAXN][MAXN];

void inpre();

int main() {
    memset(dp, 127, sizeof dp);
    inpre();
    for (int i = 1; i <= n; i++)
        for (int j = 0; j < n; j++)
            for (int u = 0; u < n; u++)
                for (int v = 0; v < n; v++)
                    dp[i][u][v] = min(dp[i][u][v], dp[0][u][j] + dp[i - 1][j][v]);
    while (r--) {
        int s, t, k;
        cin >> s >> t >> k;
        cout << dp[min(k, n)][--s][--t] << '\n';
    }
    return 0;
}
void inpre() {
    cin >> n >> m >> r;
    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 j = 0; j < n; j++) for (int u = 0; u < n; u++) for (int v = 0; v < n; v++) a[i][u][v] = min(a[i][u][v], a[i][u][j] + a[i][j][v]);
        for (int j = 0; j < n; j++)
            for (int k = 0; k < n; k++)
                dp[0][j][k] = min(dp[0][j][k], a[i][j][k]);
    }
}


/* -*- coding: utf-8 -*-
 *
 * 0187B.cc: B. AlgoRace
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>

using namespace std;

/* constant */

const int MAX_N = 60;
const int MAX_M = 60;
const int MAX_K = MAX_N;
const int INF = 1 << 29;

/* typedef */

/* global variables */

int cds[MAX_M][MAX_N][MAX_N], ds[MAX_K][MAX_N][MAX_N];

/* subroutines */

inline void setmin(int &a, int b) { if (a > b) a = b; }

void printmat(int n, int ds[MAX_N][MAX_N]) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) printf("%d ", ds[i][j]);
    putchar('\n');
  }
}

/* main */

int main() {
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);

  int n, m, r;
  scanf("%d%d%d", &n, &m, &r);
  int maxk = n - 1;

  for (int k = 0; k < MAX_K; k++)
    for (int i = 0; i < n; i++) fill(ds[k][i], ds[k][i] + n, INF);

  for (int c = 0; c < m; c++) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) scanf("%d", &cds[c][i][j]);

    for (int l = 0; l < n; l++)
      for (int i = 0; i < n; i++)
	for (int j = 0; j < n; j++)
	  setmin(cds[c][i][j], cds[c][i][l] + cds[c][l][j]);
    //printmat(n, cds[c]);

    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
	setmin(ds[0][i][j], cds[c][i][j]);
  }
  //printmat(n, ds[0]);

  for (int k = 0; k < maxk; k++)
    for (int l = 0; l < n; l++)
      for (int i = 0; i < n; i++)
	for (int j = 0; j < n; j++)
	  setmin(ds[k + 1][i][j], ds[k][i][l] + ds[0][l][j]);

  while (r--) {
    int s, t, k;
    scanf("%d%d%d", &s, &t, &k);
    s--, t--;
    setmin(k, maxk);

    int mind = INF;
    for (int i = 0; i <= k; i++) setmin(mind, ds[i][s][t]);

    printf("%d\n", mind);
  }

  return 0;
}


#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
#define read(a) scanf("%d" &a)
#define readl(a) scanf("%lld", &a)
#define readf(a) scanf("%lf", &a)
#define readc(a) scanf("%c", &a)
#define reads(a) scanf("%s", a)
#define Buff ios::sync_with_stdio(false)
#define mem(a) memset(a, 0, sizeof a)
#define pb push_back
const int INF = 1e9 + 7;
const int N = 1e2 + 7;
const int M = 1e3 + 7;
const ll mod = 1e9 + 7;
int dis[N][N][N], dp[N][N][M];
signed main()
{
    Buff;
    int n, m, q;
    cin >> n >> m >> q;

    // Floyed 求出每个车的最短路
    memset(dis, 0x3f, sizeof dis);
    for(int car = 1; car <= m; car++)
    {
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                cin >> dis[car][i][j];
        for(int k = 1; k <= n; k++)
            for(int i = 1; i <= n; i++)
                for(int j = 1; j <= n; j++)
                    dis[car][i][j] = min(dis[car][i][j], dis[car][i][k] + dis[car][k][j]);
    }

    // 处理出 dp[i][j][0]
    memset(dp, 0x3f, sizeof dp);
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            for(int car = 1; car <= m; car++)
                dp[i][j][0] = min(dp[i][j][0], dis[car][i][j]);

    // 递推处理出 dp[i][j][k]
    for(int ki = 1; ki <= 1000; ki++)
    {
        // 不用
        for(int si = 1; si <= n; si++)
            for(int ti = 1; ti <= n; ti++)
                dp[si][ti][ki] = dp[si][ti][ki-1];

        // 在 中转点 k 用一次
        for(int k = 1; k <= n; k++)
            for(int si = 1; si <= n; si++)
                for(int ti = 1; ti <= n; ti++)
                    dp[si][ti][ki] = min(dp[si][ti][ki], dp[si][k][ki-1] + dp[k][ti][0]);
    }

    while(q--)
    {
        int si, ti, ki;
        cin >> si >> ti >> ki;
        cout << dp[si][ti][ki] << "\n";
    }
    return 0;
}

/**
* Date: Finished in 2020年10月13日00:53:37
状态表示：
    dp[i][j][k]: i --> j 换车总次数为 k 的方案集合
属性：
    Min
集合划分：
    当前是否使用换车机会
    1）不使用：
        dp[i][j][k] = dp[i][j][k-1]
    2）使用：
        如果使用一定在某一个中转点 u 使用
        dp[i][j][k] = min(dp[i][u][k-1] + dp[u][j][0]);
**/


#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
#define read(a) scanf("%d", &a)
#define readl(a) scanf("%lld", &a)
#define readf(a) scanf("%lf", &a)
#define readc(a) scanf("%c", &a)
#define reads(a) scanf("%s", a)
#define Buff ios::sync_with_stdio(false)
#define mem(a) memset(a, 0, sizeof a)
#define pb push_back
const int INF = 1e9 + 7;
const int N = 1e2 + 7;
const int M = 1e3 + 7;
const ll mod = 1e9 + 7;
int dis[N][N][N], dp[N][N][M];
signed main()
{
    // Buff;
    int n, m, q;
    read(n); read(m); read(q);

    // Floyed 求出每个车的最短路
    memset(dis, 0x3f, sizeof dis);
    for(int car = 1; car <= m; car++)
    {
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                cin >> dis[car][i][j];
        for(int k = 1; k <= n; k++)
            for(int i = 1; i <= n; i++)
                for(int j = 1; j <= n; j++)
                    dis[car][i][j] = min(dis[car][i][j], dis[car][i][k] + dis[car][k][j]);
    }

    // 处理出 dp[i][j][0]
    memset(dp, 0x3f, sizeof dp);
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
        {
            dp[i][j][0] = INF;
            for(int car = 1; car <= m; car++)
                dp[i][j][0] = min(dp[i][j][0], dis[car][i][j]);
        }

    // 递推处理出 dp[i][j][k]
    for(int ki = 1; ki <= 1000; ki++)
    {
        // 不用
        for(int si = 1; si <= n; si++)
            for(int ti = 1; ti <= n; ti++)
                dp[si][ti][ki] = dp[si][ti][ki-1];

        // 在 中转点 k 用一次
        for(int k = 1; k <= n; k++)
            for(int si = 1; si <= n; si++)
                for(int ti = 1; ti <= n; ti++)
                    dp[si][ti][ki] = min(dp[si][ti][ki], dp[si][k][ki-1] + dp[k][ti][0]);
    }

    while(q--)
    {
        int si, ti, ki;
        read(si); read(ti); read(ki);
        printf("%d\n", dp[si][ti][ki]);
    }
    return 0;
}

/**
* Date: Finished in 2020年10月13日00:53:37
状态表示：
    dp[i][j][k]: i --> j 换车总次数为 k 的方案集合
属性：
    Min
集合划分：
    当前是否使用换车机会
    1）不使用：
        dp[i][j][k] = dp[i][j][k-1]
    2）使用：
        如果使用一定在某一个中转点 u 使用
        dp[i][j][k] = min(dp[i][u][k-1] + dp[u][j][0]);
**/


#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
#define read(a) scanf("%d" &a)
#define readl(a) scanf("%lld", &a)
#define readf(a) scanf("%lf", &a)
#define readc(a) scanf("%c", &a)
#define reads(a) scanf("%s", a)
#define Buff ios::sync_with_stdio(false)
#define mem(a) memset(a, 0, sizeof a)
#define pb push_back
const int INF = 1e9 + 7;
const int N = 1e2 + 7;
const int M = 1e3 + 7;
const ll mod = 1e9 + 7;
int dis[N][N][N], dp[N][N][M];
signed main()
{
    // Buff;
    int n, m, q;
    read(n); read(m); read(q);

    // Floyed 求出每个车的最短路
    memset(dis, 0x3f, sizeof dis);
    for(int car = 1; car <= m; car++)
    {
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                cin >> dis[car][i][j];
        for(int k = 1; k <= n; k++)
            for(int i = 1; i <= n; i++)
                for(int j = 1; j <= n; j++)
                    dis[car][i][j] = min(dis[car][i][j], dis[car][i][k] + dis[car][k][j]);
    }

    // 处理出 dp[i][j][0]
    memset(dp, 0x3f, sizeof dp);
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
        {
            dp[i][j][0] = INF;
            for(int car = 1; car <= m; car++)
                dp[i][j][0] = min(dp[i][j][0], dis[car][i][j]);
        }

    // 递推处理出 dp[i][j][k]
    for(int ki = 1; ki <= 1000; ki++)
    {
        // 不用
        for(int si = 1; si <= n; si++)
            for(int ti = 1; ti <= n; ti++)
                dp[si][ti][ki] = dp[si][ti][ki-1];

        // 在 中转点 k 用一次
        for(int k = 1; k <= n; k++)
            for(int si = 1; si <= n; si++)
                for(int ti = 1; ti <= n; ti++)
                    dp[si][ti][ki] = min(dp[si][ti][ki], dp[si][k][ki-1] + dp[k][ti][0]);
    }

    while(q--)
    {
        int si, ti, ki;
        read(si); read(ti); read(ki);
        printf("%d\n", dp[si][ti][ki]);
    }
    return 0;
}

/**
* Date: Finished in 2020年10月13日00:53:37
状态表示：
    dp[i][j][k]: i --> j 换车总次数为 k 的方案集合
属性：
    Min
集合划分：
    当前是否使用换车机会
    1）不使用：
        dp[i][j][k] = dp[i][j][k-1]
    2）使用：
        如果使用一定在某一个中转点 u 使用
        dp[i][j][k] = min(dp[i][u][k-1] + dp[u][j][0]);
**/


#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
#define read(a) scanf("%d", &a)
#define readl(a) scanf("%lld", &a)
#define readf(a) scanf("%lf", &a)
#define readc(a) scanf("%c", &a)
#define reads(a) scanf("%s", a)
#define Buff ios::sync_with_stdio(false)
#define mem(a) memset(a, 0, sizeof a)
#define pb push_back
const int INF = 1e9 + 7;
const int N = 1e2 + 7;
const int M = 1e3 + 7;
const ll mod = 1e9 + 7;
int dis[N][N][N], dp[N][N][M];
signed main()
{
    // Buff;
    int n, m, q;
    read(n); read(m); read(q);

    // Floyed 求出每个车的最短路
    memset(dis, 0x3f, sizeof dis);
    for(int car = 1; car <= m; car++)
    {
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                cin >> dis[car][i][j];
        for(int k = 1; k <= n; k++)
            for(int i = 1; i <= n; i++)
                for(int j = 1; j <= n; j++)
                    dis[car][i][j] = min(dis[car][i][j], dis[car][i][k] + dis[car][k][j]);
    }

    // 处理出 dp[i][j][0]
    memset(dp, 0x3f, sizeof dp);
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
        {
            dp[i][j][0] = INF;
            for(int car = 1; car <= m; car++)
                dp[i][j][0] = min(dp[i][j][0], dis[car][i][j]);
        }

    // 递推处理出 dp[i][j][k]
    for(int ki = 1; ki <= n; ki++)
    {
        // 不用
        for(int si = 1; si <= n; si++)
            for(int ti = 1; ti <= n; ti++)
                dp[si][ti][ki] = dp[si][ti][ki-1];

        // 在 中转点 k 用一次
        for(int k = 1; k <= n; k++)
            for(int si = 1; si <= n; si++)
                for(int ti = 1; ti <= n; ti++)
                    dp[si][ti][ki] = min(dp[si][ti][ki], dp[si][k][ki-1] + dp[k][ti][0]);
    }

    while(q--)
    {
        int si, ti, ki;
        read(si); read(ti); read(ki);
        ki = min(ki, n);
        printf("%d\n", dp[si][ti][ki]);
    }
    return 0;
}

/**
* Date: Finished in 2020年10月13日00:53:37
状态表示：
    dp[i][j][k]: i --> j 换车总次数为 k 的方案集合
属性：
    Min
集合划分：
    当前是否使用换车机会
    1）不使用：
        dp[i][j][k] = dp[i][j][k-1]
    2）使用：
        如果使用一定在某一个中转点 u 使用
        dp[i][j][k] = min(dp[i][u][k-1] + dp[u][j][0]);
**/


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define DEBUG
#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
	template <typename Arg1>
	void __f(const char* name, Arg1&& arg1){
		cerr << name << " : " << arg1 << std::endl;
	}
	template <typename Arg1, typename... Args>
	void __f(const char* names, Arg1&& arg1, Args&&... args){
		const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
	}
#else
#define debug(...)
#endif
#define FOR(i,a,b) 	for(int i=a;i<b;++i)
#define RFOR(i,a,b) 	for(int i=a;i>=b;--i)
#define ln 		"\n"
#define mp make_pair
#define pb push_back
#define sz(a)	ll(a.size())
#define F first
#define S second
#define all(c)	c.begin(),c.end()
#define trace(c,x) for(auto &x:c)
#define pii pair<ll,ll>
typedef long long ll;
typedef long double ld;
typedef	priority_queue<pii,std::vector<pii>,greater<pii> > revpr;

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> pbds;
// ordered_set X
//K-th smallest
//*X.find_by_order(k-1)
//NO OF ELEMENTS < A
//X.order_of_key(A)

const int L=62;
const int M=1e3+7;
int s, t, k;
int n, m, r;
ll dp[L][L][L], out[M][L][L];
void floyd()
{
	FOR(car, 0, m)
		FOR(mid, 0, n)
			FOR(i, 0, n)
				FOR(j, 0, n)
					dp[car][i][j] = min(dp[car][i][j], dp[car][i][mid] + dp[car][mid][j]);
}
void solve()
{
	FOR(i,0,1000)
		FOR(j,0,n)
			FOR(k,0,n)
				out[i][j][k] = LLONG_MAX;
	FOR(i,0,m)
		FOR(j,0,n)
			FOR(k,0,n)
				out[0][j][k] = min(out[0][j][k], dp[i][j][k]);

    FOR(changes,1,n)
    	FOR(mid,0,n)
    		FOR(i,0,n)
    			FOR(j,0,n)
	   				out[changes][i][j] = min(out[changes][i][j], out[changes - 1][i][mid] + out[0][mid][j]);
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> m >> r;
	FOR(i,0,m)
		FOR(j,0,n)
			FOR(k,0,n)
				cin >> dp[i][j][k];
    floyd();
    solve();
	while(r--)
	{
		cin >> s >> t >> k;
		s--, t--;
		k = min(k,n-1);
		cout << out[k][s][t] << ln;
	}
	return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define DEBUG
#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
	template <typename Arg1>
	void __f(const char* name, Arg1&& arg1){
		cerr << name << " : " << arg1 << std::endl;
	}
	template <typename Arg1, typename... Args>
	void __f(const char* names, Arg1&& arg1, Args&&... args){
		const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
	}
#else
#define debug(...)
#endif
#define FOR(i,a,b) 	for(int i=a;i<b;++i)
#define RFOR(i,a,b) 	for(int i=a;i>=b;--i)
#define ln 		"\n"
#define mp make_pair
#define pb push_back
#define sz(a)	ll(a.size())
#define F first
#define S second
#define all(c)	c.begin(),c.end()
#define trace(c,x) for(auto &x:c)
#define pii pair<ll,ll>
typedef long long ll;
typedef long double ld;
typedef	priority_queue<pii,std::vector<pii>,greater<pii> > revpr;

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> pbds;
// ordered_set X
//K-th smallest
//*X.find_by_order(k-1)
//NO OF ELEMENTS < A
//X.order_of_key(A)

const int L=62;
const int M=1e3+7;
int s, t, k;
int n, m, r;
ll dp[L][L][L], out[M][L][L];
void floyd()
{
	FOR(car, 0, m)
		FOR(mid, 0, n)
			FOR(i, 0, n)
				FOR(j, 0, n)
					dp[car][i][j] = min(dp[car][i][j], dp[car][i][mid] + dp[car][mid][j]);
}
void solve()
{
	FOR(i,0,1000)
		FOR(j,0,n)
			FOR(k,0,n)
				out[i][j][k] = LLONG_MAX;
	FOR(i,0,m)
		FOR(j,0,n)
			FOR(k,0,n)
				out[0][j][k] = min(out[0][j][k], dp[i][j][k]);

    FOR(changes,1,n)
    	FOR(mid,0,n)
    		FOR(i,0,n)
    			FOR(j,0,n)
	   				out[changes][i][j] = min(out[changes][i][j], out[changes - 1][i][mid] + out[0][mid][j]);
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> m >> r;
	FOR(i,0,m)
		FOR(j,0,n)
			FOR(k,0,n)
				cin >> dp[i][j][k];
    floyd();
    solve();
	while(r--)
	{
		cin >> s >> t >> k;
		s--, t--;
		k = min(k,n);
		cout << out[k][s][t] << ln;
	}
	return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define DEBUG
#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
	template <typename Arg1>
	void __f(const char* name, Arg1&& arg1){
		cerr << name << " : " << arg1 << std::endl;
	}
	template <typename Arg1, typename... Args>
	void __f(const char* names, Arg1&& arg1, Args&&... args){
		const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
	}
#else
#define debug(...)
#endif
#define FOR(i,a,b) 	for(int i=a;i<b;++i)
#define RFOR(i,a,b) 	for(int i=a;i>=b;--i)
#define ln 		"\n"
#define mp make_pair
#define pb push_back
#define sz(a)	ll(a.size())
#define F first
#define S second
#define all(c)	c.begin(),c.end()
#define trace(c,x) for(auto &x:c)
#define pii pair<ll,ll>
typedef long long ll;
typedef long double ld;
typedef	priority_queue<pii,std::vector<pii>,greater<pii> > revpr;

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> pbds;
// ordered_set X
//K-th smallest
//*X.find_by_order(k-1)
//NO OF ELEMENTS < A
//X.order_of_key(A)

const int L=62;
int s, t, k;
int n, m, r;
ll dp[L][L][L], ans;
void floyd()
{
	FOR(car, 0, m)
		FOR(mid, 0, n)
			FOR(i, 0, n)
				FOR(j, 0, n)
					dp[car][i][j] = min(dp[car][i][j], dp[car][i][mid] + dp[car][mid][j]);
}
void solve(int car, int x, int sofar, ll dist = 0)
{
	if(x == t)
	{
		ans = min(ans, dist);
		return;
	}
	if(dist >= ans)return;
	ans = min(ans, dist + dp[car][x][t]);
	if(sofar < min(k, n))
	{
		FOR(i,0,m)
		{
			if(i == car)continue;
			FOR(j,0,n)
			{
				if(j == x)continue;
				solve(i, j, sofar + 1, dist + dp[car][x][j]);
			}
		}
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> m >> r;
	FOR(i,0,m)
		FOR(j,0,n)
			FOR(k,0,n)
				cin >> dp[i][j][k];
    floyd();
	while(r--)
	{
		cin >> s >> t >> k;
		ans = LLONG_MAX;
		s--, t--;
		FOR(i,0,m)
			solve(i, s, 0);
		cout << ans << ln;
	}
	return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define DEBUG
#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
	template <typename Arg1>
	void __f(const char* name, Arg1&& arg1){
		cerr << name << " : " << arg1 << std::endl;
	}
	template <typename Arg1, typename... Args>
	void __f(const char* names, Arg1&& arg1, Args&&... args){
		const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
	}
#else
#define debug(...)
#endif
#define FOR(i,a,b) 	for(int i=a;i<b;++i)
#define RFOR(i,a,b) 	for(int i=a;i>=b;--i)
#define ln 		"\n"
#define mp make_pair
#define pb push_back
#define sz(a)	ll(a.size())
#define F first
#define S second
#define all(c)	c.begin(),c.end()
#define trace(c,x) for(auto &x:c)
#define pii pair<ll,ll>
typedef long long ll;
typedef long double ld;
typedef	priority_queue<pii,std::vector<pii>,greater<pii> > revpr;

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> pbds;
// ordered_set X
//K-th smallest
//*X.find_by_order(k-1)
//NO OF ELEMENTS < A
//X.order_of_key(A)

const int L=62;
int s, t, k;
int n, m, r;
ll dp[L][L][L], ans;
void floyd()
{
	FOR(car, 0, m)
		FOR(mid, 0, n)
			FOR(i, 0, n)
				FOR(j, 0, n)
					dp[car][i][j] = min(dp[car][i][j], dp[car][i][mid] + dp[car][mid][j]);
}
void solve(int car, int x, int sofar, ll dist = 0)
{
	if(x == t)
	{
		ans = min(ans, dist);
		return;
	}
	if(dist >= ans)return;
	ans = min(ans, dist + dp[car][x][t]);
	if(sofar < k)
	{
		FOR(i,0,m)
		{
			if(i == car)continue;
			FOR(j,0,n)
			{
				if(j == x)continue;
				solve(i, j, sofar + 1, dist + dp[car][x][j]);
			}
		}
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> m >> r;
	FOR(i,0,m)
		FOR(j,0,n)
			FOR(k,0,n)
				cin >> dp[i][j][k];
    floyd();
	while(r--)
	{
		cin >> s >> t >> k;
		ans = LLONG_MAX;
		s--, t--;
		FOR(i,0,m)
			solve(i, s, 0);
		cout << ans << ln;
	}
	return 0;
}


// " --- IN THE NAME OF ALLAH --- " // ;)
// ------------sphr_agz------------ // ;)
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;

typedef long double ld;
typedef long long int lli;
typedef pair <lli, lli> pii;
#define pb push_back
#define recout(x) return cout << (x), 0
#define f first
#define s second
#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define stp(x) cout << setprecision(x) << fixed
#define mk make_pair
#define all(x) x.begin(), x.end()
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
const lli N = 60 + 10, N2 = 2e5, delta = 10067, mod = 1234, oo = 1e9, LOG = 20, SQ = 300;
const ld PI = 3.141592653589793;

lli dp[N][N][N], dis[N][N][N];

int32_t main() {
	fast;
	lli n, m, r;
	cin >> n >> m >> r;
	for (lli i = 0; i < N; i++)
		for (lli j = 0; j < n; j++)
			memset (dp[i][j], 63, sizeof dp[i][j]);
	for (lli i = 0; i < m; i++)
		for (lli j = 0; j < n; j++)
			for (lli k = 0; k < n; k++)
				cin >> dis[i][j][k];
	for (lli id = 0; id < m; id++)
		for (lli k = 0; k < n; k++)
			for (lli i = 0; i < n; i++)
				for (lli j = 0; j < n; j++)
					dis[id][i][j] = min (dis[id][i][j], dis[id][i][k] + dis[id][k][j]);
	for (lli id = 0; id < m; id++)
		for (lli i = 0; i < n; i++)
			for (lli j = 0; j < n; j++)
				dp[0][i][j] = min (dp[0][i][j], dis[id][i][j]);
	for (lli k = 1; k < N; k++)
		for (lli i = 0; i < n; i++)
			for (lli j = 0; j < n; j++)
				for (lli t = 0; t < n; t++)
					dp[k][i][j] = min (dp[k][i][j], dp[k - 1][i][t] + dp[0][t][j]);
	while (r--) {
		lli s, t, k;
		cin >> s >> t >> k;
		s--, t--;
		cout << dp[min (k, n - 1)][s][t] << '\n';
	}
	return 0;
}


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

typedef long long int   ll;
typedef long double     ld;
typedef pair<ll, ll>    pll;
typedef pair<int, int>  pii;

#define all(x)          x.begin(), x.end()
#define kill(x)         return cout << x << endl, 0
#define X               first
#define Y               second
#define sep             ' '
#define endl            '\n'
#define file_io         freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

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

const ll MAXN  = 60 + 10;
const ll MAXK  = 1e3 + 5;
const ll INF   = 1e9;
const ll MOD   = 1e9 + 7; // 99824435;

int n, m, r, dist[MAXN][MAXN], dp[MAXK][MAXN][MAXN], mn[MAXN][MAXN];

int main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    memset(dp, 63, sizeof(dp));
    cin >> n >> m >> r;
    for (int i = 0; i < m; i++) {
        for (int j = 1; j <= n; j++) {
            for (int k = 1; k <= n; k++) {
                cin >> dist[j][k];
            }
        }
        for (int j = 1; j <= n; j++) {
            for (int k = 1; k <= n; k++) {
                for (int l = 1; l <= n; l++) {
                    dist[k][l] = min(dist[k][l], dist[k][j] + dist[j][l]);
                }
            }
        }
        for (int j = 1; j <= n; j++) {
            for (int k = 1; k <= n; k++) {
                dp[0][j][k] = min(dp[0][j][k], dist[j][k]);
            }
        }
    }
    for (int k = 1; k < MAXK; k++) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                for (int mid = 1; mid <= n; mid++) {
                    dp[k][i][j] = min(dp[k][i][j], dp[0][i][mid] + dp[k - 1][mid][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 int   ll;
typedef long double     ld;
typedef pair<ll, ll>    pll;
typedef pair<int, int>  pii;

#define all(x)          x.begin(), x.end()
#define kill(x)         return cout << x << endl, 0
#define X               first
#define Y               second
#define sep             ' '
#define endl            '\n'
#define file_io         freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

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

const ll MAXN  = 60 + 10;
const ll MAXK  = 1e3 + 10;
const ll INF   = 1e9;
const ll MOD   = 1e9 + 7; // 99824435;

int n, m, r, dist[MAXN][MAXN][MAXN], dp[MAXN][MAXN][MAXK], mn[MAXN][MAXN];

int main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    for (int i = 0; i < MAXN; i++) {
        fill(mn[i], mn[i] + MAXN, INF);
        for (int j = 0; j < MAXN; j++) {
            fill(dp[i][j], dp[i][j] + MAXK, INF);
        }
    }
    cin >> n >> m >> r;
    for (int i = 0; i < m; i++) {
        for (int j = 1; j <= n; j++) {
            for (int k = 1; k <= n; k++) {
                cin >> dist[i][j][k];
            }
        }
        for (int j = 1; j <= n; j++) {
            for (int k = 1; k <= n; k++) {
                for (int l = 1; l <= n; l++) {
                    dist[i][k][l] = min(dist[i][k][l], dist[i][k][j] + dist[i][j][l]);
                    mn[k][l] = min(mn[k][l], dist[i][k][l]);
                }
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            dp[i][j][0] = mn[i][j];
        }
    }
    for (int k = 1; k < MAXK; k++) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                for (int mid = 1; mid <= n; mid++) {
                    dp[i][j][k] = min(dp[i][j][k], mn[i][mid] + dp[mid][j][k - 1]);
                }
            }
        }
    }
    while (r--) {
        int s, t, k;
        cin >> s >> t >> k;
        cout << dp[s][t][k] << endl;
    }
    return 0;
}


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

typedef long long int   ll;
typedef long double     ld;
typedef pair<ll, ll>    pll;
typedef pair<int, int>  pii;

#define all(x)          x.begin(), x.end()
#define kill(x)         return cout << x << endl, 0
#define X               first
#define Y               second
#define sep             ' '
#define endl            '\n'
#define file_io         freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

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

const ll MAXN  = 60 + 10;
const ll MAXK  = 1e3 + 10;
const ll INF   = 1e9;
const ll MOD   = 1e9 + 7; // 99824435;

ll n, m, r, dist[MAXN][MAXN][MAXN], dp[MAXN][MAXN][MAXK], mn[MAXN][MAXN];

int main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    for (ll i = 0; i < MAXN; i++) {
        fill(mn[i], mn[i] + MAXN, INF);
        for (ll j = 0; j < MAXN; j++) {
            fill(dp[i][j], dp[i][j] + MAXK, INF);
        }
    }
    cin >> n >> m >> r;
    for (ll i = 0; i < m; i++) {
        for (ll j = 1; j <= n; j++) {
            for (ll k = 1; k <= n; k++) {
                cin >> dist[i][j][k];
            }
        }
        for (ll j = 1; j <= n; j++) {
            for (ll k = 1; k <= n; k++) {
                for (ll l = 1; l <= n; l++) {
                    dist[i][k][l] = min(dist[i][k][l], dist[i][k][j] + dist[i][j][l]);
                    mn[k][l] = min(mn[k][l], dist[i][k][l]);
                }
            }
        }
    }
    for (ll i = 1; i <= n; i++) {
        for (ll j = 1; j <= n; j++) {
            dp[i][j][0] = mn[i][j];
        }
    }
    for (ll k = 1; k < MAXK; k++) {
        for (ll i = 1; i <= n; i++) {
            for (ll j = 1; j <= n; j++) {
                for (ll mid = 1; mid <= n; mid++) {
                    dp[i][j][k] = min(dp[i][j][k], mn[i][mid] + dp[mid][j][k - 1]);
                }
            }
        }
    }
    while (r--) {
        ll s, t, k;
        cin >> s >> t >> k;
        cout << dp[s][t][k] << endl;
    }
    return 0;
}


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

typedef long long int   ll;
typedef long double     ld;
typedef pair<ll, ll>    pll;
typedef pair<int, int>  pii;

#define all(x)          x.begin(), x.end()
#define kill(x)         return cout << x << endl, 0
#define X               first
#define Y               second
#define sep             ' '
#define endl            '\n'
#define file_io         freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

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

const ll MAXN  = 60 + 10;
const ll MAXK  = 1e3 + 5;
const ll INF   = 1e9;
const ll MOD   = 1e9 + 7; // 99824435;

int n, m, r, dist[MAXN][MAXN], dp[MAXN][MAXN][MAXK], mn[MAXN][MAXN];

int main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    for (int i = 0; i < MAXN; i++) {
        fill(mn[i], mn[i] + MAXN, INF);
        for (int j = 0; j < MAXN; j++) {
            fill(dp[i][j], dp[i][j] + MAXK, INF);
        }
    }
    cin >> n >> m >> r;
    for (int i = 0; i < m; i++) {
        for (int j = 1; j <= n; j++) {
            for (int k = 1; k <= n; k++) {
                cin >> dist[j][k];
            }
        }
        for (int j = 1; j <= n; j++) {
            for (int k = 1; k <= n; k++) {
                for (int l = 1; l <= n; l++) {
                    dist[k][l] = min(dist[k][l], dist[k][j] + dist[j][l]);
                }
            }
        }
        for (int j = 1; j <= n; j++) {
            for (int k = 1; k <= n; k++) {
                mn[j][k] = min(mn[j][k], dist[j][k]);
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            dp[i][j][0] = mn[i][j];
        }
    }
    for (int k = 1; k < MAXK; k++) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                for (int mid = 1; mid <= n; mid++) {
                    dp[i][j][k] = min(dp[i][j][k], mn[i][mid] + dp[mid][j][k - 1]);
                }
            }
        }
    }
    while (r--) {
        int s, t, k;
        cin >> s >> t >> k;
        cout << dp[s][t][k] << endl;
    }
    return 0;
}


#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<map>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstdio>
#include<random>
#include<bitset>
#include<complex>
#include<cassert>
#include<sstream>
#include<set>

using namespace std;

const int inf = 1e8;
const int maxN = 100;
int n, m, r;

vector<vector<vector<int>>> g;
int ans[maxN][maxN][maxN];

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    for(int k = 0; k < maxN; k++){
        for(int i = 0; i < maxN; i++){
            for(int j = 0; j < maxN; j++)ans[k][i][j] = inf;
            ans[k][i][i] = 0;
        }
    }
    cin >> n >> m >> r;
    g.resize(m, vector<vector<int>>(n, vector<int>(n)));
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            for(int k = 0; k < n; k++)cin >> g[i][j][k];
        }
    }
    for(int w = 0; w < m; w++){
        for(int k = 0; k < n; k++){
            for(int i = 0; i < n; i++){
                for(int j = 0; j < n; j++){
                    g[w][i][j] = min(g[w][i][j], g[w][i][k] + g[w][k][j]);
                }
            }
        }
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            for(int w = 0; w < m; w++)ans[0][i][j] = min(ans[0][i][j], g[w][i][j]);
        }
    }
    for(int k = 1; k < maxN; k++){
        for(int w = 0; w < n; w++){
            for(int i = 0; i < n; i++){
                for(int j = 0; j < n; j++){
                    ans[k][i][j] = min({ans[k - 1][i][j], ans[k][i][j], ans[k - 1][i][w] + ans[0][w][j]});
                }
            }
        }
    }
    for(int i = 0; i < r; i++){
        int s, f, k; cin >> s >> f >> k; s--; f--;
        cout << ans[k][s][f] << "\n";
    }
    return 0;
}


#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<map>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstdio>
#include<random>
#include<bitset>
#include<complex>
#include<cassert>
#include<sstream>
#include<set>

#define int long long int

using namespace std;

const int inf = 1e15;
const int maxN = 100;
int n, m, r;

vector<vector<vector<int>>> g;
int ans[maxN][maxN][maxN];

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    for(int k = 0; k < maxN; k++){
        for(int i = 0; i < maxN; i++){
            for(int j = 0; j < maxN; j++)ans[k][i][j] = inf;
            ans[k][i][i] = 0;
        }
    }
    cin >> n >> m >> r;
    g.resize(m, vector<vector<int>>(n, vector<int>(n)));
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            for(int k = 0; k < n; k++)cin >> g[i][j][k];
        }
    }
    for(int w = 0; w < m; w++){
        for(int k = 0; k < n; k++){
            for(int i = 0; i < n; i++){
                for(int j = 0; j < n; j++){
                    g[w][i][j] = min(g[w][i][j], g[w][i][k] + g[w][k][j]);
                }
            }
        }
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            for(int w = 0; w < m; w++)ans[0][i][j] = min(ans[0][i][j], g[w][i][j]);
        }
    }
    for(int k = 1; k < maxN; k++){
        for(int w = 0; w < n; w++){
            for(int i = 0; i < n; i++){
                for(int j = 0; j < n; j++){
                    ans[k][i][j] = min({ans[k - 1][i][j], ans[k][i][j], ans[k - 1][i][w] + ans[0][w][j]});
                }
            }
        }
    }
    for(int i = 0; i < r; i++){
        int s, f, k; cin >> s >> f >> k; s--; f--;
        cout << ans[min(99ll, k)][s][f] << "\n";
    }
    return 0;
}


#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<map>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstdio>
#include<random>
#include<bitset>
#include<complex>
#include<cassert>
#include<sstream>
#include<set>

#define int long long int

using namespace std;

const int inf = 1e15;
int n, m, r;

vector<vector<vector<int>>> g;

struct node{
    int v, d, cnt, ind;
    node(int v = 0, int d = 0, int ind = 0, int cnt = 0){
        this->v = v;
        this->ind = ind;
        this->d = d;
        this->cnt = cnt;
    }
};

struct cmp{
    bool operator ()(const node & a, const node & b) const {
        return a.d < b.d;
    }
};

int getR(int s, int f, int k){
    vector<vector<vector<int>>> dist(k + 1, vector<vector<int>>(n, vector<int>(m, inf)));
    multiset<node, cmp> se;
    for(int i = 0; i < m; i++){
        se.insert(node(s, 0, i));
        dist[0][s][i] = 0;
    }
    while(!se.empty()){
        auto w = (*se.begin()); se.erase(se.begin());
        int v = w.v, d = w.d, cnt = w.cnt, ind = w.ind;
        for(int to = 0; to < n; to++){
            if(dist[cnt][to][ind] > d + g[ind][v][to]){
                dist[cnt][to][ind] = d + g[ind][v][to];
                se.insert(node(to, dist[cnt][to][ind], ind, cnt));
            }
        }
        if(cnt < k){
            for(int i = 0; i < m; i++){
                if(dist[cnt + 1][v][i] > dist[cnt][v][ind]){
                    dist[cnt + 1][v][i] = d;
                    se.insert(node(v, dist[cnt + 1][v][i], i, cnt + 1));
                }
            }
        }
    }
    int ans = inf;
    for(int i = 0; i <= k; i++){
        for(int j = 0; j < m; j++)ans = min(ans, dist[i][f][j]);
    }
    return ans;
}

signed main(){
    cin >> n >> m >> r;
    g.resize(m, vector<vector<int>>(n, vector<int>(n)));
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            for(int k = 0; k < n; k++)cin >> g[i][j][k];
        }
    }
    for(int i = 0; i < r; i++){
        int s, f, k; cin >> s >> f >> k; s--; f--;
        cout << getR(s, f, k) << "\n";
    }
    return 0;
}


#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<map>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstdio>
#include<random>
#include<bitset>
#include<complex>
#include<cassert>
#include<sstream>
#include<set>

using namespace std;

const int inf = 1e8;
int n, m, r;

vector<vector<vector<int>>> g;

struct node{
    int v, d, cnt, ind;
    node(int v = 0, int d = 0, int ind = 0, int cnt = 0){
        this->v = v;
        this->ind = ind;
        this->d = d;
        this->cnt = cnt;
    }
};

struct cmp{
    bool operator ()(const node & a, const node & b) const {
        return a.d < b.d;
    }
};

int getR(int s, int f, int k){
    vector<vector<vector<int>>> dist(k + 1, vector<vector<int>>(n, vector<int>(m, inf)));
    multiset<node, cmp> se;
    for(int i = 0; i < m; i++){
        se.insert(node(s, 0, i));
        dist[0][s][i] = 0;
    }
    while(!se.empty()){
        auto w = (*se.begin()); se.erase(se.begin());
        int v = w.v, d = w.d, cnt = w.cnt, ind = w.ind;
        for(int to = 0; to < n; to++){
            if(dist[cnt][to][ind] > d + g[ind][v][to]){
                dist[cnt][to][ind] = d + g[ind][v][to];
                se.insert(node(to, dist[cnt][to][ind], ind, cnt));
            }
        }
        if(cnt < k){
            for(int i = 0; i < m; i++){
                if(dist[cnt + 1][v][i] > dist[cnt][v][ind]){
                    dist[cnt + 1][v][i] = d;
                    se.insert(node(v, dist[cnt + 1][v][i], i, cnt + 1));
                }
            }
        }
    }
    int ans = inf;
    for(int i = 0; i <= k; i++){
        for(int j = 0; j < m; j++)ans = min(ans, dist[i][f][j]);
    }
    return ans;
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> r;
    g.resize(m, vector<vector<int>>(n, vector<int>(n)));
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            for(int k = 0; k < n; k++)cin >> g[i][j][k];
        }
    }
    for(int i = 0; i < r; i++){
        int s, f, k; cin >> s >> f >> k; s--; f--;
        cout << getR(s, f, k) << "\n";
    }
    return 0;
}


#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<map>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstdio>
#include<random>
#include<bitset>
#include<complex>
#include<cassert>
#include<sstream>
#include<set>

using namespace std;

const int inf = 1e8;
const int maxN = 100;
int n, m, r;

vector<vector<vector<int>>> g;
int ans[maxN][maxN][maxN];

struct node{
    int v, d, cnt, ind;
    node(int v = 0, int d = 0, int ind = 0, int cnt = 0){
        this->v = v;
        this->ind = ind;
        this->d = d;
        this->cnt = cnt;
    }
};

struct cmp{
    bool operator ()(const node & a, const node & b) const {
        return a.d < b.d;
    }
};

int getR(int s, int f, int k){
    vector<vector<vector<int>>> dist(k + 1, vector<vector<int>>(n, vector<int>(m, inf)));
    multiset<node, cmp> se;
    for(int i = 0; i < m; i++){
        se.insert(node(s, 0, i));
        dist[0][s][i] = 0;
    }
    while(!se.empty()){
        auto w = (*se.begin()); se.erase(se.begin());
        int v = w.v, d = w.d, cnt = w.cnt, ind = w.ind;
        for(int to = 0; to < n; to++){
            if(dist[cnt][to][ind] > d + g[ind][v][to]){
                dist[cnt][to][ind] = d + g[ind][v][to];
                se.insert(node(to, dist[cnt][to][ind], ind, cnt));
            }
        }
        if(cnt < k){
            for(int i = 0; i < m; i++){
                if(dist[cnt + 1][v][i] > dist[cnt][v][ind]){
                    dist[cnt + 1][v][i] = d;
                    se.insert(node(v, dist[cnt + 1][v][i], i, cnt + 1));
                }
            }
        }
    }
    int ans = inf;
    for(int i = 0; i <= k; i++){
        for(int j = 0; j < m; j++)ans = min(ans, dist[i][f][j]);
    }
    return ans;
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    for(int k = 0; k < maxN; k++){
        for(int i = 0; i < maxN; i++){
            for(int j = 0; j < maxN; j++)ans[k][i][j] = inf;
            ans[k][i][i] = 0;
        }
    }
    cin >> n >> m >> r;
    g.resize(m, vector<vector<int>>(n, vector<int>(n)));
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            for(int k = 0; k < n; k++)cin >> g[i][j][k];
        }
    }
    for(int k = 0; k < maxN; k++){
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++)ans[k][i][j] = getR(i, j, k);
        }
    }
    for(int i = 0; i < r; i++){
        int s, f, k; cin >> s >> f >> k; s--; f--;
        cout << ans[k][s][f] << "\n";
    }
    return 0;
}


#include<bits/stdc++.h>
//#include<unordered_set>
#pragma GCC optimize("Ofast")
using namespace std;
#define int long long
#define die(x) return cout << x << endl, 0
#define FI first
#define SE second
#define all(o) o.begin(), o.end()
#define endl '\n'
#define IOS ios::sync_with_stdio(0), cin.tie(0)
#define FILE freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define SZ(x) ((int)(x).size())
#define PB push_back
#define PF push_front
#define POB pop_back
#define POF pop_front
#define MP make_pair
#define FR front
#define BK back
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef map<int,int> mpi;
typedef set<int> sti;
typedef vector<pii> vii;
typedef map<pii,int> mpii;
typedef set<pii> stii;
typedef long double ld;
typedef long long ll;
int gcd(int x,int y){ return (!y ? x : gcd(y, x%y)); }
int power(int x, int y) { return (!y ? 1 : power(x, y / 2) * power(x, y / 2) * (y % 2 ? x : 1)); }
int fastMax(int x, int y) { return (((y-x)>>(32-1))&(x^y))^y; }
int fastMin(int x, int y) { return (((y-x)>>(32-1))&(x^y))^x; }
const int MAXN=61,MOD=1e9+7,INF=1e9;
const double PI = atan(1.0)*4;
int mod(int x) { return (x % MOD + MOD) % MOD; }
int n,m,r;
vii g[MAXN];
int matric[MAXN][MAXN][MAXN];
int dp[MAXN][MAXN][MAXN];
void floyd(int id){
    for(int k=1;k<=n;k++)for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)matric[id][i][j]=min(matric[id][i][j],matric[id][i][k]+matric[id][k][j]);
}
int MAIN(){
    cin>>n>>m>>r;
    memset(matric,63,sizeof(matric));
    //fill(matric,matric+MAXN,1e14);
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++)for(int k=1;k<=n;k++)cin>>matric[i][j][k];
        floyd(i);
    }
    for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){
        dp[0][i][j]=INF;
        for(int k=1;k<=m;k++)dp[0][i][j]=min(dp[0][i][j],matric[k][i][j]);
    }
    for(int k=1;k<=n;k++){
        for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){
            dp[k][i][j]=INF;
            for(int mid=i;mid<=n;mid++)dp[k][i][j]=min(dp[k][i][j],dp[k-1][i][mid]+dp[0][mid][j]);
        }
    }
    while(r--){
        int s,t,k;cin>>s>>t>>k;
        cout<<dp[min(k,n)][s][t]<<endl;
    }
    return 0;
}
int32_t main(){
    IOS;
    int t=1;
    //cin>>t;
    while(t--)MAIN();
}


#include<bits/stdc++.h>
//#include<unordered_set>
#pragma GCC optimize("Ofast")
using namespace std;
#define int long long
#define die(x) return cout << x << endl, 0
#define FI first
#define SE second
#define all(o) o.begin(), o.end()
#define endl '\n'
#define IOS ios::sync_with_stdio(0), cin.tie(0)
#define FILE freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define SZ(x) ((int)(x).size())
#define PB push_back
#define PF push_front
#define POB pop_back
#define POF pop_front
#define MP make_pair
#define FR front
#define BK back
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef map<int,int> mpi;
typedef set<int> sti;
typedef vector<pii> vii;
typedef map<pii,int> mpii;
typedef set<pii> stii;
typedef long double ld;
typedef long long ll;
int gcd(int x,int y){ return (!y ? x : gcd(y, x%y)); }
int power(int x, int y) { return (!y ? 1 : power(x, y / 2) * power(x, y / 2) * (y % 2 ? x : 1)); }
int fastMax(int x, int y) { return (((y-x)>>(32-1))&(x^y))^y; }
int fastMin(int x, int y) { return (((y-x)>>(32-1))&(x^y))^x; }
const int MAXN=70,MOD=1e9+7,INF=1e18;
const double PI = atan(1.0)*4;
int mod(int x) { return (x % MOD + MOD) % MOD; }
int n,m,r;
int matric[MAXN][MAXN][MAXN];
int dp[MAXN][MAXN][MAXN];
void floyd(int id){
    for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)for(int k=1;k<=n;k++)matric[id][i][j]=min(matric[id][i][j],matric[id][i][k]+matric[id][k][j]);
}
int MAIN(){
    cin>>n>>m>>r;
    //memset(matric,63,sizeof(matric));
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++)for(int k=1;k<=n;k++)cin>>matric[i][j][k];
        floyd(i);
    }
    for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){
        dp[0][i][j]=INF;
        for(int k=1;k<=m;k++)dp[0][i][j]=min(dp[0][i][j],matric[k][i][j]);
    }
    for(int k=1;k<=n;k++){
        for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){
            dp[k][i][j]=INF;
            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]);
        }
    }
    while(r--){
        int s,t,k;cin>>s>>t>>k;
        if(k>n)k=n;
        cout<<dp[k][s][t]<<endl;
    }
    return 0;
}
int32_t main(){
    IOS;
    int t=1;
    //cin>>t;
    while(t--)MAIN();
}


#include<bits/stdc++.h>
//#include<unordered_set>
#pragma GCC optimize("Ofast")
using namespace std;
#define int long long
#define die(x) return cout << x << endl, 0
#define FI first
#define SE second
#define all(o) o.begin(), o.end()
#define endl '\n'
#define IOS ios::sync_with_stdio(0), cin.tie(0)
#define FILE freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define SZ(x) ((int)(x).size())
#define PB push_back
#define PF push_front
#define POB pop_back
#define POF pop_front
#define MP make_pair
#define FR front
#define BK back
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef map<int,int> mpi;
typedef set<int> sti;
typedef vector<pii> vii;
typedef map<pii,int> mpii;
typedef set<pii> stii;
typedef long double ld;
typedef long long ll;
int gcd(int x,int y){ return (!y ? x : gcd(y, x%y)); }
int power(int x, int y) { return (!y ? 1 : power(x, y / 2) * power(x, y / 2) * (y % 2 ? x : 1)); }
int fastMax(int x, int y) { return (((y-x)>>(32-1))&(x^y))^y; }
int fastMin(int x, int y) { return (((y-x)>>(32-1))&(x^y))^x; }
const int MAXN=61,MOD=1e9+7,INF=1e9;
const double PI = atan(1.0)*4;
int mod(int x) { return (x % MOD + MOD) % MOD; }
int n,m,r;
vii g[MAXN];
int matric[MAXN][MAXN][MAXN];
int dp[MAXN][MAXN][MAXN];
void floyd(int id){
    for(int k=1;k<=n;k++)for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)matric[id][i][j]=min(matric[id][i][j],matric[id][i][k]+matric[id][k][j]);
}
int MAIN(){
    cin>>n>>m>>r;
    //memset(matric,63,sizeof(matric));
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++)for(int k=1;k<=n;k++)cin>>matric[i][j][k];
        floyd(i);
    }
    for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){
        dp[0][i][j]=INF;
        for(int k=1;k<=m;k++)dp[0][i][j]=min(dp[0][i][j],matric[k][i][j]);
    }
    for(int k=1;k<=n;k++){
        for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){
            dp[k][i][j]=INF*INF;
            for(int mid=i;mid<=n;mid++)dp[k][i][j]=min(dp[k][i][j],dp[k-1][i][mid]+dp[0][mid][j]);
        }
    }
    while(r--){
        int s,t,k;cin>>s>>t>>k;
        cout<<dp[min(k,n)][s][t]<<endl;
    }
    return 0;
}
int32_t main(){
    IOS;
    int t=1;
    //cin>>t;
    while(t--)MAIN();
}


#include<bits/stdc++.h>
//#include<unordered_set>
#pragma GCC optimize("Ofast")
using namespace std;
#define int long long
#define die(x) return cout << x << endl, 0
#define FI first
#define SE second
#define all(o) o.begin(), o.end()
#define endl '\n'
#define IOS ios::sync_with_stdio(0), cin.tie(0)
#define FILE freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define SZ(x) ((int)(x).size())
#define PB push_back
#define PF push_front
#define POB pop_back
#define POF pop_front
#define MP make_pair
#define FR front
#define BK back
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef map<int,int> mpi;
typedef set<int> sti;
typedef vector<pii> vii;
typedef map<pii,int> mpii;
typedef set<pii> stii;
typedef long double ld;
typedef long long ll;
int gcd(int x,int y){ return (!y ? x : gcd(y, x%y)); }
int power(int x, int y) { return (!y ? 1 : power(x, y / 2) * power(x, y / 2) * (y % 2 ? x : 1)); }
int fastMax(int x, int y) { return (((y-x)>>(32-1))&(x^y))^y; }
int fastMin(int x, int y) { return (((y-x)>>(32-1))&(x^y))^x; }
const int MAXN=70,MOD=1e9+7,INF=1e18;
const double PI = atan(1.0)*4;
int mod(int x) { return (x % MOD + MOD) % MOD; }
int n,m,r;
int matric[MAXN][MAXN][MAXN];
int dp[MAXN][MAXN][MAXN];
void floyd(int id){
    for(int k=1;k<=n;k++)for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)matric[id][i][j]=min(matric[id][i][j],matric[id][i][k]+matric[id][k][j]);
}
int MAIN(){
    cin>>n>>m>>r;
    //memset(matric,63,sizeof(matric));
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++)for(int k=1;k<=n;k++)cin>>matric[i][j][k];
        floyd(i);
    }
    for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){
        dp[0][i][j]=INF;
        for(int k=1;k<=m;k++)dp[0][i][j]=min(dp[0][i][j],matric[k][i][j]);
    }
    for(int k=1;k<=n;k++){
        for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){
            dp[k][i][j]=INF;
            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]);
        }
    }
    while(r--){
        int s,t,k;cin>>s>>t>>k;
        if(k>n)k=n;
        cout<<dp[k][s][t]<<endl;
    }
    return 0;
}
int32_t main(){
    IOS;
    int t=1;
    //cin>>t;
    while(t--)MAIN();
}


#include<bits/stdc++.h>
//#include<unordered_set>
#pragma GCC optimize("Ofast")
using namespace std;
#define int long long
#define die(x) return cout << x << endl, 0
#define FI first
#define SE second
#define all(o) o.begin(), o.end()
#define endl '\n'
#define IOS ios::sync_with_stdio(0), cin.tie(0)
#define FILE freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define SZ(x) ((int)(x).size())
#define PB push_back
#define PF push_front
#define POB pop_back
#define POF pop_front
#define MP make_pair
#define FR front
#define BK back
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef map<int,int> mpi;
typedef set<int> sti;
typedef vector<pii> vii;
typedef map<pii,int> mpii;
typedef set<pii> stii;
typedef long double ld;
typedef long long ll;
int gcd(int x,int y){ return (!y ? x : gcd(y, x%y)); }
int power(int x, int y) { return (!y ? 1 : power(x, y / 2) * power(x, y / 2) * (y % 2 ? x : 1)); }
int fastMax(int x, int y) { return (((y-x)>>(32-1))&(x^y))^y; }
int fastMin(int x, int y) { return (((y-x)>>(32-1))&(x^y))^x; }
const int MAXN=70,MOD=1e9+7,INF=1e18;
const double PI = atan(1.0)*4;
int mod(int x) { return (x % MOD + MOD) % MOD; }
int n,m,r;
int matric[MAXN][MAXN][MAXN];
int dp[MAXN][MAXN][MAXN];
void floyd(int id){
    for(int k=1;k<=n;k++)for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)matric[id][i][j] =min(matric[id][i][j],matric[id][i][k]+matric[id][k][j]);
}
int MAIN(){
    cin>>n>>m>>r;
    //memset(matric,63,sizeof(matric));
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++)for(int k=1;k<=n;k++)cin>>matric[i][j][k];
        floyd(i);
    }
    for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){
        dp[0][i][j]=INF;
        for(int k=1;k<=m;k++)dp[0][i][j]=min(dp[0][i][j],matric[k][i][j]);
    }
    for(int k=1;k<=n;k++){
        for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){
            dp[k][i][j]=INF;
            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]);
        }
    }
    while(r--){
        int s,t,k;cin>>s>>t>>k;
        if(k>n)k=n;
        cout<<dp[k][s][t]<<endl;
    }
    return 0;
}
int32_t main(){
    IOS;
    int t=1;
    //cin>>t;
    while(t--)MAIN();
}


#include<bits/stdc++.h>
//#include<unordered_set>
#pragma GCC optimize("Ofast")
using namespace std;
#define int long long
#define die(x) return cout << x << endl, 0
#define FI first
#define SE second
#define all(o) o.begin(), o.end()
#define endl '\n'
#define IOS ios::sync_with_stdio(0), cin.tie(0)
#define FILE freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define SZ(x) ((int)(x).size())
#define PB push_back
#define PF push_front
#define POB pop_back
#define POF pop_front
#define MP make_pair
#define FR front
#define BK back
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef map<int,int> mpi;
typedef set<int> sti;
typedef vector<pii> vii;
typedef map<pii,int> mpii;
typedef set<pii> stii;
typedef long double ld;
typedef long long ll;
int gcd(int x,int y){ return (!y ? x : gcd(y, x%y)); }
int power(int x, int y) { return (!y ? 1 : power(x, y / 2) * power(x, y / 2) * (y % 2 ? x : 1)); }
int fastMax(int x, int y) { return (((y-x)>>(32-1))&(x^y))^y; }
int fastMin(int x, int y) { return (((y-x)>>(32-1))&(x^y))^x; }
const int MAXN=70,MOD=1e9+7,INF=1e18;
const double PI = atan(1.0)*4;
int mod(int x) { return (x % MOD + MOD) % MOD; }
int n,m,r;
vii g[MAXN];
int matric[MAXN][MAXN][MAXN];
int dp[MAXN][MAXN][MAXN];
void floyd(int id){
    for(int k=1;k<=n;k++)for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)matric[id][i][j]=min(matric[id][i][j],matric[id][i][k]+matric[id][k][j]);
}
int MAIN(){
    cin>>n>>m>>r;
    //memset(matric,63,sizeof(matric));
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++)for(int k=1;k<=n;k++)cin>>matric[i][j][k];
        floyd(i);
    }
    for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){
        dp[0][i][j]=INF;
        for(int k=1;k<=m;k++)dp[0][i][j]=min(dp[0][i][j],matric[k][i][j]);
    }
    for(int k=1;k<=n;k++){
        for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){
            dp[k][i][j]=INF;
            for(int l=i;l<=n;l++)dp[k][i][j]=min(dp[k][i][j],dp[k-1][i][l]+dp[0][l][j]);
        }
    }
    while(r--){
        int s,t,k;cin>>s>>t>>k;
        if(k>n)k=n;
        cout<<dp[k][s][t]<<endl;
    }
    return 0;
}
int32_t main(){
    IOS;
    int t=1;
    //cin>>t;
    while(t--)MAIN();
}


#include<bits/stdc++.h>
//#include<unordered_set>
#pragma GCC optimize("Ofast")
using namespace std;
#define int long long
#define die(x) return cout << x << endl, 0
#define FI first
#define SE second
#define all(o) o.begin(), o.end()
#define endl '\n'
#define IOS ios::sync_with_stdio(0), cin.tie(0)
#define FILE freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define SZ(x) ((int)(x).size())
#define PB push_back
#define PF push_front
#define POB pop_back
#define POF pop_front
#define MP make_pair
#define FR front
#define BK back
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef map<int,int> mpi;
typedef set<int> sti;
typedef vector<pii> vii;
typedef map<pii,int> mpii;
typedef set<pii> stii;
typedef long double ld;
typedef long long ll;
int gcd(int x,int y){ return (!y ? x : gcd(y, x%y)); }
int power(int x, int y) { return (!y ? 1 : power(x, y / 2) * power(x, y / 2) * (y % 2 ? x : 1)); }
int fastMax(int x, int y) { return (((y-x)>>(32-1))&(x^y))^y; }
int fastMin(int x, int y) { return (((y-x)>>(32-1))&(x^y))^x; }
const int MAXN=65,MOD=1e9+7,INF=1e18;
const double PI = atan(1.0)*4;
int mod(int x) { return (x % MOD + MOD) % MOD; }
int n,m,r;
vii g[MAXN];
int matric[MAXN][MAXN][MAXN];
int dp[MAXN][MAXN][MAXN];
void floyd(int id){
    for(int k=1;k<=n;k++)for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)matric[id][i][j]=min(matric[id][i][j],matric[id][i][k]+matric[id][k][j]);
}
int MAIN(){
    cin>>n>>m>>r;
    //memset(matric,63,sizeof(matric));
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++)for(int k=1;k<=n;k++)cin>>matric[i][j][k];
        floyd(i);
    }
    for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){
        dp[0][i][j]=INF;
        for(int k=1;k<=m;k++)dp[0][i][j]=min(dp[0][i][j],matric[k][i][j]);
    }
    for(int k=1;k<=n;k++){
        for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){
            dp[k][i][j]=INF;
            for(int mid=i;mid<=n;mid++)dp[k][i][j]=min(dp[k][i][j],dp[k-1][i][mid]+dp[0][mid][j]);
        }
    }
    while(r--){
        int s,t,k;cin>>s>>t>>k;
        if(dp[min(k,n)][s][t]==69)cout<<"-"<<s<<" "<<t<<" "<<k<<endl;
        cout<<dp[min(k,n)][s][t]<<endl;
    }
    return 0;
}
int32_t main(){
    IOS;
    int t=1;
    //cin>>t;
    while(t--)MAIN();
}


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

const int N = 60 + 7;

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

int main() {
	ios::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++)
			for (int k = 0; k < N; k++)
				dp[i][j][k] = 1e9 + 7;
	for (int i = 1; i <= m; i++)
		for (int x = 1; x <= n; x++)
			for (int y = 1; y <= n; y++)
				cin >> go[i][x][y];
   	for (int c = 1; c <= m; c++)
		for (int k = 1; k <= n; k++)
			for (int i = 1; i <= n; i++)
				for (int j = 1; j <= n; j++)
					go[c][i][j] = min(go[c][i][j], go[c][i][k] + go[c][k][j]);
	for (int i = 1; i <= n; i++)
   		for (int j = 1; j <= n; j++) {
	 		for (int c = 1; c <= m; c++)
				dp[i][j][0] = min(dp[i][j][0], go[c][i][j]);
		}
	for (int k = 1; k < 60; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				for (int x = 1; x <= n; x++)
					dp[i][j][k] = min(dp[i][x][0] + dp[x][j][k - 1], dp[i][j][k]);
	while (r--) {
		int a, b, c;
		cin >> a >> b >> c;
		cout << dp[a][b][min(m, c)] << endl;
	}
	return 0;
}


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

const int N = 60 + 7;

int ad[N][N][N], n, m, r, dp[N][N][N], go[N][N][N];

int main() {
	cin >> n >> m >> r;
	for (int i = 0; i <= n; i++)
		for (int j = 0; j <= n; j++)
			for (int k = 0; k < N; k++)
				dp[i][j][k] = 1e9 + 7;
	for (int i = 1; i <= m; i++)
		for (int x = 1; x <= n; x++)
			for (int y = 1; y <= n; y++)
				cin >> go[i][x][y];
	for (int cnt = 1; cnt <= n + 2; cnt++)
	for (int car = 1; car <= m; car++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				for (int x = 1; x <= n; x++)
					go[car][i][j] = min(go[car][i][j], go[car][i][x] + go[car][x][j]);
   	for (int i = 1; i <= n; i++)
   		for (int j = 1; j <= n; j++) {
	 		for (int c = 1; c <= m; c++)
				dp[i][j][0] = min(dp[i][j][0], go[c][i][j]);
//			cout << i << " " << j << " " << dp[i][j][0] << endl;
		}
	for (int k = 1; k < N; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				for (int x = 1; x <= n; x++)
					for (int y = 0; y < k; y++)
						dp[i][j][k] = min(dp[i][x][y] + dp[x][j][k - y - 1], dp[i][j][k]);
	while (r--) {
		int a, b, c;
		cin >> a >> b >> c;
		cout << dp[a][b][c] << endl;
	}
	return 0;
}


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

const int N = 60 + 7;

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

int main() {
	ios::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++)
			for (int k = 0; k < N; k++)
				dp[i][j][k] = 1e9 + 7;
	for (int i = 1; i <= m; i++)
		for (int x = 1; x <= n; x++)
			for (int y = 1; y <= n; y++)
				cin >> go[i][x][y];
   	for (int c = 1; c <= m; c++)
		for (int k = 1; k <= n; k++)
			for (int i = 1; i <= n; i++)
				for (int j = 1; j <= n; j++)
					go[c][i][j] = min(go[c][i][j], go[c][i][k] + go[c][k][j]);
	for (int i = 1; i <= n; i++)
   		for (int j = 1; j <= n; j++) {
	 		for (int c = 1; c <= m; c++)
				dp[i][j][0] = min(dp[i][j][0], go[c][i][j]);
		}
	for (int k = 1; k <= 61; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				for (int x = 1; x <= n; x++)
					dp[i][j][k] = min(dp[i][x][0] + dp[x][j][k - 1], dp[i][j][k]);
	while (r--) {
		int a, b, c;
		cin >> a >> b >> c;
		cout << dp[a][b][min(c, 61)] << endl;
	}
	return 0;
}


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

const int N = 60 + 7;

int ad[N][N][N], n, m, r, dp[N][N][N], g[N][N][N], go[N][N][N];

void djk(int l) {
	for (int i = 1; i <= n; i++) {
		set < pair <int, int> > s;
		s.insert({0, i});
		while (s.size()) {
			int v = s.begin()->second, d = s.begin()->first;
			s.erase(s.begin());
			if (go[l][i][v] >= 0)
				continue;
			go[l][i][v] = d;
			for (int j = 1; j <= n; j++)
				s.insert({d + g[l][v][j], j});
		}
	}
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m >> r;
	for (int i = 1; i <= m; i++)
		for (int x = 1; x <= n; x++)
			for (int y = 1; y <= n; y++)
				go[i][x][y] = -1;
	for (int i = 0; i <= n; i++)
		for (int j = 0; j <= n; j++)
			for (int k = 0; k < N; k++)
				dp[i][j][k] = 1e9 + 7;
	for (int i = 1; i <= m; i++)
		for (int x = 1; x <= n; x++)
			for (int y = 1; y <= n; y++)
				cin >> g[i][x][y];
	for (int i = 1; i <= m; i++)
		djk(i);
   	for (int i = 1; i <= n; i++)
   		for (int j = 1; j <= n; j++) {
	 		for (int c = 1; c <= m; c++)
				dp[i][j][0] = min(dp[i][j][0], go[c][i][j]);
		}
	for (int k = 1; k < 60; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				for (int x = 1; x <= n; x++)
					dp[i][j][k] = min(dp[i][x][0] + dp[x][j][k - 1], dp[i][j][k]);
	while (r--) {
		int a, b, c;
		cin >> a >> b >> c;
		cout << dp[a][b][min(m, c)] << endl;
	}
	return 0;
}


#------------------------------what is this I don't know....just makes my mess faster--------------------------------------
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

#----------------------------------Real game starts here--------------------------------------
'''

___________________THIS IS AESTROIX CODE________________________
                       KARMANYA GUPTA

'''

n , d = list(map(int, input().split()))
for i in range(int(input())):
    x , y = list(map(int, input().split()))
    a = y-x
    b = y+x
    if a < d and a > -d and b > d and b < 2*n - d:
        print("YES")
    elif a == d or a == -d or b == d or b == 2*n - d:
        print("YES")
    else:
        print("NO")


import math

class Vector:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __add__(self, other):
        return Vector(self.x + other.x, self.y + other.y)

    def __neg__(self):
        return Vector(-self.x, -self.y)

    def __sub__(self, other):
        return self + -other

    def __len__(self):
        return math.sqrt(self.x ** 2 + self.y ** 2)

def dot(a, b):
    return a.x * b.x + a.y * b.y

def cross(a, b):
    return a.x * b.y - a.y * b.x

n, d = map(int, input().split())
p1 = (0, d)
p2 = (d, 0)
p3 = (n, n - d)
p4 = (n - d, n)
vec1 = Vector(d, -d)
vec2 = Vector(n - d, n - d)
vec3 = Vector(-d, d)
vec4 = Vector(d - n, d - n)
m = int(input())
for i in range(m):
    base = tuple(map(int, input().split()))
    vec5 = Vector(base[0] - p1[0], base[1] - p1[1])
    if cross(vec1, vec5) < 0:
        print("NO")
        continue
    vec5 = Vector(base[0] - p2[0], base[1] - p2[1])
    if cross(vec2, vec5) < 0:
        print("NO")
        continue
    vec5 = Vector(base[0] - p3[0], base[1] - p3[1])
    if cross(vec3, vec5) < 0:
        print("NO")
        continue
    vec5 = Vector(base[0] - p4[0], base[1] - p4[1])
    if cross(vec4, vec5) < 0:
        print("NO")
        continue
    print('YES')


n,d = map(int,input().split())
m = int(input())
ans = []
for i in range(m):
    x1,y1 = map(int,input().split())
    t1 = max(x1, y1)
    if t1<n:
        if abs(x1-y1)<=d:
            ans.append('YES')
        else:
            ans.append('NO')
    elif t1==n:
        if min(x1,y1)==(n-d):
            ans.append('YES')
        else:
            ans.append('NO')
    else:
        ans.append('NO')
for i in ans:
    print(i)


n,d = map(int,input().split())
m = int(input())
ans = []
for i in range(m):
    x1,y1 = map(int,input().split())
    t1 = x1+y1
    t2 = x1-y1
    if t1>=d and t1<=(2*n-d) and t2>=(-d) and t2<=d:
        ans.append('YES')
    else:
        ans.append('NO')
for i in ans:
    print(i)


n, d = map(int, input().split())
m = int(input())
for i in range(m):
    x, y = map(int, input().split())
    if y >= d - x and y <= x + d and y >= x - d and y <= -x - d + 2 * n:
        print("YES")
    else:
        print("NO")


n, d = map(int, input().split())
def k(x, y):
    if y >= x - d and y <= x + d and y >= -x + d and y <= -x + 2 * n - d:
        return 1
    else:
        return 0
m = int(input())
for i in range(m):
    x, y = map(int, input().split())
    if k(x, y):
        print("YES")
    else:
        print("NO")


n,d = map(int,input().split())
m = int(input())
a = []
for i in range(m):
    x1,y1 = map(int,input().split())
    t1 = x1+y1
    t2 = x1-y1
    if t1>=d and t1<=(2*n-d) and t2>=(-d) and t2<=d:
        a.append('YES')
    else:
        a.append('NO')
for j in a:
    print(j)


import sys
import string
import math
from collections import defaultdict
from functools import lru_cache
from collections import Counter
from fractions import Fraction

def mi(s):
    return map(int, s.strip().split())

def lmi(s):
    return list(mi(s))

def tmi(s):
    return tuple(mi(s))

def mf(f, s):
    return map(f, s)

def lmf(f, s):
    return list(mf(f, s))

def js(lst):
    return " ".join(str(d) for d in lst)

def jsns(lst):
    return "".join(str(d) for d in lst)

def line():
    return sys.stdin.readline().strip()

def linesp():
    return line().split()

def iline():
    return int(line())

def mat(n):
    matr = []
    for _ in range(n):
        matr.append(linesp())
    return matr

def matns(n):
    mat = []
    for _ in range(n):
        mat.append([c for c in line()])
    return mat

def mati(n):
    mat = []
    for _ in range(n):
       mat.append(lmi(line()))
    return mat

def pmat(mat):
    for row in mat:
        print(js(row))

def rotate(d, x, y):
    root2 = 2**0.5
    x1 = x - d
    return (x1/root2 + y/root2 + d, -x1/root2 + y/root2)

def main():
    n, d = mi(line())
    m = iline()
    points = []
    for _ in range(m):
        points.append(tmi(line()))

    root2 = 2**0.5
    inside = lambda x, y: x>=d and x<=(root2*(n-d)+d) and y>=0 and y<=root2*d

    rotated_points = []
    for x, y in points:
        rotated_points.append(rotate(d, x, y))

    for x, y in rotated_points:
        if inside(x, y):
            print("YES")
        else:
            print("NO")
main()


import sys
import string
import math
from collections import defaultdict
from functools import lru_cache
from collections import Counter
from fractions import Fraction

def mi(s):
    return map(int, s.strip().split())

def lmi(s):
    return list(mi(s))

def tmi(s):
    return tuple(mi(s))

def mf(f, s):
    return map(f, s)

def lmf(f, s):
    return list(mf(f, s))

def js(lst):
    return " ".join(str(d) for d in lst)

def jsns(lst):
    return "".join(str(d) for d in lst)

def line():
    return sys.stdin.readline().strip()

def linesp():
    return line().split()

def iline():
    return int(line())

def mat(n):
    matr = []
    for _ in range(n):
        matr.append(linesp())
    return matr

def matns(n):
    mat = []
    for _ in range(n):
        mat.append([c for c in line()])
    return mat

def mati(n):
    mat = []
    for _ in range(n):
       mat.append(lmi(line()))
    return mat

def pmat(mat):
    for row in mat:
        print(js(row))

def rotate(d, x, y):
    root2 = 2**0.5
    x1 = x - d
    return (round(x1/root2 + y/root2 + d, 10), round(-x1/root2 + y/root2, 10))

def main():
    n, d = mi(line())
    m = iline()
    points = []
    for _ in range(m):
        points.append(tmi(line()))

    root2 = 2**0.5
    inside = lambda x, y: x>=d and x<=round((root2*(n-d)+d), 10) and y>=0 and y<=round(root2*d,10)

    rotated_points = []
    for x, y in points:
        rotated_points.append(rotate(d, x, y))

    for x, y in rotated_points:
        # print(x, y)
        if inside(x, y):
            print("YES")
        else:
            print("NO")
main()


n, d = map(int,input().split())   # co-ordinates of corn field
m = int(input())   # no of grasshopper
for i in range(m):
	a,b = map(int,input().split())
	eqn1 = a+b-d
	eqn2 = -a+b-d
	eqn3 = a+b-2*n+d
	eqn4 = -a+b+d

	if eqn1 >= 0 and eqn2 <= 0 and eqn3 <= 0 and eqn4 >= 0 :
		print("YES")
	else :
		print("NO")


n, d = map(int,input().split())   # co-ordinates of corn field
m = int(input())   # no of grasshopper
for i in range(m):
	a,b = map(int,input().split())
	eqn1 = a+b+d
	eqn2 = -a+b-d
	eqn3 = a+b-2*n+d
	eqn4 = -a+b+d

	if eqn1 >= 0 and eqn2 <= 0 and eqn3 <= 0 and eqn4 >= 0 :
		print("YES")
	else :
		print("NO")


def check(n, d, x, y):
    if (y >= -x + d) and (y <= 2 * n - d - x) and (y <= d + x) and (y >= x - d):
        return True
    return False

n, d = map(int, input().split())
m = int(input())
ans = []
for i in range(m):
    x, y = map(int, input().split())
    if check(n, d, x, y):
        ans.append('YES')
    else:
        ans.append('NO')
for i in range(m):
    print(ans[i])


def rs(): return input().strip()
def ri(): return int(input())
def ria(): return list(map(int, input().split()))
def ia_to_s(a): return ' '.join([str(s) for s in a])

def solve(n, d, x, y):
    # \ 0 = -x - y + d
    # \\ 0 -x - y + d + 2 * (n-d)
    # / 0 = x - y - d
    # // 0 = x - y + d
    return y >= -x+d and y <= -x + 2*n-d and y <= x + d and y >= x - d

def main():
        n, d = ria()
        m = ri()
        for i in range(m):
            x, y = ria()
            print('YES' if solve(n, d, x, y) else 'NO')

if __name__ == '__main__':
    main()


def rs(): return input().strip()
def ri(): return int(input())
def ria(): return list(map(int, input().split()))
def ia_to_s(a): return ' '.join([str(s) for s in a])

def solve(n, d, x, y):
    # \ 0 = -x - y + d
    # \\ 0 -x - y + d + 2 * (n-d)
    # / 0 = x - y - d
    # // 0 = x - y + d
    return y >= -x+d and y <= -x + 2*(n-d) and y <= x + d and y >= x - d

def main():
        n, d = ria()
        m = ri()
        for i in range(m):
            x, y = ria()
            print('YES' if solve(n, d, x, y) else 'NO')

if __name__ == '__main__':
    main()


import math as mt
import sys,string,bisect
input=sys.stdin.readline
from collections import deque,defaultdict
L=lambda : list(map(int,input().split()))
Ls=lambda : list(input().split())
M=lambda : map(int,input().split())
I=lambda :int(input())
def dist(x,y,c,d):
    return mt.sqrt((x-c)**2+(y-d)**2)

n,d=M()
m=I()
for i in range(m):
    a,b=M()
    if(a-b-d<=0 and a-b+d>=0):
        if(a+b-d>=0 and a+b+d-2*n<=0):
            print("YES")
        else:
            print("NO")
    else:
        print("NO")


n, d = [int(s) for s in input().split(' ')]
m = int(input())
for g in range(m):
    x, y = [int(s) for s in input().split(' ')]
    if d <= x + y <= 2 * n - d and - d <= y - x <= d:
        print('YES')
    else:
        print('NO')


n, d = map(int, input().split())
m = int(input())
kuz1 = map(int, input().split())
kuz2 = map(int, input().split())
kuz3 = map(int, input().split())
kuz4 = map(int, input().split())
arr = [kuz1, kuz2, kuz3, kuz4]
for (x, y) in arr:
    if x < d and y < d and y < d - x:
        print("NO")
        continue
    if x < n - d and y > d and y > x + d:
        print("NO")
        continue
    if x > n - d and y > n - d and y > 2 * n - d - x:
        print("NO")
        continue
    if x > d and y < n - d and y < x - d:
        print("NO")
        continue
    print("YES")


n, d = map(int, input().split())
m = int(input())
arr = [list(map(int, input().split())) for _ in range(m)]
for (x, y) in arr:
    if x < d and y < d and y < d - x:
        print("NO")
        continue
    if x < n - d and y > d and y > x + d:
        print("NO")
        continue
    if x > n - d and y > n - d and y > 2 * n - d - x:
        print("NO")
        continue
    if x > d and y < n - d and y < x - d:
        print("NO")
        continue
    print("YES")


h = input().split(' ')
l = list(h)
k = False
n = int(l[0])
d = int(l[1])
m = int(input())
for i in range(m):
    t = input().split(' ')
    l = list(t)
    x = int(l[0])
    y = int(l[1])
    if ((d <= x + y) & (x + y <= 2*n-d) & (d*(-1) <= x - y) & (x-y <= d)):
        print('YES')
    else:
        print('NO')


h = input().split(' ')
l = list(h)
print(l)
k = False
n = int(l[0])
d = int(l[1])
m = int(input())
for i in range(m):
    t = input().split(' ')
    l = list(t)
    x = int(l[0])
    y = int(l[1])
    if ((d <= x + y) & (x + y <= 2*n-d) & (d*(-1) <= x - y) & (x-y <= d)):
        print('YES')
    else:
        print('NO')


#1030B
[n,d] = list(map(int,input().split()))
m = int(input())
for i in range(m):
    [x,y] = list(map(int,input().split()))
    if y <= x+d and y >= -x+d and y >= x-d and y <= -x +2*n-d:
        print('YES')
    else:
        print('NO')


#1030B
[n,d] = list(map(int,input().split()))
m = int(input())
for i in range(m):
    [x,y] = list(map(int,input().split()))
    if y <= x+d and y >= -x +2 and y >= x-d and y <= -x +2*n-d:
        print('YES')
    else:
        print('NO')


n, d = map(int, input().split())
m = int(input())
for i in range(m):
    x, y = map(int, input().split())
    if (abs(y - x) <= d and x + y >= d and x + y <= 2 * n - d):
        print("YES")
    else:
        print("NO")