#define rep(i,a,b) for(int i = a; i <= b; i++)
#include<bits/stdc++.h>

#define ll long long
#define inf 1e15
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;

const int N=2e6+10;
const int mod=998244353;

int head[N];
int dis[N],ecnt;
int fa[N];

int cat[2005][2005];

ll gcd(ll a,ll b)
{
    return a%b==0?b:gcd(b,a%b);
}

ll qpow(ll base ,ll n)
{
    ll ans=1;
    while(n)
    {
        if(n&1)
            ans=(ans*base)%mod;
        n>>=1;
        base=base*base%mod;
    }
    return ans;
}

struct Node
{
    ll x,y;

}nde[N];

struct EDGE
{
    int u,v,nxt;
    ll w;
}e[N];

bool cmp(EDGE a,EDGE b)
{
    return a.w<b.w;
}

void add_edge(int u,int v,ll w)
{
    e[ecnt].u=u;
    e[ecnt].v=v;
    e[ecnt].w=w;
    e[ecnt].nxt=head[u];
    head[u]=ecnt++;
}

int fd(int x)
{
    return -1 == fa[x] ? x : fa[x] = fd(fa[x]);
}

ll  m,n,x,k,a,b;

int vis[N];
int p[N],s[N];
int ct[N];
ll ans;

int C[55][55];
int dp[150][150];

int ar[N],br[N];

struct H
{
    int p,s;
    friend bool operator <(H a,H b)
    {
        return a.p<b.p;
    }

}he[N];

bool cmp(H a,H b)
{
    return a.p<b.p;
}

 priority_queue<int> q;
 vector< int >v;
 map<pair<int,int>,int>mp;

vector<int>hs;
char farm[150][150];

int main()
{

   // while(~scanf("%d",&n))
    //int T;cin>>T;while(T--)
    {
        ll ans=inf;
        int sm=0;
       cin>>n;
       rep(i,1,n)
       {
           cin>>ar[i];
           sm+=ar[i];

           if(ar[i])
           {   br[sm]=i;  hs.push_back(i);}
       }
       if(sm<=1)
       {
           cout<<-1<<endl;
           return 0;
       }

       for(int i=2;i<=sm;i++)//每一部分有i个数
       {
           if(sm%i==0)
            {
                if(!vis[i])
                v.push_back(i);
                vis[i]=1;
            }
       }
        for(auto it:v)
        {
            int d=it;
            ll t=0;
       for(int i=0;i<hs.size()-1;i++)
       {
           int iit=hs[i];
           if(ar[iit]%d<=d-(ar[iit]%d)  )
           {
               t+=(ar[iit]%d) *(hs[i+1]-iit);
               //ar[hs[i+1]]+=ar[iit]%d;
              // printf("op1\n");
           }
           else
           {
               //printf("op2\n");
               t+=(d-(ar[iit]%d))*(hs[i+1]-iit);
              // ar[hs[i+1]]-=d-(ar[iit]%d);
           }
       }
       ans=min(ans,t);
        }
       cout<<ans<<endl;

    }

    return 0;
}


#define rep(i,a,b) for(int i = a; i <= b; i++)
#include<bits/stdc++.h>

#define ll long long
#define inf 1e15
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;

const int N=2e6+10;
const int mod=998244353;

int head[N];
int dis[N],ecnt;
int fa[N];

int cat[2005][2005];

ll gcd(ll a,ll b)
{
    return a%b==0?b:gcd(b,a%b);
}

ll qpow(ll base ,ll n)
{
    ll ans=1;
    while(n)
    {
        if(n&1)
            ans=(ans*base)%mod;
        n>>=1;
        base=base*base%mod;
    }
    return ans;
}

struct Node
{
    ll x,y;

}nde[N];

struct EDGE
{
    int u,v,nxt;
    ll w;
}e[N];

bool cmp(EDGE a,EDGE b)
{
    return a.w<b.w;
}

void add_edge(int u,int v,ll w)
{
    e[ecnt].u=u;
    e[ecnt].v=v;
    e[ecnt].w=w;
    e[ecnt].nxt=head[u];
    head[u]=ecnt++;
}

int fd(int x)
{
    return -1 == fa[x] ? x : fa[x] = fd(fa[x]);
}

ll  m,n,x,k,a,b;

int vis[N];
int p[N],s[N];
int ct[N];
ll ans;

int C[55][55];
int dp[150][150];

int ar[N],br[N];

struct H
{
    int p,s;
    friend bool operator <(H a,H b)
    {
        return a.p<b.p;
    }

}he[N];

bool cmp(H a,H b)
{
    return a.p<b.p;
}

 priority_queue<int> q;
 vector< int >v;
 map<pair<int,int>,int>mp;

vector<int>hs;
char farm[150][150];

int main()
{

   // while(~scanf("%d",&n))
    //int T;cin>>T;while(T--)
    {
        ll ans=inf;
        int sm=0;
       cin>>n;
       rep(i,1,n)
       {
           cin>>ar[i];
           sm+=ar[i];

           if(ar[i])
           {   br[sm]=i;  hs.push_back(i);}
       }
       if(sm<=1)
       {
           cout<<-1<<endl;
           return 0;
       }

       for(int i=2;i<=sm;i++)//每一部分有i个数
       {
           if(sm%i==0)
            {
                if(!vis[i])
                v.push_back(i);
                vis[i]=1;
            }
       }
        for(auto it:v)
        {
            int d=it;
            ll t=0;
       for(int i=0;i<hs.size()-1;i++)
       {
           int iit=hs[i];
           if(ar[iit]%d<=d-(ar[iit]%d) || ar[hs[i+1]]<d-(ar[iit]%d) )
           {
               t+=(ar[iit]%d) *(hs[i+1]-iit);
               ar[hs[i+1]]+=ar[iit]%d;
              // printf("op1\n");
           }
           else
           {
               //printf("op2\n");
               t+=(d-(ar[iit]%d))*(hs[i+1]-iit);
               ar[hs[i+1]]-=d-(ar[iit]%d);
           }
       }
       ans=min(ans,t);
        }
       cout<<ans<<endl;

    }

    return 0;
}


#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <bitset>
/*  Data Structures */
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>

#define int long long
#define double long double

using namespace std;

int n;
vector<int> arr;

#undef int
int main(int argc, char* argv[]) {
#define int long long
    ios::sync_with_stdio(false);
    cin >> n;
    arr.resize(n+1);
    int a;
    int sum = 0;
    for (int i=1; i<=n; ++i) {
        cin >> a;
        arr[i] = a;
        sum += a;
    }
    int result = LLONG_MAX;
    // find factorization
    for (int i=2; i*i <= sum; ++i) {
        if (sum % i == 0) {
            int curr = 0;
            int curr_result = 0;
            for (int j=1; j<=n; ++j) {
                curr += arr[j];
                curr %= i;
                // check whether to remove or add
                curr_result += min(curr, i - curr);
            }
            result = min(result, curr_result);
            // divide by current divisor
            while (sum % i == 0) {
                sum /= i;
            }
        }
    }
    // try itself
    int curr = 0;
    int curr_result = 0;
    for (int j=1; j<=n; ++j) {
        curr += arr[j];
        curr %= sum;
        curr_result += min(curr, sum - curr);
    }
    result = min(result, curr_result);
    if (result == LLONG_MAX || sum == 1) {
        cout << "-1" << endl;
    } else {
        cout << result << endl;
    }

    return 0;
}


#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <bitset>
/*  Data Structures */
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>

#define int long long
#define double long double

using namespace std;

int n;
vector<int> arr;

#undef int
int main(int argc, char* argv[]) {
#define int long long
    ios::sync_with_stdio(false);
    cin >> n;
    arr.resize(n+1);
    int a;
    int sum = 0;
    for (int i=1; i<=n; ++i) {
        cin >> a;
        arr[i] = a;
        sum += a;
    }
    int result = LLONG_MAX;
    int original_sum = sum;
    // find factorization
    for (int i=2; i*i <= sum; ++i) {
        if (sum % i == 0) {
            int curr = 0;
            int curr_result = 0;
            for (int j=1; j<=n; ++j) {
                curr += arr[j];
                curr %= i;
                // check whether to remove or add
                curr_result += min(curr, i - curr);
            }
            result = min(result, curr_result);
            // divide by current divisor
            while (sum % i == 0) {
                sum /= i;
            }
        }
    }
    // try itself
    int curr = 0;
    int curr_result = 0;
    for (int j=1; j<=n; ++j) {
        curr += arr[j];
        curr %= original_sum;
        curr_result += min(curr, original_sum - curr);
    }
    result = min(result, curr_result);
    if (result == LLONG_MAX || original_sum == 1) {
        cout << "-1" << endl;
    } else {
        cout << result << endl;
    }

    return 0;
}


#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <bitset>
/*  Data Structures */
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>

#define int long long
#define double long double

using namespace std;

int n;
vector<int> arr;

#undef int
int main(int argc, char* argv[]) {
#define int long long
    ios::sync_with_stdio(false);
    cin >> n;
    arr.resize(n+1);
    int a;
    int sum = 0;
    for (int i=1; i<=n; ++i) {
        cin >> a;
        arr[i] = a;
        sum += a;
    }
    int result = LLONG_MAX;
    int original_sum = sum;
    // cout << "sum: " << sum << endl;
    // find factorization
    for (int i=2; i*i <= sum; ++i) {
        // cout << "i: " << i << endl;
        if (sum % i == 0) {
            int curr = 0;
            int curr_result = 0;
            for (int j=1; j<=n; ++j) {
                curr += arr[j];
                curr %= i;
                // check whether to remove or add
                curr_result += min(curr, i - curr);
            }

            result = min(result, curr_result);
            // divide by current divisor
            while (sum % i == 0) {
                sum /= i;
            }
        }
    }
    // try itself
    if (sum > 1) {
        int curr = 0;
        int curr_result = 0;
        for (int j=1; j<=n; ++j) {
            curr += arr[j];
            curr %= sum;
            curr_result += min(curr, sum - curr);
        }
        result = min(result, curr_result);
    }
    if (result == LLONG_MAX || original_sum == 1) {
        cout << "-1" << endl;
    } else {
        cout << result << endl;
    }

    return 0;
}


#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define DEBUG true
#ifdef ONLINE_JUDGE
#undef DEBUG
#define DEBUG false
#endif

using namespace std;

#define MAXN ((int)60+5)
#define MOD ((int)1e9 + 7)
#define INF ((int)1e9 + 9)
#define ll long long
#define _ << " " <<
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define TRACE(x) if(DEBUG) cerr << #x << " = " << x << endl;
#define TRACE2(x,y) if(DEBUG) cerr << #x << " = " << x << " | " << #y << " = " << y << endl;
#define LOG(x) if(DEBUG) cerr << x << endl;
#define pb push_back
#define all(x) x.begin(), x.end()
#define endl "\n"
#define pii pair<int,int>
#define mid ((l+r)/2)

int n,car,r;
int d[MAXN][MAXN][MAXN];

vector <pii> v[MAXN];

int best[MAXN][MAXN][MAXN];;

void precalc()
{
	for (int i = 0; i < MAXN; ++i)
		for (int j = 0; j < MAXN; ++j)
			for (int k = 0; k < MAXN; ++k)
				best[i][j][k] = INF;

	for (int t = 0; t < car; ++t)
		for (int k = 1; k <= n; ++k)
			for (int i = 1; i <= n; ++i)
				for (int j = 1; j <= n; ++j)
					d[t][i][j] = min(d[t][i][j], d[t][i][k] + d[t][k][j]);

	for (int t = 0; t < car; ++t)
		for (int i = 1; i <= n; ++i)
			for (int j = 1; j <= n; ++j)
				best[0][i][j] = min(best[0][i][j], d[t][i][j]);
	// k change
	for (int change = 1; change <= n; ++change)
		for (int k = 1; k <= n; ++k)
			for (int i = 1; i <= n; ++i)
				for (int j = 1; j <= n; ++j) // make the change
					best[change][i][j] = min(
						best[change][i][j],
						best[change-1][i][k] + best[0][k][j]
						);

}

void solve()
{
	int st,fin,k;
	cin >> st >> fin >> k;

	int ans = INF;

	for (int i = 0; i <= min(k,n); ++i)
	{
		ans = min(ans,
			best[i][st][fin]
			);
	}

	cout << ans << endl;
}

int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);
	cin >> n >> car >> r;
	for (int t = 0; t < car; ++t)
		for (int i = 1; i <= n; ++i)
			for (int j = 1; j <= n; ++j)
				cin >> d[t][i][j];
	precalc();
	while(r--)
		solve();

}


#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define DEBUG true
#ifdef ONLINE_JUDGE
#undef DEBUG
#define DEBUG false
#endif

using namespace std;

#define MAXN ((int)60+5)
#define MOD ((int)1e9 + 7)
#define INF ((int)1e9 + 9)
#define ll long long
#define _ << " " <<
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define TRACE(x) if(DEBUG) cerr << #x << " = " << x << endl;
#define TRACE2(x,y) if(DEBUG) cerr << #x << " = " << x << " | " << #y << " = " << y << endl;
#define LOG(x) if(DEBUG) cerr << x << endl;
#define pb push_back
#define all(x) x.begin(), x.end()
#define endl "\n"
#define pii pair<int,int>
#define mid ((l+r)/2)

int n,car,r;
int d[MAXN][MAXN][MAXN];

vector <pii> v[MAXN];

int best[MAXN][MAXN][MAXN];;

void precalc()
{
	for (int i = 0; i < MAXN; ++i)
		for (int j = 0; j < MAXN; ++j)
			for (int k = 0; k < MAXN; ++k)
				best[i][j][k] = INF;

	for (int t = 0; t < car; ++t)
		for (int k = 1; k <= n; ++k)
			for (int i = 1; i <= n; ++i)
				for (int j = 1; j <= n; ++j)
					d[t][i][j] = min(d[t][i][j], d[t][i][k] + d[t][k][j]);

	for (int t = 0; t < car; ++t)
		for (int i = 1; i <= n; ++i)
			for (int j = 1; j <= n; ++j)
				best[0][i][j] = min(best[0][i][j], d[t][i][j]);
	// k change
	for (int change = 1; change <= n; ++change)
		for (int k = 1; k <= n; ++k)
			for (int i = 1; i <= n; ++i)
				for (int j = 1; j <= n; ++j) // make the change
					best[change][i][j] = min(
						best[change][i][j],
						best[change-1][i][k] + best[0][k][j]
						);

}

void solve()
{
	int st,fin,k;
	cin >> st >> fin >> k;

	cout << best[k][st][fin] << endl;
}

int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);
	cin >> n >> car >> r;
	for (int t = 0; t < car; ++t)
		for (int i = 1; i <= n; ++i)
			for (int j = 1; j <= n; ++j)
				cin >> d[t][i][j];
	precalc();
	while(r--)
		solve();

}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define gcd __gcd
#define lcm(a, b) (a*b)/gcd(a, b) //credit to DMK
#define pcount __builtin_popcount
#define nxtperm next_permutation
#define int ll
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4.2")
const ll mod = 1000000007;
const double PI = acos(-1.);
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>,rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
int binpow(ll a, ll n) { if(n == 0) return 1; if(n == 1) return a; ll x = binpow(a, n/2); if(n%2 == 0) return x*x; return x*x*a; }
int n, m, q, w[65][65][65]; // weight i -> j using car k
int dp[65][65][65], giap[65][65];
signed main()
{
//	freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
	cin >> n >> m >> q;
	for(int k=0;k<m;k++)
	{
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				cin >> w[i][j][k];
			}
		}
	}

	for(int k=0;k<=60;k++) for(int i=0;i<n;i++) for(int j=0;j<n;j++) dp[k][i][j] = 1e18;
 	for(int car=0;car<m;car++)
 	{
 		for(int i=0;i<n;i++) for(int j=0;j<n;j++) giap[i][j] = w[i][j][car];
 		for(int k=0;k<n;k++)
 		{
 			for(int i=0;i<n;i++)
			{
				for(int j=0;j<n;j++)
				{
					giap[i][j] = min(giap[i][j], giap[i][k] + giap[k][j]);
				}
			}
		}
		for(int i=0;i<n;i++) for(int j=0;j<n;j++)
		dp[0][i][j] = min(dp[0][i][j], giap[i][j]);
	}
//	cout << "desu\n";
//	for(int i=0;i<n;i++)
//	{
//		for(int j=0;j<n;j++)
//		{
//			cout << dp[0][i][j] << ' ' ;
//		}
//		cout << '\n';
//	}
	for(int t=1;t<=60;t++)
	{
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				for(int s=0;s<n;s++)
				{
					dp[t][i][j] = min(dp[t][i][j], dp[t-1][i][s] + dp[0][s][j]);
				}
			}
		}
	}
	for(int i=0, s, d, k;i<q;i++)
	{
		cin >> s >> d >> k ;
		cout << dp[min(60LL, k)][s-1][d-1] << '\n';
	}
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=70;
int cost[maxn][maxn][maxn],dp[maxn*20][maxn][maxn];
const int inf=0x3f3f3f3f;
int main()
{
    int n,r,x,y,k,m;
    scanf("%d%d%d",&n,&m,&r);
    for(int i=1;i<=m;i++)
    {
        for(int j=1;j<=n;j++)
            for(k=1;k<=n;k++)scanf("%d",&cost[i][j][k]);
        for(k=1;k<=n;k++)
            for(x=1;x<=n;x++)
                for(y=1;y<=n;y++)cost[i][x][y]=min(cost[i][x][y],cost[i][x][k]+cost[i][k][y]);
    }
    for(int i=0;i<=1001;i++)
        for(int j=1;j<=n;j++)
            for(k=1;k<=n;k++)dp[i][j][k]=inf;

    for(int i=1;i<=m;i++)
        for(k=1;k<=n;k++)
            for(x=1;x<=n;x++)
                for(y=1;y<=n;y++)
                    dp[0][x][y]=min(dp[0][x][y],cost[i][x][k]+cost[i][k][y]);
    for(int i=1;i<n;i++)
        for(k=1;k<=n;k++)
            for(x=1;x<=n;x++)
                for(y=1;y<=n;y++)
                    dp[i][x][y]=min(dp[i][x][y],dp[i-1][x][k]+dp[0][k][y]);
    for(int i=1;i<=r;i++)
    {
        scanf("%d%d%d",&x,&y,&k);
        int ans=inf;
        k=min(n,k);
        for(int i=0;i<=k;i++)ans=min(ans,dp[i][x][y]);
        printf("%d\n",ans);
    }
    return 0;
}


# include<bits/stdc++.h>

using namespace std;

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

# define S second
# define F first
# define kill(x) return(cout << x << endl, 0)
# define debug(x) cerr<< #x << " : " << x << endl
# define ddebug(x, y) cerr<< #x << " : " << x  << ", " << #y << " : " << y << endl
# define sz(s) (int)s.size()
# define sq(x) (x) * (x)
# define PB(x) push_back(x)
# define smax(x, y) (x) = max((x), (y))
# define smin(x, y) (x) = min((x), (y))
# define all(x) x.begin(), x.end()
# define SP fixed << setprecision(10)
# define UB upper_bound

const int MAXN = 70, INF = 1e9;
int arr[MAXN][MAXN][MAXN],
    minp[MAXN][MAXN],
    dp[MAXN][MAXN][MAXN];

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

	int n, m, r;
	cin >> n >> m >> r;
	for (int x = 0; x < m; x ++)
		for (int i = 0; i < n; i ++)
			for (int j = 0; j < n; j ++)
				cin >> arr[x][i][j];

	for (int x = 0; x < m; x ++)
		for (int k = 0; k < n; k ++)
			for (int i = 0; i < n; i ++)
				for (int j = 0; j < n; j ++)
					smin(arr[x][i][j], arr[x][i][k] + arr[x][k][j]);

	for (int i = 0; i < n; i ++)
		for (int j = 0; j < n; j ++)
		{
			minp[i][j] = INF;
			for (int x = 0; x < m; x ++)
				smin(minp[i][j], arr[x][i][j]);
			dp[i][j][0] = minp[i][j];
		}

	for (int k = 1; k <= n; k ++)
		for (int s = 0; s < n; s ++)
			for (int t = 0; t < n; t ++)
			{
				dp[s][t][k] = INT_MAX;
				for (int v = 0; v < n; v ++)
					smin(dp[s][t][k], minp[s][v] + dp[v][t][k - 1]);
			}

	while (r --)
	{
		int s, t, k;
		cin >> s >> t >> k;
		cout << dp[s - 1][t - 1][min(k, n - 2)] << "\n";
	}

	return 0;
}


# include<bits/stdc++.h>

using namespace std;

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

# define S second
# define F first
# define kill(x) return(cout << x << endl, 0)
# define debug(x) cerr<< #x << " : " << x << endl
# define ddebug(x, y) cerr<< #x << " : " << x  << ", " << #y << " : " << y << endl
# define sz(s) (int)s.size()
# define sq(x) (x) * (x)
# define PB(x) push_back(x)
# define smax(x, y) (x) = max((x), (y))
# define smin(x, y) (x) = min((x), (y))
# define all(x) x.begin(), x.end()
# define SP fixed << setprecision(10)
# define UB upper_bound

const int MAXN = 70, INF = 1e9;
int arr[MAXN][MAXN][MAXN],
    minp[MAXN][MAXN],
    dp[MAXN][MAXN][MAXN];

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

	int n, m, r;
	cin >> n >> m >> r;
	for (int x = 0; x < m; x ++)
		for (int i = 0; i < n; i ++)
			for (int j = 0; j < n; j ++)
				cin >> arr[x][i][j];

	for (int x = 0; x < m; x ++)
		for (int k = 0; k < n; k ++)
			for (int i = 0; i < n; i ++)
				for (int j = 0; j < n; j ++)
					smin(arr[x][i][j], arr[x][i][k] + arr[x][k][j]);

	for (int i = 0; i < n; i ++)
		for (int j = 0; j < n; j ++)
		{
			minp[i][j] = INF;
			for (int x = 0; x < m; x ++)
				smin(minp[i][j], arr[x][i][j]);
			dp[i][j][0] = minp[i][j];
		}

	for (int k = 1; k <= m; k ++)
		for (int s = 0; s < n; s ++)
			for (int t = 0; t < n; t ++)
			{
				dp[s][t][k] = INT_MAX;
				for (int v = 0; v < n; v ++)
					smin(dp[s][t][k], minp[s][v] + dp[v][t][k - 1]);
			}

	while (r --)
	{
		int s, t, k;
		cin >> s >> t >> k;
		cout << dp[s - 1][t - 1][min(k, n - 2)] << "\n";
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,r,s,t,k,vis[61][61][61][61],dp[61][61][65][61],G[61][61][61];
int solve(int i,int j,int k,int prv){
	if(k<0) return 1e8;
	if(i==j) return 0;
	if(dp[i][j][k][prv]!=-1) return dp[i][j][k][prv];
	if(vis[i][j][k][prv]) return 1e8;
	vis[i][j][k][prv]=1;
	int op1=1e8,op2=1e8;
	for(int nw=1;nw<=n;++nw){
		op1=min(op1,solve(nw,j,k,prv)+G[prv][i][nw]);
		for(int car=1;car<=m;++car){
			if(car==prv) continue;
			op2=min(op2,solve(nw,j,k-1,car)+G[car][i][nw]);
		}
	}
	return dp[i][j][k][prv]=min(op1,op2);
}
main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    memset(dp,-1,sizeof(dp));
    cin>>n>>m>>r;
    int change;
    for(int i=1;i<=m;++i) for(int j=1;j<=n;++j) for(int k=1;k<=n;++k) cin>>G[i][j][k];
    for(int i=1;i<=r;++i){
    	cin>>s>>t>>change;
    	int ans=1e8;
    	for(int c=1;c<=m;++c) ans=min(ans,solve(s,t,min(n+2,change),c));
    	cout<<ans<<'\n';
    }
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>

using namespace std;

#define R register
#define LL long long
const int N = 60 + 5;

int read() {
	int x = 0, f = 1; char a = getchar();
	for(; a > '9' || a < '0'; a = getchar()) if(a == '-') f = -1;
	for(; a >= '0' && a <= '9'; a = getchar()) x = x * 10 + a - '0';
	return x * f;
}

int n, m, q;
int g[N][N][N], f[N][N][N];

int main() {
	#ifdef IN
	//freopen(".in", "r", stdin);
	//freopen(".out", "w", stdout);
	#endif
	n = read(); m = read(); q = read();
	memset(g, 0x3f, sizeof(g));
	memset(f, 0x3f, sizeof(f));
	for(R int p = 1; p <= m; p ++) {
		for(R int i = 1; i <= n; i ++)
			for(R int j = 1; j <= n; j ++) g[p][i][j] = read();
	}
	for(R int p = 1; p <= m; p ++) {
		for(R int k = 1; k <= n; k ++)
			for(R int i = 1; i <= n; i ++)
				for(R int j = 1; j <= n; j ++)
					g[p][i][j] = min(g[p][i][j], g[p][i][k] + g[p][k][j]);
	}
	for(R int p = 1; p <= m; p ++)
		for(R int i = 1; i <= n; i ++)
			for(R int j = 1; j <= n; j ++)
				f[0][i][j] = min(f[0][i][j], g[p][i][j]);
	for(R int i = 1; i <= n; i ++) f[0][i][i] = 0;
	for(R int p = 1; p <= 60; p ++) {
		for(R int k = 1; k <= n; k ++)
			for(R int i = 1; i <= n; i ++)
				for(R int j = 1; j <= n; j ++)
					f[p][i][j] = min(f[p][i][j], f[p - 1][i][k] + f[0][k][j]);
	}
	while(q --) {
		int s = read(), t = read(), k = min(read(), 60);
		printf("%d\n", f[k][s][t]);
	}
	return 0;
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//typedef tree < int , null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update> TREE;
#define ll long long
#define ull unsigned long long int
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define pii pair<int,int>
#define pss pair<short,short>
#define pld pair<long double,long double >
#define ld long double
#define piii  pair<pii,int>
#define vii vector<pair<int,int> >
#define st first
#define nd second
#define pll pair<ll,ll>
#define speed ios::sync_with_stdio(false);cin.tie();cout.tie();
#define M_PI 3.14159265358979323846
//#define int long long
const int mod=1000000007;
//const int mod=1009;
//const int mod=998244353;
const int inf=1000000009;
const long long INF=1000000000000000009;
const long long big=1000000000000000;
const long double eps=0.000000000000000000001;
using namespace std;
int D[65][65][65],DP[65][65][65],W[65][65];
int main()
{
    speed
    int n,m,r;
    cin>>n>>m>>r;
    for(int i=1; i<=m; i++)
        for(int j=1; j<=n; j++)
            for(int k=1; k<=n; k++)
                cin>>D[i][j][k];
    for(int i=1; i<=m; i++)
        for(int j=1; j<=n; j++)
            for(int k=1; k<=n; k++)
                for(int l=1; l<=n; l++)
                    D[i][j][l]=min(D[i][j][l],D[i][j][k]+D[i][k][l]);
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
        {
            W[i][j]=inf;
            for(int k=1; k<=m; k++)
                W[i][j]=min(W[i][j],D[k][i][j]);
            //cout<<i<<" "<<j<<" "<<W[i][j]<<endl;
        }

    for(int j=1; j<=n; j++)
        for(int k=1; k<=n; k++)
        {
            DP[j][k][0]=W[j][k];
            for(int l=1; l<=n; l++)
                DP[j][k][l]=inf;
        }

    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            for(int k=1; k<=n; k++)
                for(int l=1; l<=n; l++)
                {
                    DP[j][l][i]=min(DP[j][l][i],DP[j][k][i-1]+W[k][l]);
                    //cout<<i<<" "<<j<<" "<<k<<" "<<l<<" "<<DP[j][l][i]<<endl;
                }

    for(int i=1; i<=r; i++)
    {
        int a,b,c;
        cin>>a>>b>>c;
        c=min(c,n);
        cout<<DP[a][b][c]<<"\n";
    }
    return 0;
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//typedef tree < int , null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update> TREE;
#define ll long long
#define ull unsigned long long int
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define pii pair<int,int>
#define pss pair<short,short>
#define pld pair<long double,long double >
#define ld long double
#define piii  pair<pii,int>
#define vii vector<pair<int,int> >
#define st first
#define nd second
#define pll pair<ll,ll>
#define speed ios::sync_with_stdio(false);cin.tie();cout.tie();
#define M_PI 3.14159265358979323846
//#define int long long
const int mod=1000000007;
//const int mod=1009;
//const int mod=998244353;
const int inf=1000000009;
const long long INF=1000000000000000009;
const long long big=1000000000000000;
const long double eps=0.000000000000000000001;
using namespace std;
int D[65][65][65],DP[65][65][65],W[65][65];
int main()
{
    speed
    int n,m,r;
    cin>>n>>m>>r;
    for(int i=1; i<=m; i++)
        for(int j=1; j<=n; j++)
            for(int k=1; k<=n; k++)
                cin>>D[i][j][k];
    for(int i=1; i<=m; i++)
        for(int j=1; j<=n; j++)
            for(int k=1; k<=n; k++)
                for(int l=1; l<=n; l++)
                    D[i][k][l]=min(D[i][k][l],D[i][k][j]+D[i][j][l]);
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
        {
            W[i][j]=inf;
            for(int k=1; k<=m; k++)
                W[i][j]=min(W[i][j],D[k][i][j]);
            //cout<<i<<" "<<j<<" "<<W[i][j]<<endl;
        }

    for(int j=1; j<=n; j++)
        for(int k=1; k<=n; k++)
        {
            DP[j][k][0]=W[j][k];
            for(int l=1; l<=n; l++)
                DP[j][k][l]=inf;
        }
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            for(int k=1; k<=n; k++)
                for(int l=1; l<=n; l++)
                {
                    DP[k][l][i]=min(DP[k][l][i],DP[k][j][i-1]+W[j][l]);
                    //cout<<i<<" "<<j<<" "<<k<<" "<<l<<" "<<DP[j][l][i]<<endl;
                }

    for(int i=1; i<=r; i++)
    {
        int a,b,c;
        cin>>a>>b>>c;
        c=min(c,n);
        cout<<DP[a][b][c]<<"\n";
    }
    return 0;
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//typedef tree < int , null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update> TREE;
#define ll long long
#define ull unsigned long long int
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define pii pair<int,int>
#define pss pair<short,short>
#define pld pair<long double,long double >
#define ld long double
#define piii  pair<pii,int>
#define vii vector<pair<int,int> >
#define st first
#define nd second
#define pll pair<ll,ll>
#define speed ios::sync_with_stdio(false);cin.tie();cout.tie();
#define M_PI 3.14159265358979323846
//#define int long long
const int mod=1000000007;
//const int mod=1009;
//const int mod=998244353;
const int inf=1000000009;
const long long INF=1000000000000000009;
const long long big=1000000000000000;
const long double eps=0.000000000000000000001;
using namespace std;
int D[65][65][65],DP[65][65][65],W[65][65];
int main()
{
    speed
    int n,m,r;
    cin>>n>>m>>r;
    for(int i=1; i<=m; i++)
        for(int j=1; j<=n; j++)
            for(int k=1; k<=n; k++)
                cin>>D[i][j][k];
    for(int a=1;a<=5;a++)
    for(int i=1; i<=m; i++)
        for(int j=1; j<=n; j++)
            for(int k=1; k<=n; k++)
                for(int l=1; l<=n; l++)
                    D[i][j][l]=min(D[i][j][l],D[i][j][k]+D[i][k][l]);
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
        {
            W[i][j]=inf;
            for(int k=1; k<=m; k++)
                W[i][j]=min(W[i][j],D[k][i][j]);
            //cout<<i<<" "<<j<<" "<<W[i][j]<<endl;
        }

    for(int j=1; j<=n; j++)
        for(int k=1; k<=n; k++)
        {
            DP[j][k][0]=W[j][k];
            for(int l=1; l<=n; l++)
                DP[j][k][l]=inf;
        }
    for(int a=1;a<=5;a++)
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            for(int k=1; k<=n; k++)
                for(int l=1; l<=n; l++)
                {
                    DP[j][l][i]=min(DP[j][l][i],DP[j][k][i-1]+W[k][l]);
                    //cout<<i<<" "<<j<<" "<<k<<" "<<l<<" "<<DP[j][l][i]<<endl;
                }

    for(int i=1; i<=r; i++)
    {
        int a,b,c;
        cin>>a>>b>>c;
        c=min(c,n);
        cout<<DP[a][b][c]<<"\n";
    }
    return 0;
}


# include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;

# define F first
# define S second
# define pb push_back
# define endl '\n'
# define sep ' '

const int xn = 60 + 10;
const int xm = 1e3 + 10;

int n, m, r;
int a[xn][xn][xn], dist[xn][xn];
int dis[xn][xn][xn], dp[xn][xn][xm];

int main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	cin >> n >> m >> r;
	for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) cin >> a[i][j][k];

	for (int car = 1; car <= m; car++){
		for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) dis[car][i][j] = a[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]);
	}
	//cout << endl;
	for (int i = 1; i <= n; i++){
		for (int j = 1; j <= n; j++){
			dist[i][j] = 1e9+10;
			for (int k = 1; k <= m; k++) dist[i][j] = min(dis[k][i][j], dist[i][j]);
			//cout << dist[i][j] << sep;
		}
		//cout << endl;
	}
	for (int i = 1; i <= n; i++){
		for (int j = 1;  j <= n; j++) dp[i][j][0] = dist[i][j];
		for (int k = 1; k <= 1000; k++){
			for (int v = 1; v <= n; v++){
				dp[i][v][k] = 1e9 + 10;
				for (int u = 1; u <= n; u++){
					if (v == u) continue;
					dp[i][v][k] = min(dp[i][v][k], dp[i][u][k-1] + dist[u][v]);
				}
			}
		}
	}
	//cout << dp[1][4][1] << endl;
	while (r--){
		int s, t, k;
		cin >> s >> t >> k;
		int mn = 1e9+10;
		for (int i = 0; i <= k; i++) mn = min(mn, dp[s][t][i]);
		cout << mn << endl;
	}
}

/*
4 2 3
0 1 5 6
2 0 3 6
1 3 0 1
6 6 7 0
0 3 5 6
2 0 1 6
1 3 0 2
6 6 7 0
1 4 2
1 4 1
1 4 3

*/


# include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;

# define F first
# define S second
# define pb push_back
# define endl '\n'
# define sep ' '

const int xn = 60 + 10;
const int xm = 1e3 + 10;

int n, m, r;
int a[xn][xn][xn], dist[xn][xn];
int dis[xn][xn][xn], dp[xn][xn][xm];

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

	for (int car = 1; car <= m; car++){
		for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) dis[car][i][j] = a[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]);
	}
	//cout << endl;
	for (int i = 1; i <= n; i++){
		for (int j = 1; j <= n; j++){
			dist[i][j] = 1e9+10;
			for (int k = 1; k <= m; k++) dist[i][j] = min(dis[k][i][j], dist[i][j]);
			//cout << dist[i][j] << sep;
		}
		//cout << endl;
	}
	for (int i = 1; i <= n; i++){
		for (int j = 1;  j <= n; j++) dp[i][j][0] = dist[i][j];
		for (int k = 1; k <= 1000; k++){
			for (int v = 1; v <= n; v++){
				dp[i][v][k] = 1e9 + 10;
				for (int u = 1; u <= n; u++){
					if (v == u) continue;
					dp[i][v][k] = min(dp[i][v][k], dp[i][u][k-1] + dist[u][v]);
				}
			}
		}
	}
	//cout << dp[1][4][1] << endl;
	while (r--){
		int s, t, k;
		cin >> s >> t >> k;
		int mn = 1e9+10;
		for (int i = 0; i <= k; i++) mn = min(mn, dp[s][t][i]);
		cout << mn << endl;
	}
}

/*
4 2 3
0 1 5 6
2 0 3 6
1 3 0 1
6 6 7 0
0 3 5 6
2 0 1 6
1 3 0 2
6 6 7 0
1 4 2
1 4 1
1 4 3

*/


#include "bits/stdc++.h"
// #include "ext/pb_ds/assoc_container.hpp"

#define mp make_pair
#define pb push_back
#define ppb pop_back
#define db puts("*****")
#define mid(x , y) ((x+y)>>1)
#define ff first
#define ss second
#define all(x)	x.begin(),x.end()
#define ll long long
#define sqr(x)	((x)*(x))
#define pii pair <int , int>
#define pll pair <ll, ll>
#define sz(x) int(x.size())
#define tr(it , c) for(__typeof(c.begin()) it = (c.begin()); it != (c.end()); it++)
#define y1 you_made_my_day

using namespace std;
// using namespace __gnu_pbds;

const int N = 60+7;
const int M = 600;
const int MOD = 1e9+7;
const int K = 1e3+7;

template<class T> bool umin(T& a, T b) { if(a > b){ a = b; return 1; } return 0;}
template<class T> bool umax(T& a, T b) { if(a < b){ a = b;return 1;}return 0;}
template<class T> bool umod(T& a) { while(a < 0) a += MOD; a %= MOD; return 1;}
// typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

//	freopen("file.in" , "r" , stdin);
//	freopen("file.out" , "w" , stdout);

int n, m, q;
int cost[N][N][N], d[N][N][N];

int main(){
  scanf("%d%d%d", &n, &m, &q);
  for(int w=1; w<=m; w++)
    for(int i=1; i<=n; i++)
      for(int j=1; j<=n; j++)
        scanf("%d", &cost[w][i][j]);

  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++)
          umin(cost[c][i][j], cost[c][i][k] + cost[c][k][j]);

  for(int i=1; i<=n; i++)
    for(int j=1; j<=n; j++){
      for(int k=0; k<=n; k++)
        d[i][j][k] = MOD;
      for(int c=1; c<=m; c++)
        umin(d[i][j][0], cost[c][i][j]);
    }

  for(int w=1; w<=n; w++)
    for(int k=1; k<=n; k++)
      for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++){                        // d[i][j][w-1] is calculated for every {i, j};
          umin(d[i][j][w], d[i][j][w-1]);               // d[i][k][w-2] + d[k][j][1] is actually considered at some other h
          umin(d[i][j][w], d[i][k][w-1] + d[k][j][0]);  // d[i][h][w-1] + d[h][j][0] where h is a node on the path from k.. to ..j
        }

  int a, b, c;
  while(q--){
    scanf("%d%d%d", &a, &b, &c);
    printf("%d\n", d[a][b][min(n, c)]);
  }

  return 0;
}


#include <bits/stdc++.h>

#pragma GCC optimize ("Ofast")

#define oo 1000000000000000000LL
#define ooo __builtin_inf()
#define mod 1000000007
#define MOD 998244353
#define dbg(x) cerr<< #x << " : " << x << endl
#define SZ(x) (long long)((x).size())
#define File(s, t) ifstream fin ((string)s + ".txt", ios::in); ofstream fout ((string)t + ".txt", ios::out);
#define input(j, n, a) for (size_t _i = (j); _i < (n)+(j); _i++) cin>> a[_i];
#define output(j, n, a, t) for (size_t _i = (j); _i < (n)+(j); _i++) cout<< a[_i] << (((t) && _i != (n)+(j)-1)? ' ' : '\n');
#define cl const long long
#define fi first
#define se second
#define All(x) ((x).begin(), (x).end())
#define dbl_io(x) cout<< fixed << setprecision(x)

using namespace std;

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

template <class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) {
	return out << "(" << a.first << ", " << a.second << ")";
}

//ll ppow (ll x, ll n, ll m) { return (!n ? 1 : ((n & 1 ? x : 1) * ppow(x * x % m, n / 2, m) % m)); }
//ll fact [1000*1000];
//ll fct (ll x, ll m) { return fact[x] = (!x ? 1 : (x * fct (x-1, m) % m)); }
//ll C (ll k, ll n, ll m) { return (k > n? 0 : (fact[n] * ppow((fact[k] * fact[n-k]) % m, m-2 ,m)) % m);}

cl N = 62;

ll dis [N][N][N][2], dp [N][N][N];

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

    memset (dis, 31, sizeof dis);
    memset (dp, 31, sizeof dp);

    ll n, m, q, i, j, r, t, v, u, k;

    cin>> n >> m >> q;

    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++)
            for (r = 0; r < n; r++)
                cin>> dis[i][j][r][0];

        for (j = 0; j < n; j++) {
            k = 1 - (j % 2);
            for (r = 0; r < n; r++)
                for (t = 0; t < n; t++)
                    dis[i][r][t][k] = min (dis[i][r][t][1-k], dis[i][r][j][1-k] + dis[i][j][t][1-k]),
                    dp[0][r][t] = min (dp[0][r][t], dis[i][r][t][k]);
        }
    }

    for (i = 1; i < N; i++)
        for (j = 0; j < n; j++)
            for (r = 0; r < n; r++)
                for (t = 0; t < n; t++)
                    dp[i][j][r] = min (dp[i][j][r], dp[i-1][j][t] + dp[0][t][r]);

    while (q--) {
        cin>> v >> u >> k;
        v--, u--, k = min (k, n);

        cout<< dp[k][v][u] << '\n';
    }

    cerr<< "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";

    return 0;
}
/*

 */


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

const int loli_cuteness = 0x7fffffff;

int n, m, r, s, t, k;
int d[65][65];
int res[65][65][65];

void floyd_warshall() {
	for (int k = 0; k < n; k++) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
			}
		}
	}
}

signed loli() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> n >> m >> r;
	for (int i = 0; i < 65; i++) {
		for (int j = 0; j < 65; j++) {
			for (int k = 0; k < 65; k++) {
				res[i][j][k] = loli_cuteness;
			}
		}
	}
	for (int l = 1; l <= m; l++) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				cin >> d[i][j];
			}
		}
		floyd_warshall();
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				res[0][i][j] = min(d[i][j], res[0][i][j]);
			}
		}
	}
	for (int l = 1; l <= n; l++) {
		for (int k = 0; k < n; k++) {
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					res[l][i][j] = min(res[l][i][j], res[l - 1][i][k] + res[0][k][j]);
				}
			}
		}
	}
	for (int i = 0; i < r; i++) {
		cin >> s >> t >> k;
		cout << res[min(k, n)][s - 1][t - 1] << '\n';
	}
	return 0;
}


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

#define fi first
#define se second
#define pb push_back
#define mod(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,a,b) for(int i = a; i < b; i++)
#define forr(i,a,b) for(int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

typedef pair<ll, pair<int,ii> > state;
const int maxn = 60+10;
const ll oo = 1ll<<50;
ll mat[maxn][maxn][maxn];
ll dp[maxn][maxn][maxn];
int N,M,R;

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> N >> M >> R;
	R = min(R,N);
	forn(i,0,M)forn(j,0,N)forn(k,0,N)cin >> mat[i][j][k];
	forn(l,0,M)forn(k,0,N)forn(i,0,N)forn(j,0,N)if(mat[l][i][k]+mat[l][k][j] < mat[l][i][j]){
		mat[l][i][j] = mat[l][i][k]+mat[l][k][j];
	}
	forn(r,0,R+1)forn(i,0,N)forn(j,0,N)dp[r][i][j] = oo;
	forn(i,0,N)forn(j,0,N){
		forn(k,0,M)dp[0][i][j] = min(dp[0][i][j],mat[k][i][j]);
	}
	forn(r,1,R+1){
		forn(k,0,N)forn(i,0,N)forn(j,0,N){
			if(dp[r-1][i][k]+dp[0][k][j] < dp[r][i][j]){
				dp[r][i][j] = dp[r-1][i][k]+dp[0][k][j];
			}
		}
	}
	while(R--){
		int u,v,c; cin >> u >> v >> c; u--; v--;
		c = min(c,N);
		cout << dp[c][u][v] << '\n';
	}
	return 0;
}
/*
__builtin_mul_overflow(x,y,&x)
-fsplit-stack
*/


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

#define fi first
#define se second
#define pb push_back
#define mod(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,a,b) for(int i = a; i < b; i++)
#define forr(i,a,b) for(int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

typedef pair<ll, pair<int,ii> > state;
const int maxn = 60+10;
const ll oo = 1ll<<50;
ll mat[maxn][maxn][maxn];
ll dp[maxn][maxn][maxn];
int N,M,R;

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> N >> M >> R;
	forn(i,0,M)forn(j,0,N)forn(k,0,N)cin >> mat[i][j][k];
	forn(l,0,M)forn(k,0,N)forn(i,0,N)forn(j,0,N)if(mat[l][i][k]+mat[l][k][j] < mat[l][i][j]){
		mat[l][i][j] = mat[l][i][k]+mat[l][k][j];
	}
	forn(r,0,N+1)forn(i,0,N)forn(j,0,N)dp[r][i][j] = oo;
	forn(i,0,N)forn(j,0,N){
		forn(k,0,M)dp[0][i][j] = min(dp[0][i][j],mat[k][i][j]);
	}
	forn(r,1,N+1){
		forn(k,0,N)forn(i,0,N)forn(j,0,N){
			if(dp[r-1][i][k]+dp[0][k][j] < dp[r][i][j]){
				dp[r][i][j] = dp[r-1][i][k]+dp[0][k][j];
			}
		}
	}
	while(R--){
		int u,v,c; cin >> u >> v >> c; u--; v--;
		c = min(c,N);
		cout << dp[c][u][v] << '\n';
	}
	return 0;
}
/*
__builtin_mul_overflow(x,y,&x)
-fsplit-stack
*/


#include <iostream>
#include <bits/stdc++.h>
#define forn(i,n)for(int i=0;i<n;i++)
#define forna(i,a,n)for(int i=a;i<n;i++)
#define pb push_back
#define mp make_pair
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mod 1e9+7
#define ff first
#define ss second
#define INF 1e9
#define MAX 100001
#define ya cout<<"YES"<<endl;continue;
typedef long long ll;
using namespace std;

int main()
{
    IOS
    int n,m,r;cin>>n>>m>>r;
    int arr[m][n][n];
    int ans[65][n][n];
    forn(k,65) forn(i,n) forn(j,n) ans[k][i][j] = INF;
    forn(i,m){
        forn(j,n) forn(k,n) cin>>arr[i][j][k];
        forn(l,n) forn(j,n) forn(k,n)
            arr[i][j][k] = min(arr[i][j][k],arr[i][j][l]+arr[i][l][k]);
        forn(j,n) forn(k,n) ans[0][j][k] = min(ans[0][j][k],arr[i][j][k]);
    }
    forn(i,65) forn(j,n) forn(k,n) forn(x,n)
        ans[i+1][j][k] = min(ans[i+1][j][k],ans[i][j][x]+ans[0][x][k]);

    forn(i,r){
        int a,b,y;cin>>a>>b>>y; a--;b--;
        if(y>=65) y= 64;
        cout<<ans[y][a][b]<<endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

inline void Boost() {
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
}

const int NMAX = 65;
const int RMAX = 1e5 + 10;
const int KMAX = 1005;
const long long inf = 1e12;

///n number of cities
///m diferent cars
///r competitors
int n,m,r;
long long mat[NMAX][NMAX][NMAX];
int s, t, k;
long long dp[NMAX][NMAX][NMAX];

int main(){
    Boost();

    int i,j,k,Max = 0,x,y,ii,jj,o;

    cin >> n >> m >> r;

    for(k = 0 ; k <= n ; k++)
        for(i = 0 ; i <= n ; i++)
            for(j = 0 ; j <= n ; j++)
                dp[k][i][j] = inf;

    for(k = 1 ; k <= m ; k++){

        for(i = 1 ; i <= n ; i++)
            for(j = 1 ; j <= n ; j++)
                cin >> mat[k][i][j];

        for(o = 1 ; o <= n ; o++)
            for(i = 1 ; i <= n ; i++)
                for(j = 1 ; j <= n ; j++)
                    if(i != j)
                        mat[k][i][j] = min(mat[k][i][j], mat[k][i][o] + mat[k][o][j]);

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

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

    while(r--){
        cin >> s >> t >> k;
        k = min(k, n - 1);
        cout << dp[k][s][t] << "\n";
    }

    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define pll pair<ll, ll>
#define pii pair<int, int>
//#define debug

using namespace std;
int arr[65][65][65], dp[65][65][65];

int main()
{
#ifndef debug
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
#endif
    int n, m, r;
    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 >> arr[i][j][k];

    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            for (int z = 0; z <= n; z++)
                dp[i][j][z] = INT_MAX;

    for (int c = 0; c < m; c++)
    {
        for (int k = 0; k < n; k++)
        {
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    arr[c][i][j] = min(arr[c][i][k] + arr[c][k][j], arr[c][i][j]);
                    dp[i][j][0] = min(dp[i][j][0], arr[c][i][j]);
                }
            }
        }
    }
    for (int k = 1; k <= n; k++)
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                for (int z = 0; z < n; z++)
                {
                    dp[i][j][k] = min(dp[i][j][k], dp[i][z][k - 1] + dp[z][j][0]);
                }
            }
        }
    }
    for (int i = 0; i < r; i++)
    {
        int a, b, k;
        cin >> a >> b >> k;
        a--, b--, k = min(k, n);
        cout << dp[a][b][k] << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define pll pair<ll, ll>
#define pii pair<int, int>
//#define debug

using namespace std;
int arr[65][65][65], dp[65][65][65];

int main()
{
#ifndef debug
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
#endif
    int n, m, r;
    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 >> arr[i][j][k];
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            for (int z = 0; z < n; z++)
            {
                dp[i][j][z] = INT_MAX;
            }
        }
    }
    for (int c = 0; c < m; c++)
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                for (int k = 0; k < n; k++)
                {
                    arr[c][i][j] = min(arr[c][i][k] + arr[c][k][j], arr[c][i][j]);
                    dp[i][j][0] = min(dp[i][j][0], arr[c][i][j]);
                }
            }
        }
    }
    for (int k = 1; k <= n; k++)
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                for (int z = 0; z < n; z++)
                {
                    dp[i][j][k] = min(dp[i][j][k], dp[i][z][k - 1] + dp[z][j][0]);
                }
            }
        }
    }
    for (int i = 0; i < r; i++)
    {
        int a, b, k;
        cin >> a >> b >> k;
        a--, b--;
        cout << dp[a][b][k] << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define pll pair<ll, ll>
#define pii pair<int, int>
//#define debug

using namespace std;
int arr[65][65][65], dp[65][65][65];

int main()
{
#ifndef debug
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
#endif
    int n, m, r;
    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 >> arr[i][j][k];
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            for (int z = 0; z <= n; z++)
            {
                dp[i][j][z] = INT_MAX;
            }
        }
    }
    for (int c = 0; c < m; c++)
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                for (int k = 0; k < n; k++)
                {
                    arr[c][i][j] = min(arr[c][i][k] + arr[c][k][j], arr[c][i][j]);
                    dp[i][j][0] = min(dp[i][j][0], arr[c][i][j]);
                }
            }
        }
    }
    for (int k = 1; k <= n; k++)
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                for (int z = 0; z < n; z++)
                {
                    dp[i][j][k] = min(dp[i][j][k], dp[i][z][k - 1] + dp[z][j][0]);
                }
            }
        }
    }
    for (int i = 0; i < r; i++)
    {
        int a, b, k;
        cin >> a >> b >> k;
        a--, b--, k = min(k, n);
        cout << dp[a][b][k] << '\n';
    }
    return 0;
}


//IOI 2021
//labriz kon code ra, ham paye ie CF manam, CF age yari kone ta sob man code miznm :)
#include <bits/stdc++.h>
using namespace std;

const int MAXK = 1000 + 1, MAXN = 60 + 1, inf = 1e9 + 17;

int n, m, q, a[MAXN][MAXN][MAXN], dp[MAXN][MAXN][MAXK];

int main() {
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n >> m >> q;
	for (int k = 1; k <= m; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				cin >> a[k][i][j], dp[i][j][0] = inf;
	//floyd
	for (int k = 1; k <= m; k++)
		for (int p = 1; p <= n; p++)
			for (int i = 1; i <= n; i++)
				for (int j = 1; j <= n; j++)
					a[k][i][j] = min(a[k][i][j], a[k][i][p] + a[k][p][j]);
	//paye dp
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			for (int k = 1; k <= m; k++)
				dp[i][j][0] = min(dp[i][j][0], a[k][i][j]);

	//dp
	for (int k = 1; k < MAXK; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++) {
				dp[i][j][k] = dp[i][j][k - 1];
				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]);
			}
	//query
	while (q--) {
		int s, t, k;
		cin >> s >> t >> k;
		cout << dp[s][t][k] << '\n';
	}
	return 0;
}


//IOI 2021
//labriz kon code ra, ham paye ie CF manam, CF age yari kone ta sob man code miznm :)
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 60 + 17, inf = 1e9 + 17;

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

int main() {
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n >> m >> q;
	for (int k = 1; k <= m; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				cin >> a[k][i][j], dp[i][j][0] = inf;
	//floyd
	for (int k = 1; k <= m; k++)
		for (int p = 1; p <= n; p++)
			for (int i = 1; i <= n; i++)
				for (int j = 1; j <= n; j++)
					a[k][i][j] = min(a[k][i][j], a[k][i][p] + a[k][p][j]);
	//paye dp
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			for (int k = 1; k <= m; k++)
				dp[i][j][0] = min(dp[i][j][0], a[k][i][j]);

	//dp
	for (int k = 1; k < MAXN; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++) {
				dp[i][j][k] = dp[i][j][k - 1];
				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]);
			}
	//query
	while (q--) {
		int s, t, k;
		cin >> s >> t >> k;
		cout << dp[s][t][min(k, MAXN - 1)] << '\n';
	}
	return 0;
}


//IOI 2021
//labriz kon code ra, ham paye ie CF manam, CF age yari kone ta sob man code miznm :)
#pragma GCC optimize("O3,Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define ff first
#define ss second

typedef long long ll;
typedef pair<int, int> pii;

const int MAXK = 1000 + 17, MAXN = 60 + 17, inf = 2e9 + 17;

int n, m, q, a[MAXN][MAXN][MAXN], dp[MAXN][MAXN][MAXK];

int main() {
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n >> m >> q;
	for (int k = 1; k <= m; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				cin >> a[k][i][j];
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			dp[i][j][0] = inf;
	int tmp[MAXN][MAXN];
	for (int k = 1; k <= m; k++) {
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				tmp[i][j] = a[k][i][j];
		for (int p = 1; p <= n; p++)
			for (int i = 1; i <= n; i++)
				for (int j = 1; j <= n; j++)
					tmp[i][j] = min(tmp[i][j], tmp[i][p] + tmp[p][j]);

		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				dp[i][j][0] = min(dp[i][j][0], tmp[i][j]);
	}
	for (int k = 1; k < MAXK; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++) {
				dp[i][j][k] = inf;
				for (int p = 1; p <= n; p++)
					dp[i][j][k] = min(1LL * dp[i][j][k], 1LL * dp[i][p][k - 1] + dp[p][j][0]);
			}

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


//IOI 2021
//labriz kon code ra, ham paye ie CF manam, CF age yari kone ta sob man code miznm :)
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define ff first
#define ss second

typedef long long ll;
typedef pair<int, int> pii;

const int MAXK = 1000 + 17, MAXN = 60 + 17, inf = 2e9 + 17;

int n, m, q, a[MAXN][MAXN][MAXN], dp[MAXN][MAXN][MAXK];

int main() {
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n >> m >> q;
	for (int k = 1; k <= m; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				cin >> a[k][i][j];
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			dp[i][j][0] = inf;
	int tmp[MAXN][MAXN];
	for (int k = 1; k <= m; k++) {
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				tmp[i][j] = a[k][i][j];
		for (int p = 1; p <= n; p++)
			for (int i = 1; i <= n; i++)
				for (int j = 1; j <= n; j++)
					tmp[i][j] = min(tmp[i][j], tmp[i][p] + tmp[p][j]);

		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				dp[i][j][0] = min(dp[i][j][0], tmp[i][j]);
	}
	for (int k = 1; k < MAXK; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++) {
				dp[i][j][k] = inf;
				for (int p = 1; p <= n; p++)
					dp[i][j][k] = min(1LL * dp[i][j][k], 1LL * dp[i][p][k - 1] + dp[p][j][0]);
			}

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


//IOI 2021
//labriz kon code ra, ham paye ie CF manam, CF age yari kone ta sob man code miznm :)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define ff first
#define ss second

typedef long long ll;
typedef pair<int, int> pii;

const int MAXK = 1000 + 17, MAXN = 60 + 17, inf = 2e9 + 17;

int n, m, q, a[MAXN][MAXN][MAXN], dp[MAXN][MAXN][MAXK];

int main() {
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n >> m >> q;
	for (int k = 1; k <= m; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				cin >> a[k][i][j];
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			dp[i][j][0] = inf;
	int tmp[MAXN][MAXN];
	for (int k = 1; k <= m; k++) {
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				tmp[i][j] = a[k][i][j];
		for (int p = 1; p <= n; p++)
			for (int i = 1; i <= n; i++)
				for (int j = 1; j <= n; j++)
					tmp[i][j] = min(tmp[i][j], tmp[i][p] + tmp[p][j]);

		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				dp[i][j][0] = min(dp[i][j][0], tmp[i][j]);
	}
	for (int k = 1; k < MAXK; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++) {
				dp[i][j][k] = inf;
				for (int p = 1; p <= n; p++)
					dp[i][j][k] = min(1LL * dp[i][j][k], 1LL * dp[i][p][k - 1] + dp[p][j][0]);
			}

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


//IOI 2021
//labriz kon code ra, ham paye ie CF manam, CF age yari kone ta sob man code miznm :)
#pragma GCC optimize("O3,Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define ff first
#define ss second

typedef long long ll;
typedef pair<int, int> pii;

const int MAXK = 1000 + 1, MAXN = 60 + 1, inf = 2e9 + 17;

int n, m, q, a[MAXN][MAXN][MAXN], dp[MAXN][MAXN][MAXK];

int main() {
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n >> m >> q;
	for (int k = 1; k <= m; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				cin >> a[k][i][j];
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			dp[i][j][0] = inf;
	int tmp[MAXN][MAXN];
	for (int k = 1; k <= m; k++) {
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				tmp[i][j] = a[k][i][j];
		for (int p = 1; p <= n; p++)
			for (int i = 1; i <= n; i++)
				for (int j = 1; j <= n; j++)
					tmp[i][j] = min(tmp[i][j], tmp[i][p] + tmp[p][j]);

		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				dp[i][j][0] = min(dp[i][j][0], tmp[i][j]);
	}
	for (int k = 1; k < MAXK; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++) {
				dp[i][j][k] = inf;
				for (int p = 1; p <= n; p++)
					dp[i][j][k] = min(1LL * dp[i][j][k], 1LL * dp[i][p][k - 1] + dp[p][j][0]);
			}

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


//IOI 2021
//labriz kon code ra, ham paye ie CF manam, CF age yari kone ta sob man code miznm :)
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 60 + 17, inf = 1e9 + 17;

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

int main() {
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n >> m >> q;
	for (int k = 1; k <= m; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				cin >> a[k][i][j], dp[i][j][0] = inf;
	//floyd
	for (int k = 1; k <= m; k++)
		for (int p = 1; p <= n; p++)
			for (int i = 1; i <= n; i++)
				for (int j = 1; j <= n; j++)
					a[k][i][j] = min(a[k][i][j], a[k][i][p] + a[k][p][j]);
	//paye dp
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			for (int k = 1; k <= m; k++)
				dp[i][j][0] = min(dp[i][j][0], a[k][i][j]);

	//dp
	for (int k = 1; k < MAXN; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++) {
				dp[i][j][k] = dp[i][j][k - 1];
				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]);
			}
	//query
	while (q--) {
		int s, t, k;
		cin >> s >> t >> k;
		cout << dp[s][t][min(k, MAXN)] << '\n';
	}
	return 0;
}


//IOI 2021
//labriz kon code ra, ham paye ie CF manam, CF age yari kone ta sob man code miznm :)
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int MAXK = 1000 + 1, MAXN = 60 + 1, inf = 1e9 + 17;

int n, m, q, a[MAXN][MAXN][MAXN], dp[MAXN][MAXN][MAXK];

int main() {
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n >> m >> q;
	for (int k = 1; k <= m; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				cin >> a[k][i][j], dp[i][j][0] = inf;
	//floyd
	for (int k = 1; k <= m; k++)
		for (int p = 1; p <= n; p++)
			for (int i = 1; i <= n; i++)
				for (int j = 1; j <= n; j++)
					a[k][i][j] = min(a[k][i][j], a[k][i][p] + a[k][p][j]);
	//paye dp
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			for (int k = 1; k <= m; k++)
				dp[i][j][0] = min(dp[i][j][0], a[k][i][j]);

	//dp
	for (int k = 1; k < MAXK; k++)
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++) {
				dp[i][j][k] = dp[i][j][k - 1];
				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]);
			}
	//query
	while (q--) {
		int s, t, k;
		cin >> s >> t >> k;
		cout << dp[s][t][k] << endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <chrono>
#include <random>
#include <limits>
#include <cstring>

std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());

const int ms = 66;

int dp[ms][ms][ms];

int main() {
	std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);
	int n, m, r;
	std::cin >> n >> m >> r;
	memset(dp, 0x3f, sizeof dp);
	while(m--) {
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				std::cin >> dp[0][i][j];
			}
		}
		for(int k = 0; k < n; k++) {
			for(int i = 0; i < n; i++) {
				for(int j = 0; j < n; j++) {
					dp[0][i][j] = std::min(dp[0][i][j], dp[0][i][k] + dp[0][k][j]);
				}
			}
		}
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				dp[1][i][j] = std::min(dp[1][i][j], dp[0][i][j]);
			}
		}
	}
	for(int x = 2; x < ms; x++) {
		for(int k = 0; k < n; k++) {
			for(int i = 0; i < n; i++) {
				for(int j = 0; j < n; j++) {
					dp[x][i][j] = std::min(dp[x][i][j], dp[x-1][i][k] + dp[1][k][j]);
				}
			}
		}
	}
	while(r--) {
		int s, t, k;
		std::cin >> s >> t >> k;
		s--;t--;k++;
		k = std::min(k, ms - 1);
		std::cout << dp[k][s][t] << '\n';
	}
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int MAXN = 60 + 6;
int n, m, r;
int a[MAXN][MAXN][MAXN], dis[MAXN][MAXN][MAXN], dp[MAXN][MAXN][MAXN];
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >> n >> m >> r;
	for(int i = 0; i < m; i ++){
		for(int j = 0; j < n; j ++){
			for(int h = 0; h < n; h ++){
				cin >> a[i][j][h];
				dis[i][j][h] = a[i][j][h];
			}
		}
	}
	for(int c = 0; c < m; c ++){
		for(int ii = 0; ii < n/2; ii ++){
		for(int i = 0; i < n; i ++){
			for(int j = 0; j < n; j ++)
				for(int h = 0; h < n; h ++) dis[c][i][j] = min(dis[c][i][j], dis[c][i][h] + dis[c][h][j]);
		}
		}
		for(int i = 0; i < n; i ++){
			for(int j = 0; j < n; j ++){
				if( c != 0) dp[0][i][j] = min(dp[0][i][j], dis[c][i][j]);
				else dp[0][i][j] = dis[0][i][j];
			}
		}
	}
	for(int c = 1; c <  n; c ++){
		for(int i = 0; i < n; i ++){
			for(int j = 0; j < n; j ++){
				dp[c][i][j] = INT_MAX;
				for(int h = 0; h < n; h ++){
					dp[c][i][j] = min(dp[c][i][j], dp[0][i][h] + dp[c - 1][h][j]);
				}
			}
		}
	}
	while( r ){
		r --;
		int s, t, np;
		cin >> s >> t >> np;
		np = min(np, n - 1);
		cout << dp[np][--s][--t] << endl;
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int MAXN = 60 + 6;
int n, m, r;
int a[MAXN][MAXN][MAXN], dis[MAXN][MAXN][MAXN], dp[MAXN][MAXN][MAXN];
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >> n >> m >> r;
	for(int i = 0; i < m; i ++){
		for(int j = 0; j < n; j ++){
			for(int h = 0; h < n; h ++){
				cin >> a[i][j][h];
				dis[i][j][h] = a[i][j][h];
			}
		}
	}
	for(int c = 0; c < m; c ++){
		for(int i = 0; i < n; i ++){
			for(int j = 0; j < n; j ++)
				for(int h = 0; h < n; h ++) dis[c][j][h] = min(dis[c][j][h], dis[c][j][i] + dis[c][i][h]);
		}
		for(int i = 0; i < n; i ++){
			for(int j = 0; j < n; j ++){
				if( c != 0) dp[0][i][j] = min(dp[0][i][j], dis[c][i][j]);
				else dp[0][i][j] = dis[0][i][j];
			}
		}
	}
	for(int c = 1; c <  n; c ++){
		for(int i = 0; i < n; i ++){
			for(int j = 0; j < n; j ++){
				dp[c][i][j] = INT_MAX;
				for(int h = 0; h < n; h ++){
					dp[c][i][j] = min(dp[c][i][j], dp[0][i][h] + dp[c - 1][h][j]);
				}
			}
		}
	}
	while( r ){
		r --;
		int s, t, np;
		cin >> s >> t >> np;
		np = min(np, n - 1);
		cout << dp[np][--s][--t] << endl;
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int MAXN = 60 + 6;
int n, m, r;
int a[MAXN][MAXN][MAXN], dis[MAXN][MAXN][MAXN], dp[MAXN][MAXN][MAXN];
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >> n >> m >> r;
	for(int i = 0; i < m; i ++){
		for(int j = 0; j < n; j ++){
			for(int h = 0; h < n; h ++){
				cin >> a[i][j][h];
				dis[i][j][h] = a[i][j][h];
			}
		}
	}
	for(int c = 0; c < m; c ++){
		for(int ii = 0; ii < 2; ii ++){
		for(int i = 0; i < n; i ++){
			for(int j = 0; j < n; j ++)
				for(int h = 0; h < n; h ++) dis[c][i][j] = min(dis[c][i][j], dis[c][i][h] + dis[c][h][j]);
		}
		}
		for(int i = 0; i < n; i ++){
			for(int j = 0; j < n; j ++){
				if( c != 0) dp[0][i][j] = min(dp[0][i][j], dis[c][i][j]);
				else dp[0][i][j] = dis[0][i][j];
			}
		}
	}
	for(int c = 1; c <  n; c ++){
		for(int i = 0; i < n; i ++){
			for(int j = 0; j < n; j ++){
				dp[c][i][j] = INT_MAX;
				for(int h = 0; h < n; h ++){
					dp[c][i][j] = min(dp[c][i][j], dp[0][i][h] + dp[c - 1][h][j]);
				}
			}
		}
	}
	while( r ){
		r --;
		int s, t, np;
		cin >> s >> t >> np;
		np = min(np, n - 1);
		cout << dp[np][--s][--t] << endl;
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int MAXN = 60 + 6;
int n, m, r;
int a[MAXN][MAXN][MAXN], dis[MAXN][MAXN][MAXN], dp[MAXN][MAXN][MAXN];
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >> n >> m >> r;
	for(int i = 0; i < m; i ++){
		for(int j = 0; j < n; j ++){
			for(int h = 0; h < n; h ++){
				cin >> a[i][j][h];
				dis[i][j][h] = a[i][j][h];
			}
		}
	}
	for(int c = 0; c < m; c ++){
		for(int i = 0; i < n; i ++){
			for(int j = 0; j < n; j ++)
				for(int h = 0; h < n; h ++) dis[c][i][j] = min(dis[c][i][j], dis[c][i][h] + dis[c][h][j]);
		}
		for(int i = 0; i < n; i ++){
			for(int j = 0; j < n; j ++){
				if( c != 0) dp[0][i][j] = min(dp[0][i][j], dis[c][i][j]);
				else dp[0][i][j] = dis[0][i][j];
			}
		}
	}
	for(int c = 1; c <  n; c ++){
		for(int i = 0; i < n; i ++){
			for(int j = 0; j < n; j ++){
				dp[c][i][j] = INT_MAX;
				for(int h = 0; h < n; h ++){
					dp[c][i][j] = min(dp[c][i][j], dp[0][i][h] + dp[c - 1][h][j]);
				}
			}
		}
	}
	while( r ){
		r --;
		int s, t, np;
		cin >> s >> t >> np;
		np = min(np, n - 1);
		cout << dp[np][--s][--t] << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define lc 2 * v
#define rc 2 * v + 1
#define mid (s + e) / 2
#define ll long long
#define int long long
#define ld long double
#define pii pair <int , int>
#define pll pair <long long , long long>
#define FAST ios::sync_with_stdio(false);cin.tie(0)
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")

const ll maxn = 60 + 10 , N = 1000 + 10 , SQ = 600 , base = 1999 , mod = 1e9 + 7 , INF = 1e18 , lg = 17;

int n , m , r , a[maxn][maxn][maxn] , mindist[maxn][maxn] , dp[maxn][maxn][N];

int32_t main() {
    FAST;
    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 i = 0; i < m; ++i) {
        for (int k = 0; k < n; ++k) {
            for (int ii = 0; ii < n; ++ii) {
                for (int j = 0; j < n; ++j) {
                    a[i][ii][j] = min(a[i][ii][j] , a[i][ii][k] + a[i][k][j]);
                }
            }
        }
    }

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

    for (int lim = 1; lim < N; ++lim) {
        for (int k = 0; k < n; ++k) {
            for (int i = 0; i < n; ++i) {
                dp[k][i][lim] = INF;
                for (int j = 0; j < n; ++j) {
                    dp[k][i][lim] = min(dp[k][i][lim] , dp[k][j][lim - 1] + mindist[j][i]);
                }
            }
        }
    }

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


/*
    ____        __
   / __ \____  / /_  ____ _____ ___
  / /_/ / __ \/ __ \/ __ `/ __ `__ \
 / _, _/ /_/ / / / / /_/ / / / / / /
/_/ |_|\____/_/ /_/\__,_/_/ /_/ /_/
*/
#include<bits/stdc++.h>

typedef long long ll ;

#define X   first
#define Y   second
#define mp  make_pair
#define pii pair<int , int>
#define vec vector
#define file_io freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define migmig ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pll pair<long long , long long >
#define pb push_back
#define SZ(x) ll(x.size())

using namespace std;
const int maxr = 1e5 + 12  ;
const int maxn = 60+5 ;
const int mod = 1000000007 /*998244353*/  ;
const ll inf = 1e15 ;
int n , m , dp[maxn][maxn][maxn] , r , a[maxn][maxn][maxn] , s[maxr] , t[maxr] , ka[maxr] ;

int main()
{
	migmig ;
	cin>>n>>m>>r;
	for(int i = 1 ; i <= m ; i ++ )
	{
		for(int j =1 ; j <= n ; j++ )
		{
			for(int k = 1 ; k <= n ; k++ )
			{
				cin>>a[i][j][k] ;
			}
		}
	}
	for(int i = 1 ; i <= r ; i ++ )
	{
		cin>>s[i]>>t[i]>>ka[i]  ;
	}

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

	memset(dp , 63 , sizeof dp) ;

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

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

	for(int i = 1 ; i <= r ; i ++ )
	{
		cout<<dp[min(ka[i] , n)][s[i]][t[i]] << endl ;
	}

}
//////
////////
///////////

////


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

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<string>vst;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

void fastio(){
	ios_base::sync_with_stdio(false);cin.tie(0);
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	#endif
	return;
}
#define all(ct) ct.begin() , ct.end()
#define endl "\n"
#define fr(i,a) for(auto i:a)
#define f(i,a,b) for(int i=a;i<b;++i)
#define fd(i,a,b) for(int i=a;i>=b;--i)
#define pb push_back
#define in(d,v) d.find(v)!=d.end()
#define mp make_pair
#define size(a) int(a.size())

const ll mod = 1e9 + 7;
const ll inf = ll(1e18);
const double PI = acos(-1);

ll modexp(ll x, ll n, int md) {
	if (n == 0)return 1;
	if (n % 2 == 0)return modexp((x * x) % md, n / 2, md);
	return (x * modexp((x * x) % md, n / 2, md)) % md;
}
int ans[60][60][60],n,m,a[60][60][60];
vi vis(60,0);
void shd(int s,int u,int c,int ch,int d){
	vis[u]=1;
	ans[s][u][ch]=min(ans[s][u][ch],d);
	f(i,0,n){
		if(vis[i])continue;
		f(j,0,m){
			if(j==c or u==s){
				shd(s,i,j,ch,d+a[j][u][i]);
			}
			else{
				shd(s,i,j,ch+1,d+a[j][u][i]);
			}
		}
	}
	vis[u]=0;
}
int main(){
	fastio();
	//clock_t tm = clock();
	int r,x,y,c;
	f(i,0,60)f(j,0,60)f(k,0,60)ans[i][j][k]=1e9;
	cin>>n>>m>>r;
	f(i,0,m)f(j,0,n)f(k,0,n)cin>>a[i][j][k];
	f(i,0,n)shd(i,i,0,0,0);
	f(ch,1,n){
		f(i,0,n)f(j,0,n)ans[i][j][ch]=min(ans[i][j][ch],ans[i][j][ch-1]);
	}
	while(r--){
		cin>>x>>y>>c;
		cout<<ans[x-1][y-1][min(c,n-1)]<<endl;
	}
	//cout<<((clock()-tm)*1.0)/CLOCKS_PER_SEC<<endl;
}


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

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<string>vst;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

void fastio(){
	ios_base::sync_with_stdio(false);cin.tie(0);
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	#endif
	return;
}
#define all(ct) ct.begin() , ct.end()
#define endl "\n"
#define fr(i,a) for(auto i:a)
#define f(i,a,b) for(int i=a;i<b;++i)
#define fd(i,a,b) for(int i=a;i>=b;--i)
#define pb push_back
#define in(d,v) d.find(v)!=d.end()
#define mp make_pair
#define size(a) int(a.size())

const ll mod = 1e9 + 7;
const ll inf = ll(1e18);
const double PI = acos(-1);

ll modexp(ll x, ll n, int md) {
	if (n == 0)return 1;
	if (n % 2 == 0)return modexp((x * x) % md, n / 2, md);
	return (x * modexp((x * x) % md, n / 2, md)) % md;
}
int main(){
	fastio();
	//clock_t tm = clock();
	int n,m,r,x,y,c;
	int ans[60][60][60],a[60][60][60];
	f(i,0,60)f(j,0,60)f(k,0,60)ans[i][j][k]=1e9;
	cin>>n>>m>>r;
	f(i,0,m)f(j,0,n)f(k,0,n)cin>>a[i][j][k];
	f(car,0,m){
		f(k,0,n){
            f(i,0,n){
                f(j,0,n) {
                    a[car][i][j] = min(a[car][i][j], a[car][i][k] + a[car][k][j]);
					ans[0][i][j]=min(ans[0][i][j],a[car][i][j]);
                }
            }
        }
	}
	f(changes,1,n){
		f(i,0,n){
			f(j,0,n){
				f(k,0,n){
					ans[changes][i][j]=min(ans[changes][i][j],ans[changes-1][i][k]+ans[0][k][j]);
				}
			}
		}
	}
	while(r--){
		cin>>x>>y>>c;
		cout<<ans[min(c,n-1)][x-1][y-1]<<endl;
	}
	//cout<<((clock()-tm)*1.0)/CLOCKS_PER_SEC<<endl;
}


#include<bits/stdc++.h>

using namespace std;

int dp[62][62][62];
bool isV[62][62][62];
int G[62][62];
int Min[62][62];
int n;

int cal(int i , int j , int k){
    if(k == 0)return Min[i][j];
    if(isV[i][j][k]) return dp[i][j][k];
    int ret = Min[i][j];
    for(int m = 0; i<n; i++){
        if(m == i || m == j)continue;
        ret = min(ret , Min[i][m] + cal(m , j , k - 1));
    }
    isV[i][j][k] = 1;
    return dp[i][j][k] = ret;
}

int main()
{
    int  m , r;
    scanf("%d%d%d",&n,&m,&r);
    for(int i = 0; i<n; i++){
        for(int j = 0; j<n; j++)
            Min[i][j] = 3e9;
    }
    while(m--){
        for(int i = 0; i<n; i++){
            for(int j = 0; j<n; j++)
                scanf("%d",&G[i][j]);
        }
        for(int k = 0; k<n; k++){
            for(int i = 0; i<n; i++){
                for(int j = 0; j<n; j++){
                    G[i][j] = min(G[i][j],G[i][k]+G[k][j]);
                }
            }
        }
        for(int i = 0; i<n; i++){
            for(int j = 0; j<n; j++){
                Min[i][j] = min(Min[i][j],G[i][j]);
            }
        }
    }
    while(r--){
        int i , j , k;
        scanf("%d%d%d",&i,&j,&k);
        i--; j--;
        printf("%d\n",cal(i,j,min(k,60)));
    }
}


#include<bits/stdc++.h>

using namespace std;

int dp[62][62][62];
bool isV[62][62][62];
int G[62][62];
int Min[62][62];
int n;

int cal(int i , int j , int k){
    if(k == 0)return Min[i][j];
    if(isV[i][j][k]) return dp[i][j][k];
    int ret = Min[i][j];
    for(int m = 0; m<n; m++){
        ret = min(ret , Min[i][m] + cal(m , j , k - 1));
    }
    isV[i][j][k] = 1;
    return dp[i][j][k] = ret;
}

int main()
{
    int  m , r;
    scanf("%d%d%d",&n,&m,&r);
    for(int i = 0; i<n; i++){
        for(int j = 0; j<n; j++)
            Min[i][j] = 1e9;
    }
    while(m--){
        for(int i = 0; i<n; i++){
            for(int j = 0; j<n; j++)
                scanf("%d",&G[i][j]);
        }
        for(int k = 0; k<n; k++){
            for(int i = 0; i<n; i++){
                for(int j = 0; j<n; j++){
                    G[i][j] = min(G[i][j],G[i][k]+G[k][j]);
                }
            }
        }
        for(int i = 0; i<n; i++){
            for(int j = 0; j<n; j++){
                Min[i][j] = min(Min[i][j],G[i][j]);
            }
        }
    }
    while(r--){
        int i , j , k;
        scanf("%d%d%d",&i,&j,&k);
        i--; j--;
        printf("%d\n",cal(i,j,min(k,60)));
    }
}


#include<bits/stdc++.h>

using namespace std;

int dp[62][62][62];
bool isV[62][62][62];
int G[62][62];
int Min[62][62];
int n;

int cal(int i , int j , int k){
    if(k == 0)return Min[i][j];
    if(dp[i][j][k] != -1) return dp[i][j][k];
    int ret = Min[i][j];
    for(int m = 0; m<n; m++){
        ret = min(ret , Min[i][m] + cal(m , j , k - 1));
    }
    return dp[i][j][k] = ret;
}

int main()
{
    int  m , r;
    scanf("%d%d%d",&n,&m,&r);
    for(int i = 0; i<n; i++){
        for(int j = 0; j<n; j++)
            Min[i][j] = 1e9;
    }
    memset(dp , -1 , sizeof dp);
    while(m--){
        for(int i = 0; i<n; i++){
            for(int j = 0; j<n; j++)
                scanf("%d",&G[i][j]);
        }
        for(int k = 0; k<n; k++){
            for(int i = 0; i<n; i++){
                for(int j = 0; j<n; j++){
                    G[i][j] = min(G[i][j],G[i][k]+G[k][j]);
                }
            }
        }
        for(int i = 0; i<n; i++){
            for(int j = 0; j<n; j++){
                Min[i][j] = min(Min[i][j],G[i][j]);
            }
        }
    }
    while(r--){
        int i , j , k;
        scanf("%d%d%d",&i,&j,&k);
        i--; j--;
        printf("%d\n",cal(i,j,min(k,60)));
    }
}


// be name khoda \\

// #define stream_enable
// #define bloat_enable
#define long_enable
#define debug_enable

#ifdef bloat_enable
#include <bits/stdc++.h>
#else
#include <iostream>
#include <map>
#include <iomanip>
#include <vector>
#include <set>
#include <cmath>
#include <utility>
#include <algorithm>
#include <sstream>
#include <queue>
#include <stack>
#include <limits.h>
#include <fstream>
#include <cstring>
#endif

using namespace std;

#ifdef stream_enable
#define cin sss
#endif

#ifdef long_enable
typedef long long int ll;
#else
typedef int ll;
#endif

typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef pair<ll, pii> pip;
typedef vector<vi> vii;
typedef vector<vii> viii;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef vector<pii> vpii;
typedef vector<pip> vpip;
typedef priority_queue<ll> maxHeap;
typedef priority_queue<ll, vi, greater<ll>> minHeap;

const ll MOD = 1000000007;

const long long BIG = 1446803456761533460LL;
const int Big = 336860180;

#ifdef long_enable
const ll INF = LONG_LONG_MAX;
#else
const ll INF = INT_MAX;
#endif

const vii adj4({{0, 1}, {0, -1}, {1, 0}, {-1, 0}});
const vii adj8({{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1}});

#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define pb push_back

#define print(x) cout << (x) << endl
#define printv(x) fori (j12345, (x).size()) { cout << (x)[j12345] << ' '; } cout << endl
#define printvv(x) fori (i123456, (x).size()) { fori (j123456, (x)[i123456].size()) { cout << (x)[i123456][j123456] << ' '; } cout << endl; }
#define printp(x) cout << "(" << x.ff << ", " << x.ss << ")" << endl
#define printvp(x) fori (i1234567, (x).size()) { cout << "(" << x[i1234567].ff << ", " << x[i1234567].ss << ")" << endl; }
#define printa(x, n) fori (ja12345, n) { cout << (x)[ja12345] << ' '; } cout << endl
#define printap(x, n) fori (ia1234567, n) { cout << "(" << x[ia1234567].ff << ", " << x[ia1234567].ss << ")" << endl; }
#define printaa(x, n, m) fori (iaa123456, n) { fori (jaa123456, m) { cout << (x)[iaa123456][jaa123456] << ' '; } cout << endl; }

#ifdef debug_enable
#define debug(x) cout << #x << " -> "; print(x)
#define debugv(x) cout << #x << " -> "; printv(x)
#define debugvv(x) cout << #x << " ->\n"; printvv(x)
#define debugp(x) cout << #x << " -> "; printp(x)
#define debugvp(x) cout << #x << " ->\n"; printvp(x)
#define debuga(x, n) cout << #x << " -> "; printa(x, n)
#define debugap(x, n) cout << #x << " ->\n"; printap(x, n)
#define debugaa(x, n, m) cout << #x << " ->\n"; printaa(x, n, m)
#else
#define debug(x)
#define debugv(x)
#define debugvv(x)
#define debugp(x)
#define debugvp(x)
#define debuga(x, n)
#define debugap(x, n)
#define debugaa(x, n, m)
#endif

#define fori(i, n) for(ll i = 0; i < (n); ++i)
#define forir(i, n) for(ll i = (n) - 1; i >= 0; --i)
#define forifrom(i, f, n) for(ll i = (f); i < (n); ++i)
#define forirto(i, n, f) for(ll i = (n); i >= (f); --i)

#define smin(a, b) a = min(a, (b))
#define smax(a, b) a = max(a, (b))

#define inp(x) ll x; cin >> x
#define inp2(x, y) ll x, y; cin >> x >> y
#define inp3(x, y, z) ll x, y, z; cin >> x >> y >> z
#define inp4(x, y, z, zz) ll x, y, z, zz; cin >> x >> y >> z >> zz
#define inpvi(x, size) vi x(size); fori(i123458, size) cin >> x[i123458]

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

bool isprime(ll n) {
    if (n == 2 || n == 3) return true;
    if (n < 2 || n % 2 == 0 || n % 3 == 0) return false;
    for (ll i = 6; (i - 1) * (i - 1) <= n; i += 6) {
        if (n % (i - 1) == 0 || n % (i + 1) == 0) {
            return false;
        }
    }
    return true;
}

vb sprime;
void genPrime(ll sz) {
    sprime = vb(sz, true);
    sprime[0] = false;
    sprime[1] = false;
    forifrom (i, 2, sz) {
        if (sprime[i]) {
            for (ll j = i * i; j < sz; j += i) {
                sprime[j] = false;
            }
        }
    }
}

ll powMod(ll a, ll b, ll mod) {
    ll n = 1;
    ll p = a;
    while (b > 0) {
        if (b % 2 == 1) {
            n *= p;
            n %= mod;
        }
        p *= p;
        p %= mod;
        b /= 2;
    }
    return n;
}

ll modularInverse(ll a, ll mod) {
    return powMod(a, mod - 2, mod);
}

ll binarysearch(ll l, ll r, bool (*bsfunction) (ll)) {
    while (r - l > 1) {
        ll mid = (l + r) / 2;
        bool val = bsfunction(mid);
        if (val) {
            r = mid;
        } else {
            l = mid;
        }
    }
    return l;
}

stringstream sss;

// -----------------------------------------------------------------------

const ll maxn = 61;
ll d[maxn][maxn][maxn];
ll dp[maxn][maxn][maxn];

void MAIN() {

    memset(d, 20, sizeof d);
    memset(dp, 20, sizeof dp);

    inp3(n, m, r);
    fori (i, m) {
        fori (j, n) {
            fori (k, n) {
                cin >> d[i][j][k];
            }
        }
    }

    fori (mm, m) {
        fori (k, n) {
            fori (i, n) {
                fori (j, n) {
                    smin(d[mm][i][j], d[mm][i][k] + d[mm][k][j]);
                }
            }
        }
    }

    fori (i, n) {
        fori (j, n) {
            fori (mm, m) {
                smin(dp[0][i][j], d[mm][i][j]);
            }
        }
    }

    forifrom (kk, 1, maxn) {
        fori (i, n) {
            fori (j, n) {
                fori (k, n) {
                    smin(dp[kk][i][j], dp[0][i][k] + dp[kk - 1][k][j]);
                }
            }
        }
    }

    fori (rr, r) {
        inp3(s, t, kk);
        print(dp[min(kk, maxn - 1)][s - 1][t - 1]);
    }

}

// -----------------------------------------------------------------------

#ifdef stream_enable
const ll TESTCASEN = 1;
#else
const ll TESTCASEN = 1;
#endif

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(10);
    sss << R"(
4 2 3
0 1 5 6
2 0 3 6
1 3 0 1
6 6 7 0
0 3 5 6
2 0 1 6
1 3 0 2
6 6 7 0
1 4 2
1 4 1
1 4 3
    )";
    fori (i, TESTCASEN) {
        MAIN();
    }
    return 0;
}


// be name khoda \\

// #define stream_enable
// #define bloat_enable
#define long_enable
#define debug_enable

#ifdef bloat_enable
#include <bits/stdc++.h>
#else
#include <iostream>
#include <map>
#include <iomanip>
#include <vector>
#include <set>
#include <cmath>
#include <utility>
#include <algorithm>
#include <sstream>
#include <queue>
#include <stack>
#include <limits.h>
#include <fstream>
#include <cstring>
#endif

using namespace std;

#ifdef stream_enable
#define cin sss
#endif

#ifdef long_enable
typedef long long int ll;
#else
typedef int ll;
#endif

typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef pair<ll, pii> pip;
typedef vector<vi> vii;
typedef vector<vii> viii;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef vector<pii> vpii;
typedef vector<pip> vpip;
typedef priority_queue<ll> maxHeap;
typedef priority_queue<ll, vi, greater<ll>> minHeap;

const ll MOD = 1000000007;

const long long BIG = 1446803456761533460LL;
const int Big = 336860180;

#ifdef long_enable
const ll INF = LONG_LONG_MAX;
#else
const ll INF = INT_MAX;
#endif

const vii adj4({{0, 1}, {0, -1}, {1, 0}, {-1, 0}});
const vii adj8({{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1}});

#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define pb push_back

#define print(x) cout << (x) << endl
#define printv(x) fori (j12345, (x).size()) { cout << (x)[j12345] << ' '; } cout << endl
#define printvv(x) fori (i123456, (x).size()) { fori (j123456, (x)[i123456].size()) { cout << (x)[i123456][j123456] << ' '; } cout << endl; }
#define printp(x) cout << "(" << x.ff << ", " << x.ss << ")" << endl
#define printvp(x) fori (i1234567, (x).size()) { cout << "(" << x[i1234567].ff << ", " << x[i1234567].ss << ")" << endl; }
#define printa(x, n) fori (ja12345, n) { cout << (x)[ja12345] << ' '; } cout << endl
#define printap(x, n) fori (ia1234567, n) { cout << "(" << x[ia1234567].ff << ", " << x[ia1234567].ss << ")" << endl; }
#define printaa(x, n, m) fori (iaa123456, n) { fori (jaa123456, m) { cout << (x)[iaa123456][jaa123456] << ' '; } cout << endl; }

#ifdef debug_enable
#define debug(x) cout << #x << " -> "; print(x)
#define debugv(x) cout << #x << " -> "; printv(x)
#define debugvv(x) cout << #x << " ->\n"; printvv(x)
#define debugp(x) cout << #x << " -> "; printp(x)
#define debugvp(x) cout << #x << " ->\n"; printvp(x)
#define debuga(x, n) cout << #x << " -> "; printa(x, n)
#define debugap(x, n) cout << #x << " ->\n"; printap(x, n)
#define debugaa(x, n, m) cout << #x << " ->\n"; printaa(x, n, m)
#else
#define debug(x)
#define debugv(x)
#define debugvv(x)
#define debugp(x)
#define debugvp(x)
#define debuga(x, n)
#define debugap(x, n)
#define debugaa(x, n, m)
#endif

#define fori(i, n) for(ll i = 0; i < (n); ++i)
#define forir(i, n) for(ll i = (n) - 1; i >= 0; --i)
#define forifrom(i, f, n) for(ll i = (f); i < (n); ++i)
#define forirto(i, n, f) for(ll i = (n); i >= (f); --i)

#define smin(a, b) a = min(a, (b))
#define smax(a, b) a = max(a, (b))

#define inp(x) ll x; cin >> x
#define inp2(x, y) ll x, y; cin >> x >> y
#define inp3(x, y, z) ll x, y, z; cin >> x >> y >> z
#define inp4(x, y, z, zz) ll x, y, z, zz; cin >> x >> y >> z >> zz
#define inpvi(x, size) vi x(size); fori(i123458, size) cin >> x[i123458]

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

bool isprime(ll n) {
    if (n == 2 || n == 3) return true;
    if (n < 2 || n % 2 == 0 || n % 3 == 0) return false;
    for (ll i = 6; (i - 1) * (i - 1) <= n; i += 6) {
        if (n % (i - 1) == 0 || n % (i + 1) == 0) {
            return false;
        }
    }
    return true;
}

vb sprime;
void genPrime(ll sz) {
    sprime = vb(sz, true);
    sprime[0] = false;
    sprime[1] = false;
    forifrom (i, 2, sz) {
        if (sprime[i]) {
            for (ll j = i * i; j < sz; j += i) {
                sprime[j] = false;
            }
        }
    }
}

ll powMod(ll a, ll b, ll mod) {
    ll n = 1;
    ll p = a;
    while (b > 0) {
        if (b % 2 == 1) {
            n *= p;
            n %= mod;
        }
        p *= p;
        p %= mod;
        b /= 2;
    }
    return n;
}

ll modularInverse(ll a, ll mod) {
    return powMod(a, mod - 2, mod);
}

ll binarysearch(ll l, ll r, bool (*bsfunction) (ll)) {
    while (r - l > 1) {
        ll mid = (l + r) / 2;
        bool val = bsfunction(mid);
        if (val) {
            r = mid;
        } else {
            l = mid;
        }
    }
    return l;
}

stringstream sss;

// -----------------------------------------------------------------------

const ll maxn = 61;
ll d[maxn][maxn][maxn];
ll dp[maxn][maxn][maxn];

void MAIN() {

    memset(d, 20, sizeof d);
    memset(dp, 20, sizeof dp);

    inp3(n, m, r);
    fori (i, m) {
        fori (j, n) {
            fori (k, n) {
                cin >> d[i][j][k];
            }
        }
    }

    fori (mm, m) {
        fori (k, n) {
            fori (i, n) {
                fori (j, n) {
                    smin(d[mm][i][j], d[mm][i][k] + d[mm][k][j]);
                }
            }
        }
    }

    fori (i, n) {
        fori (j, n) {
            fori (mm, m) {
                smin(dp[0][i][j], d[mm][i][j]);
            }
        }
    }

    forifrom (kk, 1, maxn) {
        fori (i, n) {
            fori (j, n) {
                fori (k, n) {
                    smin(dp[kk][i][j], dp[0][i][k] + dp[kk - 1][k][j]);
                }
            }
        }
    }

    fori (rr, r) {
        inp3(s, t, kk);
        print(dp[min(kk, maxn)][s - 1][t - 1]);
    }

}

// -----------------------------------------------------------------------

#ifdef stream_enable
const ll TESTCASEN = 1;
#else
const ll TESTCASEN = 1;
#endif

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(10);
    sss << R"(
4 2 3
0 1 5 6
2 0 3 6
1 3 0 1
6 6 7 0
0 3 5 6
2 0 1 6
1 3 0 2
6 6 7 0
1 4 2
1 4 1
1 4 3
    )";
    fori (i, TESTCASEN) {
        MAIN();
    }
    return 0;
}


// be name khoda \\

// #define stream_enable
// #define bloat_enable
#define long_enable
#define debug_enable

#ifdef bloat_enable
#include <bits/stdc++.h>
#else
#include <iostream>
#include <map>
#include <iomanip>
#include <vector>
#include <set>
#include <cmath>
#include <utility>
#include <algorithm>
#include <sstream>
#include <queue>
#include <stack>
#include <limits.h>
#include <fstream>
#include <cstring>
#endif

using namespace std;

#ifdef stream_enable
#define cin sss
#endif

#ifdef long_enable
typedef long long int ll;
#else
typedef int ll;
#endif

typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef pair<ll, pii> pip;
typedef vector<vi> vii;
typedef vector<vii> viii;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef vector<pii> vpii;
typedef vector<pip> vpip;
typedef priority_queue<ll> maxHeap;
typedef priority_queue<ll, vi, greater<ll>> minHeap;

const ll MOD = 1000000007;

const long long BIG = 1446803456761533460LL;
const int Big = 336860180;

#ifdef long_enable
const ll INF = LONG_LONG_MAX;
#else
const ll INF = INT_MAX;
#endif

const vii adj4({{0, 1}, {0, -1}, {1, 0}, {-1, 0}});
const vii adj8({{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1}});

#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define pb push_back

#define print(x) cout << (x) << endl
#define printv(x) fori (j12345, (x).size()) { cout << (x)[j12345] << ' '; } cout << endl
#define printvv(x) fori (i123456, (x).size()) { fori (j123456, (x)[i123456].size()) { cout << (x)[i123456][j123456] << ' '; } cout << endl; }
#define printp(x) cout << "(" << x.ff << ", " << x.ss << ")" << endl
#define printvp(x) fori (i1234567, (x).size()) { cout << "(" << x[i1234567].ff << ", " << x[i1234567].ss << ")" << endl; }
#define printa(x, n) fori (ja12345, n) { cout << (x)[ja12345] << ' '; } cout << endl
#define printap(x, n) fori (ia1234567, n) { cout << "(" << x[ia1234567].ff << ", " << x[ia1234567].ss << ")" << endl; }
#define printaa(x, n, m) fori (iaa123456, n) { fori (jaa123456, m) { cout << (x)[iaa123456][jaa123456] << ' '; } cout << endl; }

#ifdef debug_enable
#define debug(x) cout << #x << " -> "; print(x)
#define debugv(x) cout << #x << " -> "; printv(x)
#define debugvv(x) cout << #x << " ->\n"; printvv(x)
#define debugp(x) cout << #x << " -> "; printp(x)
#define debugvp(x) cout << #x << " ->\n"; printvp(x)
#define debuga(x, n) cout << #x << " -> "; printa(x, n)
#define debugap(x, n) cout << #x << " ->\n"; printap(x, n)
#define debugaa(x, n, m) cout << #x << " ->\n"; printaa(x, n, m)
#else
#define debug(x)
#define debugv(x)
#define debugvv(x)
#define debugp(x)
#define debugvp(x)
#define debuga(x, n)
#define debugap(x, n)
#define debugaa(x, n, m)
#endif

#define fori(i, n) for(ll i = 0; i < (n); ++i)
#define forir(i, n) for(ll i = (n) - 1; i >= 0; --i)
#define forifrom(i, f, n) for(ll i = (f); i < (n); ++i)
#define forirto(i, n, f) for(ll i = (n); i >= (f); --i)

#define smin(a, b) a = min(a, (b))
#define smax(a, b) a = max(a, (b))

#define inp(x) ll x; cin >> x
#define inp2(x, y) ll x, y; cin >> x >> y
#define inp3(x, y, z) ll x, y, z; cin >> x >> y >> z
#define inp4(x, y, z, zz) ll x, y, z, zz; cin >> x >> y >> z >> zz
#define inpvi(x, size) vi x(size); fori(i123458, size) cin >> x[i123458]

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

bool isprime(ll n) {
    if (n == 2 || n == 3) return true;
    if (n < 2 || n % 2 == 0 || n % 3 == 0) return false;
    for (ll i = 6; (i - 1) * (i - 1) <= n; i += 6) {
        if (n % (i - 1) == 0 || n % (i + 1) == 0) {
            return false;
        }
    }
    return true;
}

vb sprime;
void genPrime(ll sz) {
    sprime = vb(sz, true);
    sprime[0] = false;
    sprime[1] = false;
    forifrom (i, 2, sz) {
        if (sprime[i]) {
            for (ll j = i * i; j < sz; j += i) {
                sprime[j] = false;
            }
        }
    }
}

ll powMod(ll a, ll b, ll mod) {
    ll n = 1;
    ll p = a;
    while (b > 0) {
        if (b % 2 == 1) {
            n *= p;
            n %= mod;
        }
        p *= p;
        p %= mod;
        b /= 2;
    }
    return n;
}

ll modularInverse(ll a, ll mod) {
    return powMod(a, mod - 2, mod);
}

ll binarysearch(ll l, ll r, bool (*bsfunction) (ll)) {
    while (r - l > 1) {
        ll mid = (l + r) / 2;
        bool val = bsfunction(mid);
        if (val) {
            r = mid;
        } else {
            l = mid;
        }
    }
    return l;
}

stringstream sss;

// -----------------------------------------------------------------------

const ll maxn = 60;
const ll maxk = 1001;
ll d[maxn][maxn][maxn];
ll dp[maxk][maxn][maxn];

void MAIN() {

    memset(d, 20, sizeof d);
    memset(dp, 20, sizeof dp);

    inp3(n, m, r);
    fori (i, m) {
        fori (j, n) {
            fori (k, n) {
                cin >> d[i][j][k];
            }
        }
    }

    fori (mm, m) {
        fori (k, n) {
            fori (i, n) {
                fori (j, n) {
                    smin(d[mm][i][j], d[mm][i][k] + d[mm][k][j]);
                }
            }
        }
    }

    fori (i, n) {
        fori (j, n) {
            fori (mm, m) {
                smin(dp[0][i][j], d[mm][i][j]);
            }
        }
    }

    forifrom (kk, 1, maxk) {
        fori (i, n) {
            fori (j, n) {
                fori (k, n) {
                    smin(dp[kk][i][j], dp[0][i][k] + dp[kk - 1][k][j]);
                }
            }
        }
    }

    fori (rr, r) {
        inp3(s, t, kk);
        --s, --t;
        print(dp[kk][s][t]);
    }

}

// -----------------------------------------------------------------------

#ifdef stream_enable
const ll TESTCASEN = 1;
#else
const ll TESTCASEN = 1;
#endif

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(10);
    sss << R"(
4 2 3
0 1 5 6
2 0 3 6
1 3 0 1
6 6 7 0
0 3 5 6
2 0 1 6
1 3 0 2
6 6 7 0
1 4 2
1 4 1
1 4 3
    )";
    fori (i, TESTCASEN) {
        MAIN();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define mod 1000000007
#define all(v)  v.begin(),v.end()
#define pb push_back
#define size(v)  (int) v.size()
#define fast ios_base:: sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL)
ll power_mod(ll a,ll x){if(x==0) return 1;ll y=power_mod(a,x/2);ll ans=(y*y)%mod; if(x%2) ans=(ans*a)%mod;return ans;}
ll inv(ll a){return power_mod(a,mod-2);}
ll power(ll a, ll x){ if(x==0) return 1; ll y= power(a,x/2); ll ans=(y*y); if(x%2) ans*=a; return ans;}

int main() {

    int n,m,r;
    cin >> n >> m >> r;
    int cars[m+1][n+1][n+1];
    for(int i = 1; i <= m; i++) {
        for(int j = 1; j <= n; j++) {
            for(int k = 1; k <= n; k++) {
                cin >> cars[i][j][k];
            }
        }
    }

    ll dp[n+1][n+1][n+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] = INT_MAX;

    for(int c = 1; c <= m; c++) {
        ll dp1[n+1][n+1];
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                dp1[i][j] = cars[c][i][j];
            }
        }
        for(int k = 1; k <= n; k++) {
            for(int i = 1; i <= n; i++) {
                for(int j = 1;j <= n; j++) {
                    dp1[i][j] = min(dp1[i][j],dp1[i][k] + dp1[k][j]);
                    dp[0][i][j] = min(dp1[i][j],dp[0][i][j]);
                }
            }
        }

        // for(int i = 1; i <= n; i++) {
        //     for(int j = 1; j <= n; j++) {
        //         cout << dp[0][i][j] <<" ";
        //     }
        //     cout << endl;
        // }

    }

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

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

}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define mod 1000000007
#define all(v)  v.begin(),v.end()
#define pb push_back
#define size(v)  (int) v.size()
#define fast ios_base:: sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL)
ll power_mod(ll a,ll x){if(x==0) return 1;ll y=power_mod(a,x/2);ll ans=(y*y)%mod; if(x%2) ans=(ans*a)%mod;return ans;}
ll inv(ll a){return power_mod(a,mod-2);}
ll power(ll a, ll x){ if(x==0) return 1; ll y= power(a,x/2); ll ans=(y*y); if(x%2) ans*=a; return ans;}

int main() {

    int n,m,r;
    cin >> n >> m >> r;
    int cars[m+1][n+1][n+1];
    for(int i = 1; i <= m; i++) {
        for(int j = 1; j <= n; j++) {
            for(int k = 1; k <= n; k++) {
                cin >> cars[i][j][k];
            }
        }
    }

    ll dp[n+1][n+1][n+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] = INT_MAX;

    for(int c = 1; c <= m; c++) {
        ll dp1[n+1][n+1];
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                dp1[i][j] = cars[c][i][j];
            }
        }
        for(int k = 1; k <= n; k++) {
            for(int i = 1; i <= n; i++) {
                for(int j = 1;j <= n; j++) {
                    dp1[i][j] = min(dp1[i][j],dp1[i][k] + dp1[k][j]);
                    dp[0][i][j] = min(dp1[i][j],dp[0][i][j]);
                }
            }
        }

        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                cout << dp[0][i][j] <<" ";
            }
            cout << endl;
        }

    }

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

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

}