#include <bits/stdc++.h>
#define int long long

using namespace std;

const int N = 65, inf = 1e15;
int dis[N][N][N], dp[N][N][N];

int32_t main()
{
    int n, m, q; cin >> n >> m >> q;
    memset (dp, 31, sizeof dp);
    for (int a = 1; a <= m; a ++) for (int i = 1; i <= n; i ++) for (int j = 1; j <= n; j ++) cin >> dis[a][i][j];
    for (int a = 1; a <= m; a ++) for (int k = 1; k <= n; k ++) for (int i = 1; i <= n; i ++) for (int j = 1; j <= n; j ++) dis[a][i][j] = min(dis[a][i][j], dis[a][i][k] + dis[a][k][j]);
    for (int a = 1; a <= m; a ++) for (int i = 1; i <= n; i ++) for (int j = 1; j <= n; j ++) dp[0][i][j] = min(dp[0][i][j], dis[a][i][j]);
    for (int t = 1; t <= n; t ++)
        for (int k = 1; k <= n; k ++)
            for (int i = 1; i <= n; i ++)
                for (int j = 1; j <= n; j ++)
                        dp[t][i][j] = min(dp[t][i][j], dp[t - 1][i][k] + dp[0][k][j]);
    while (q --) {
        int u, v, t; cin >> u >> v >> t;
        t = min(n, t);
        cout << dp[t][u][v] << "\n";
    }
}


#include <bits/stdc++.h>
#define int long long

using namespace std;

const int N = 65, inf = 1e15;
int dis[N][N][N], dp[N][N][N];

int32_t main()
{
    int n, m, q; cin >> n >> m >> q;
    memset (dp, 31, sizeof dp);
    for (int a = 1; a <= m; a ++) for (int i = 1; i <= n; i ++) for (int j = 1; j <= n; j ++) {int x; cin >> x; dis[a][i][j] = (x != 0 ? x : inf);}
    for (int a = 1; a <= m; a ++) for (int i = 1; i <= n; i ++) dis[a][i][i] = 0;
    for (int a = 1; a <= m; a ++) for (int k = 1; k <= n; k ++) for (int i = 1; i <= n; i ++) for (int j = 1; j <= n; j ++) dis[a][i][j] = min(dis[a][i][j], dis[a][i][k] + dis[a][k][j]);
    for (int a = 1; a <= m; a ++) for (int i = 1; i <= n; i ++) for (int j = 1; j <= n; j ++) dp[0][i][j] = min(dp[0][i][j], dis[a][i][j]);
    for (int t = 1; t <= n; t ++)
        for (int k = 1; k <= n; k ++)
            for (int i = 1; i <= n; i ++)
                for (int j = 1; j <= n; j ++)
                        dp[t][i][j] = min(dp[t][i][j], dp[t - 1][i][k] + dp[0][k][j]);
    while (q --) {
        int u, v, t; cin >> u >> v >> t;
        t = min(n, t);
        cout << dp[t][u][v] << "\n";
    }
}


#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")
#define int long long

using namespace std;

const int N = 65, inf = 1e15;
int dis[N][N][N], dp[N][N][N];

int32_t main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int n, m, q; cin >> n >> m >> q;
    memset (dp, 31, sizeof dp);
    for (int a = 1; a <= m; a ++) for (int i = 1; i <= n; i ++) for (int j = 1; j <= n; j ++) cin >> dis[a][i][j];
    for (int a = 1; a <= m; a ++) for (int k = 1; k <= n; k ++) for (int i = 1; i <= n; i ++) for (int j = 1; j <= n; j ++) dis[a][i][j] = min(dis[a][i][j], dis[a][i][k] + dis[a][k][j]);
    for (int a = 1; a <= m; a ++) for (int i = 1; i <= n; i ++) for (int j = 1; j <= n; j ++) dp[0][i][j] = min(dp[0][i][j], dis[a][i][j]);
    for (int t = 1; t <= n; t ++)
        for (int k = 1; k <= n; k ++)
            for (int i = 1; i <= n; i ++)
                for (int j = 1; j <= n; j ++)
                        dp[t][i][j] = min(dp[t][i][j], dp[t - 1][i][k] + dp[0][k][j]);
    while (q --) {
        int u, v, t; cin >> u >> v >> t;
        t = min(n, t);
        cout << dp[t][u][v] << "\n";
    }
}


#include <bits/stdc++.h>

using namespace std;

const int N = 65, oo = 1e9;
int n, m, r, f[N][N][N], g[N][N], ans[(int)1e5 + 7], dp[N][N], temp[N][N];

struct Query {
	int l, r, id, k;
};
vector <Query> q[1005];

int main(int argc, char const *argv[])
{
	ios_base::sync_with_stdio(0); cin.tie(0);

	cin >> n >> m >> r;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			g[i][j] = oo;
		}
	}
	for (int i = 1; i <= m; ++i) {
		for (int j = 1; j <= n; ++j) {
			for (int k = 1; k <= n; ++k) {
				cin >> f[i][j][k];
			}
		}
		for (int mid = 1; mid <= n; ++mid) {
			for (int j = 1; j <= n; ++j) {
				for (int k = 1; k <= n; ++k) {
					f[i][j][k] = min(f[i][j][k], f[i][j][mid] + f[i][mid][k]);
				}
			}
		}
		for (int j = 1; j <= n; ++j) {
			for (int k = 1; k <= n; ++k) {
				g[j][k] = min(g[j][k], f[i][j][k]);
			}
		}
	}
	for (int i = 1; i <= r; ++i) {
		Query qr;
		cin >> qr.l >> qr.r >> qr.k;
		qr.id = i;
		q[qr.k].push_back(qr);
	}
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) dp[i][j] = g[i][j];
	}
	for (Query qr : q[0]) {
		ans[qr.id] = g[qr.l][qr.r];
	}
	for (int turn = 1; turn <= 1000; ++turn) {
		for (int i = 1; i <= n; ++i) {
			for (int j = 1; j <= n; ++j)
				temp[i][j] = dp[i][j];
		}
		for (int i = 1; i <= n; ++i) {
			for (int j = 1; j <= n; ++j) {
				for (int k = 1; k <= n; ++k) {
					temp[i][j] = min(temp[i][j], dp[i][k] + g[k][j]);
				}
			}
		}
		for (int i = 1; i <= n; ++i) {
			for (int j = 1; j <= n; ++j)
				dp[i][j] = temp[i][j];
		}
		for (Query qr : q[turn]) {
			ans[qr.id] = dp[qr.l][qr.r];
		}
	}
	for (int i = 1; i <= r; ++i) {
		cout << ans[i] << endl;
	}

	return 0;
}


#define _USE_MATH_DEFINES
//#pragma GCC optimize("O3")
// #pragma GCC target("sse4")

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

#define LSB(i) ((i) & -(i)) // zeroes all the bits except the least significant one

#define OPEN(a) freopen(a, "r", stdin)
#define F first
#define S second
#define PB push_back
#define BOOST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ALL(i) begin((i)), end((i))
#define SZ(i) ((int)i.size())
#define SORT(a) sort(ALL(a))

using LL = long long;
using LD = long double;
using PII = pair<int, int>;
using PLL = pair<LL, LL>;
using PLD = pair<LD, LD>;
using VI = vector<int>;
using VLL = vector<LL>;
using VLD = vector<LD>;
using VPII = vector<PII>;
using VPLL = vector<PLL>;
using VPLD = vector<PLD>;
using VVI = vector<VI>;

#define GET_MACRO(_1, _2, _3, _4, NAME, ...) NAME
#define __FOR3(i, a, n, inc) for(int i = (a); (inc) > 0 ? i < (n) : i >= (n); i += (inc))
#define __FOR2(i, a, n) __FOR3(i, a, n, 1)
#define __FOR1(i, n) __FOR2(i, 0, n)
#define FOR(...) GET_MACRO(__VA_ARGS__, __FOR3, __FOR2, __FOR1)(__VA_ARGS__)

#define REV(a,b) for(int a= b; a >= 0; --a)
#define FRU(a,b) for(const auto& a: b)
#define FRUM(a,b) for(auto& a : b)

const int inf = 1e9 + 7;
const int MOD = 1e9 + 7;
const LL INF = 1e18 + 7;
const long double PI = acos(-1);
const LD EPS = 1e-9;

namespace input {
	template< class T> istream& operator>>(istream& st, vector<T> & container) { for (auto& u : container) st >> u; return st; }
	template< class T, size_t N> istream& operator>>(istream& st, array<T, N> & container) { for (auto& u : container) st >> u; return st; }
	template <class T, class U> istream& operator>>(istream& st, pair<T, U> & p) { st >> p.first >> p.second; return st; }

	void re() {}
	template<typename T, typename... TArgs> void re(T& x, TArgs&... rest) { cin >> x; re(rest...); };
}
using namespace input;

#define RE(...)   int __VA_ARGS__; re(__VA_ARGS__);
#define RELL(...) LL __VA_ARGS__; re(__VA_ARGS__);
#define RELD(...) LD __VA_ARGS__; re(__VA_ARGS__);

namespace output
{
	template< class T> ostream& operator<<(ostream& st, const vector<T> & container) { for (auto& u : container) st << u << ' '; return st; }
	template< class T, size_t N> ostream& operator<<(ostream& st, const array<T, N> & container) { for (auto& u : container) st << u << ' '; return st; }
	template <class T, class U> ostream& operator<<(ostream& st, pair<T, U> p) { st << p.first << ' ' << p.second; return st; }

	void pr() {}
	template <typename T> void pr(const T& x) { cout << x; }
	template <typename T, typename... TArgs> void pr(const T& x, const TArgs&... rest) { cout << x << ' '; pr(rest...); }
	template <typename... TArgs> void prln(const TArgs&... args) { pr(args...); cout << '\n'; }
}
using namespace output;

namespace pairs
{
	template<class T, class U, class V> pair<T, U> operator* (pair<T, U>p, V val) { return{ p.first * val, p.second * val }; }
	template<class T, class U, class V> pair<T, U> operator/ (pair<T, U>p, V val) { return{ p.first / val, p.second / val }; }
	template<class T, class U>  pair<T, U> operator- (pair<T, U> a, pair<T, U> b) { return{ a.first - b.first, a.second - b.second }; }
	template<class T, class U>  pair<T, U> operator+ (pair<T, U> a, pair<T, U> b) { return{ a.first + b.first, a.second + b.second }; }
}
using namespace pairs;

namespace triples
{
#define TT1T2T3 template<class T1, class T2, class T3>
#define TT1T2T3T4 template<class T1, class T2, class T3, class T4>
#define TRT1T2T3  triple<T1, T2, T3>
	TT1T2T3 struct triple { T1 x; T2 y; T3 z; triple() : x(T1()), y(T2()), z(T3()) {}; triple(T1 _x, T2 _y, T3 _z) :x(_x), y(_y), z(_z) {} };
	TT1T2T3 bool operator<(const TRT1T2T3&t1, const TRT1T2T3&t2) { if (t1.x != t2.x)return t1.x < t2.x; if (t1.y != t2.y) return t1.y < t2.y; else return t1.z < t2.z; }
	TT1T2T3 bool operator>(const TRT1T2T3&t1, const TRT1T2T3&t2) { if (t1.x != t2.x)return t1.x > t2.x; if (t1.y != t2.y) return t1.y > t2.y; else return t1.z > t2.z; }
	TT1T2T3 bool operator==(const TRT1T2T3&t1, const TRT1T2T3&t2) { return (t1.x == t2.x && t1.y == t2.y && t1.z == t2.z); }
	TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t) { return os >> t.x >> t.y >> t.y; }
	TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t) { return os << t.x << " " << t.y << " " << t.z; }

	TT1T2T3 TRT1T2T3 operator+(TRT1T2T3 a, TRT1T2T3 b) { return { a.x + b.x, a.y + b.y, a.z + b.z }; }
	TT1T2T3 TRT1T2T3 operator-(TRT1T2T3 a, TRT1T2T3 b) { return { a.x - b.x, a.y - b.y, a.z - b.z }; }
	TT1T2T3T4 TRT1T2T3 operator*(TRT1T2T3 a, T4 val) { return { a.x * val, a.y * val, a.z * val }; }
	TT1T2T3T4 TRT1T2T3 operator/(TRT1T2T3 a, T4 val) { return { a.x / val, a.y / val, a.z / val }; }

#undef TT1T2T3T4
#undef TRT1T2T3
#undef TT1T2T3
	using TRII = triple<int, int, int>;
	using TRLL = triple<LL, LL, LL>;
	using TRLD = triple<LD, LD, LD>;
	using VTRII = vector<TRII>;
	using VTRLL = vector<TRLL>;
	using VTRLD = vector<TRLD>;
}
using namespace triples;

namespace geo
{
	template<class T> T dotProduct(pair<T, T> a, pair<T, T> b) { return a.first*b.first + a.second* b.second; }
	template<class T> T crossProduct(pair<T, T>a, pair<T, T> b) { return a.first * b.second - a.second * b.first; }
	template<class T> T lengthPow(pair<T, T> a) { return a.first * 1ll * a.first + a.second * 1ll * a.second; }
	template<class T> LD length(pair<T, T> a) { return sqrt(lengthPow(a)); }

	template<class T> T dotProduct(triple<T, T, T> a, triple<T, T, T> b) { return a.x*b.x + a.y* b.y + a.z*b.z; }
	template<class T> T crossProduct(triple<T, T, T> a, triple<T, T, T> b) { return { a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x }; }
	template<class T> T lengthPow(triple<T, T, T> a) { return a.x * 1ll * a.x + a.y * 1ll * a.y + a.z * 1ll * a.z; }
	template<class T> LD length(triple<T, T, T> a) { return sqrt(lengthPow(a)); }
}
using namespace geo;

template <class T> T invGeneral(T a, T b) { // 0 < a < b, gcd(a,b) = 1
	a %= b;
	if (a == 0) return b == 1 ? 0 : -1;
	T x = invGeneral(b, a);
	return x == -1 ? -1 : ((1 - (LL)b * x) / a + b) % b;
}

template<class T> struct modular {
	T val;
	explicit operator T() const { return val; }
	modular() { val = 0; }
	modular(const LL& v) {
		val = (-MOD <= v && v <= MOD) ? v : v % MOD;
		if (val < 0) val += MOD;
	}

	friend ostream& operator<<(ostream& os, const modular& a) { return os << a.val; }
	friend void pr(const modular& a) { pr(a.val); }
	friend void re(modular& a) { LL x; re(x); a = modular(x); }

	friend bool operator==(const modular& a, const modular& b) { return a.val == b.val; }
	friend bool operator!=(const modular& a, const modular& b) { return !(a == b); }
	friend bool operator<(const modular& a, const modular& b) { return a.val < b.val; }

	modular operator-() const { return modular(-val); }
	modular& operator+=(const modular& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; }
	modular& operator-=(const modular& m) { if ((val -= m.val) < 0) val += MOD; return *this; }
	modular& operator*=(const modular& m) { val = (LL)val*m.val%MOD; return *this; }
	friend modular pow(modular a, LL p) {
		modular ans = 1; for (; p; p /= 2, a *= a) if (p & 1) ans *= a;
		return ans;
	}
	friend modular inv(const modular& a) {
		auto i = invGeneral(a.val, MOD); assert(i != -1);
		return i;
	} // equivalent to return exp(b,MOD-2) if MOD is prime
	modular& operator/=(const modular& m) { return (*this) *= inv(m); }

	friend modular operator+(modular a, const modular& b) { return a += b; }
	friend modular operator-(modular a, const modular& b) { return a -= b; }
	friend modular operator*(modular a, const modular& b) { return a *= b; }
	friend modular operator/(modular a, const modular& b) { return a /= b; }
};

using MI = modular<int>;
using PMI = pair<MI, MI>;
using VMI = vector<MI>;
using VPMI = vector<PMI>;

namespace debug {
	template < typename _T > inline void _debug(const char *s, _T x) { cerr << s << " = " << x << "\n"; }
	template < typename _T, typename... args > void _debug(const char *s, _T x, args... a) { while (*s != ',') cerr << *s++; cerr << " = " << x << ','; _debug(s + 1, a...); }

#if 1  && defined(LOCAL)
#define debug(...) _debug(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) 1999
#define cerr if(0) cout
#endif
}
using namespace debug;

template <class T> bool setMax(T& v, T newV) { if (v < newV) { v = newV; return true; } return false; }
template <class T> bool setMin(T& v, T newV) { if (v > newV) { v = newV; return true; } return false; }
LL sq(LL a) { return a * a; }

const int N = 65;
const int K = 70;
const int Q = 1e5 + 5;

LL dp[N][N];
LL dp2[N][N];
LL dist[N][N][N];
LL mini[N][N];

vector<TRII > queries[K];
int res[Q];
void solve()
{
	int n, m, q;
	re(n, m, q);

	FOR(i, m)
	{
		FOR(j, n)
		{
			FOR(k, n)
			{
				re(dist[i][j][k]);
			}
		}

		FOR(mid, n) FOR(a, n) FOR(b, n)
			setMin(dist[i][a][b], dist[i][a][mid] + dist[i][mid][b]);
	}

	FOR(a, n)FOR(b, n)
		dp[a][b] = INF;

	FOR(s, m)FOR(a, n)FOR(b, n)
		setMin(dp[a][b], dist[s][a][b]);

	FOR(a, n)
	{
		FOR(b, n)
		{
			mini[a][b] = dp[a][b];
		}
	}

	FOR(i, q)
	{
		int a, b, r;
		re(a, b, r);
		--a, --b;
		if (r >= K)
			r = K - 3;

		queries[r].push_back({ a,b,i });
	}

	for (int k = 1; k < K; ++k)
	{
		debug(queries[k - 1]);
		for (auto u : queries[k - 1])
		{
			auto[a, b, i] = u;
			res[i] = dp[a][b];
		}

		FOR(a, n) FOR(b, n)
			dp2[a][b] = INF;
		FOR(mid, n) FOR(a, n) FOR(b, n)
			setMin(dp2[a][b], dp[a][mid] + mini[mid][b]);

		FOR(a, n) FOR(b, n)
			dp[a][b] = dp2[a][b];

	}

	FOR(i, q)
		prln(res[i]);

}

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

	BOOST;
	cout << fixed << setprecision(50);
	int t = 1;
	// cin >> t;
	while (t--)
	{
		solve();
	}
	return 0;
}


#define _USE_MATH_DEFINES
//#pragma GCC optimize("O3")
// #pragma GCC target("sse4")

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

#define LSB(i) ((i) & -(i)) // zeroes all the bits except the least significant one

#define OPEN(a) freopen(a, "r", stdin)
#define F first
#define S second
#define PB push_back
#define BOOST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ALL(i) begin((i)), end((i))
#define SZ(i) ((int)i.size())
#define SORT(a) sort(ALL(a))

using LL = long long;
using LD = long double;
using PII = pair<int, int>;
using PLL = pair<LL, LL>;
using PLD = pair<LD, LD>;
using VI = vector<int>;
using VLL = vector<LL>;
using VLD = vector<LD>;
using VPII = vector<PII>;
using VPLL = vector<PLL>;
using VPLD = vector<PLD>;
using VVI = vector<VI>;

#define GET_MACRO(_1, _2, _3, _4, NAME, ...) NAME
#define __FOR3(i, a, n, inc) for(int i = (a); (inc) > 0 ? i < (n) : i >= (n); i += (inc))
#define __FOR2(i, a, n) __FOR3(i, a, n, 1)
#define __FOR1(i, n) __FOR2(i, 0, n)
#define FOR(...) GET_MACRO(__VA_ARGS__, __FOR3, __FOR2, __FOR1)(__VA_ARGS__)

#define REV(a,b) for(int a= b; a >= 0; --a)
#define FRU(a,b) for(const auto& a: b)
#define FRUM(a,b) for(auto& a : b)

const int inf = 1e9 + 7;
const int MOD = 1e9 + 7;
const LL INF = 1e18 + 7;
const long double PI = acos(-1);
const LD EPS = 1e-9;

namespace input {
	template< class T> istream& operator>>(istream& st, vector<T> & container) { for (auto& u : container) st >> u; return st; }
	template< class T, size_t N> istream& operator>>(istream& st, array<T, N> & container) { for (auto& u : container) st >> u; return st; }
	template <class T, class U> istream& operator>>(istream& st, pair<T, U> & p) { st >> p.first >> p.second; return st; }

	void re() {}
	template<typename T, typename... TArgs> void re(T& x, TArgs&... rest) { cin >> x; re(rest...); };
}
using namespace input;

#define RE(...)   int __VA_ARGS__; re(__VA_ARGS__);
#define RELL(...) LL __VA_ARGS__; re(__VA_ARGS__);
#define RELD(...) LD __VA_ARGS__; re(__VA_ARGS__);

namespace output
{
	template< class T> ostream& operator<<(ostream& st, const vector<T> & container) { for (auto& u : container) st << u << ' '; return st; }
	template< class T, size_t N> ostream& operator<<(ostream& st, const array<T, N> & container) { for (auto& u : container) st << u << ' '; return st; }
	template <class T, class U> ostream& operator<<(ostream& st, pair<T, U> p) { st << p.first << ' ' << p.second; return st; }

	void pr() {}
	template <typename T> void pr(const T& x) { cout << x; }
	template <typename T, typename... TArgs> void pr(const T& x, const TArgs&... rest) { cout << x << ' '; pr(rest...); }
	template <typename... TArgs> void prln(const TArgs&... args) { pr(args...); cout << '\n'; }
}
using namespace output;

namespace pairs
{
	template<class T, class U, class V> pair<T, U> operator* (pair<T, U>p, V val) { return{ p.first * val, p.second * val }; }
	template<class T, class U, class V> pair<T, U> operator/ (pair<T, U>p, V val) { return{ p.first / val, p.second / val }; }
	template<class T, class U>  pair<T, U> operator- (pair<T, U> a, pair<T, U> b) { return{ a.first - b.first, a.second - b.second }; }
	template<class T, class U>  pair<T, U> operator+ (pair<T, U> a, pair<T, U> b) { return{ a.first + b.first, a.second + b.second }; }
}
using namespace pairs;

namespace triples
{
#define TT1T2T3 template<class T1, class T2, class T3>
#define TT1T2T3T4 template<class T1, class T2, class T3, class T4>
#define TRT1T2T3  triple<T1, T2, T3>
	TT1T2T3 struct triple { T1 x; T2 y; T3 z; triple() : x(T1()), y(T2()), z(T3()) {}; triple(T1 _x, T2 _y, T3 _z) :x(_x), y(_y), z(_z) {} };
	TT1T2T3 bool operator<(const TRT1T2T3&t1, const TRT1T2T3&t2) { if (t1.x != t2.x)return t1.x < t2.x; if (t1.y != t2.y) return t1.y < t2.y; else return t1.z < t2.z; }
	TT1T2T3 bool operator>(const TRT1T2T3&t1, const TRT1T2T3&t2) { if (t1.x != t2.x)return t1.x > t2.x; if (t1.y != t2.y) return t1.y > t2.y; else return t1.z > t2.z; }
	TT1T2T3 bool operator==(const TRT1T2T3&t1, const TRT1T2T3&t2) { return (t1.x == t2.x && t1.y == t2.y && t1.z == t2.z); }
	TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t) { return os >> t.x >> t.y >> t.y; }
	TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t) { return os << t.x << " " << t.y << " " << t.z; }

	TT1T2T3 TRT1T2T3 operator+(TRT1T2T3 a, TRT1T2T3 b) { return { a.x + b.x, a.y + b.y, a.z + b.z }; }
	TT1T2T3 TRT1T2T3 operator-(TRT1T2T3 a, TRT1T2T3 b) { return { a.x - b.x, a.y - b.y, a.z - b.z }; }
	TT1T2T3T4 TRT1T2T3 operator*(TRT1T2T3 a, T4 val) { return { a.x * val, a.y * val, a.z * val }; }
	TT1T2T3T4 TRT1T2T3 operator/(TRT1T2T3 a, T4 val) { return { a.x / val, a.y / val, a.z / val }; }

#undef TT1T2T3T4
#undef TRT1T2T3
#undef TT1T2T3
	using TRII = triple<int, int, int>;
	using TRLL = triple<LL, LL, LL>;
	using TRLD = triple<LD, LD, LD>;
	using VTRII = vector<TRII>;
	using VTRLL = vector<TRLL>;
	using VTRLD = vector<TRLD>;
}
using namespace triples;

namespace geo
{
	template<class T> T dotProduct(pair<T, T> a, pair<T, T> b) { return a.first*b.first + a.second* b.second; }
	template<class T> T crossProduct(pair<T, T>a, pair<T, T> b) { return a.first * b.second - a.second * b.first; }
	template<class T> T lengthPow(pair<T, T> a) { return a.first * 1ll * a.first + a.second * 1ll * a.second; }
	template<class T> LD length(pair<T, T> a) { return sqrt(lengthPow(a)); }

	template<class T> T dotProduct(triple<T, T, T> a, triple<T, T, T> b) { return a.x*b.x + a.y* b.y + a.z*b.z; }
	template<class T> T crossProduct(triple<T, T, T> a, triple<T, T, T> b) { return { a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x }; }
	template<class T> T lengthPow(triple<T, T, T> a) { return a.x * 1ll * a.x + a.y * 1ll * a.y + a.z * 1ll * a.z; }
	template<class T> LD length(triple<T, T, T> a) { return sqrt(lengthPow(a)); }
}
using namespace geo;

template <class T> T invGeneral(T a, T b) { // 0 < a < b, gcd(a,b) = 1
	a %= b;
	if (a == 0) return b == 1 ? 0 : -1;
	T x = invGeneral(b, a);
	return x == -1 ? -1 : ((1 - (LL)b * x) / a + b) % b;
}

template<class T> struct modular {
	T val;
	explicit operator T() const { return val; }
	modular() { val = 0; }
	modular(const LL& v) {
		val = (-MOD <= v && v <= MOD) ? v : v % MOD;
		if (val < 0) val += MOD;
	}

	friend ostream& operator<<(ostream& os, const modular& a) { return os << a.val; }
	friend void pr(const modular& a) { pr(a.val); }
	friend void re(modular& a) { LL x; re(x); a = modular(x); }

	friend bool operator==(const modular& a, const modular& b) { return a.val == b.val; }
	friend bool operator!=(const modular& a, const modular& b) { return !(a == b); }
	friend bool operator<(const modular& a, const modular& b) { return a.val < b.val; }

	modular operator-() const { return modular(-val); }
	modular& operator+=(const modular& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; }
	modular& operator-=(const modular& m) { if ((val -= m.val) < 0) val += MOD; return *this; }
	modular& operator*=(const modular& m) { val = (LL)val*m.val%MOD; return *this; }
	friend modular pow(modular a, LL p) {
		modular ans = 1; for (; p; p /= 2, a *= a) if (p & 1) ans *= a;
		return ans;
	}
	friend modular inv(const modular& a) {
		auto i = invGeneral(a.val, MOD); assert(i != -1);
		return i;
	} // equivalent to return exp(b,MOD-2) if MOD is prime
	modular& operator/=(const modular& m) { return (*this) *= inv(m); }

	friend modular operator+(modular a, const modular& b) { return a += b; }
	friend modular operator-(modular a, const modular& b) { return a -= b; }
	friend modular operator*(modular a, const modular& b) { return a *= b; }
	friend modular operator/(modular a, const modular& b) { return a /= b; }
};

using MI = modular<int>;
using PMI = pair<MI, MI>;
using VMI = vector<MI>;
using VPMI = vector<PMI>;

namespace debug {
	template < typename _T > inline void _debug(const char *s, _T x) { cerr << s << " = " << x << "\n"; }
	template < typename _T, typename... args > void _debug(const char *s, _T x, args... a) { while (*s != ',') cerr << *s++; cerr << " = " << x << ','; _debug(s + 1, a...); }

#if 1  && defined(LOCAL)
#define debug(...) _debug(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) 1999
#define cerr if(0) cout
#endif
}
using namespace debug;

template <class T> bool setMax(T& v, T newV) { if (v < newV) { v = newV; return true; } return false; }
template <class T> bool setMin(T& v, T newV) { if (v > newV) { v = newV; return true; } return false; }
LL sq(LL a) { return a * a; }

const int N = 65;
const int K = 73;
const int Q = 1e5 + 5;

LL dp[N][N];
LL dp2[N][N];
LL dist[N][N][N];
LL mini[N][N];

vector<TRII > queries[K];
int res[Q];
void solve()
{
	int n, m, q;
	re(n, m, q);

	FOR(i, m)
	{
		FOR(j, n)
		{
			FOR(k, n)
			{
				re(dist[i][j][k]);
			}
		}

		FOR(mid, n) FOR(a, n) FOR(b, n)
			setMin(dist[i][a][b], dist[i][a][mid] + dist[i][mid][b]);
	}

	FOR(a, n)FOR(b, n)
		dp[a][b] = INF;

	FOR(s, m)FOR(a, n)FOR(b, n)
		setMin(dp[a][b], dist[s][a][b]);

	FOR(a, n)
	{
		FOR(b, n)
		{
			mini[a][b] = dp[a][b];
		}
	}

	FOR(i, q)
	{
		int a, b, r;
		re(a, b, r);
		--a, --b;
		if (r >= 70)
			r = 70;

		queries[r].push_back({ a,b,i });
	}

	for (int k = 1; k < K; ++k)
	{
		debug(queries[k - 1]);
		for (auto u : queries[k - 1])
		{
			auto[a, b, i] = u;
			res[i] = dp[a][b];
		}

		FOR(a, n) FOR(b, n)
			dp2[a][b] = INF;
		FOR(mid, n) FOR(a, n) FOR(b, n)
			setMin(dp2[a][b], dp[a][mid] + mini[mid][b]);

		FOR(a, n) FOR(b, n)
			dp[a][b] = dp2[a][b];

	}

	FOR(i, q)
		prln(res[i]);

}

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

	BOOST;
	cout << fixed << setprecision(50);
	int t = 1;
	// cin >> t;
	while (t--)
	{
		solve();
	}
	return 0;
}


#define _USE_MATH_DEFINES
//#pragma GCC optimize("O3")
// #pragma GCC target("sse4")

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

#define LSB(i) ((i) & -(i)) // zeroes all the bits except the least significant one

#define OPEN(a) freopen(a, "r", stdin)
#define F first
#define S second
#define PB push_back
#define BOOST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ALL(i) begin((i)), end((i))
#define SZ(i) ((int)i.size())
#define SORT(a) sort(ALL(a))

using LL = long long;
using LD = long double;
using PII = pair<int, int>;
using PLL = pair<LL, LL>;
using PLD = pair<LD, LD>;
using VI = vector<int>;
using VLL = vector<LL>;
using VLD = vector<LD>;
using VPII = vector<PII>;
using VPLL = vector<PLL>;
using VPLD = vector<PLD>;
using VVI = vector<VI>;

#define GET_MACRO(_1, _2, _3, _4, NAME, ...) NAME
#define __FOR3(i, a, n, inc) for(int i = (a); (inc) > 0 ? i < (n) : i >= (n); i += (inc))
#define __FOR2(i, a, n) __FOR3(i, a, n, 1)
#define __FOR1(i, n) __FOR2(i, 0, n)
#define FOR(...) GET_MACRO(__VA_ARGS__, __FOR3, __FOR2, __FOR1)(__VA_ARGS__)

#define REV(a,b) for(int a= b; a >= 0; --a)
#define FRU(a,b) for(const auto& a: b)
#define FRUM(a,b) for(auto& a : b)

const int inf = 1e9 + 7;
const int MOD = 1e9 + 7;
const LL INF = 1e18 + 7;
const long double PI = acos(-1);
const LD EPS = 1e-9;

namespace input {
	template< class T> istream& operator>>(istream& st, vector<T> & container) { for (auto& u : container) st >> u; return st; }
	template< class T, size_t N> istream& operator>>(istream& st, array<T, N> & container) { for (auto& u : container) st >> u; return st; }
	template <class T, class U> istream& operator>>(istream& st, pair<T, U> & p) { st >> p.first >> p.second; return st; }

	void re() {}
	template<typename T, typename... TArgs> void re(T& x, TArgs&... rest) { cin >> x; re(rest...); };
}
using namespace input;

#define RE(...)   int __VA_ARGS__; re(__VA_ARGS__);
#define RELL(...) LL __VA_ARGS__; re(__VA_ARGS__);
#define RELD(...) LD __VA_ARGS__; re(__VA_ARGS__);

namespace output
{
	template< class T> ostream& operator<<(ostream& st, const vector<T> & container) { for (auto& u : container) st << u << ' '; return st; }
	template< class T, size_t N> ostream& operator<<(ostream& st, const array<T, N> & container) { for (auto& u : container) st << u << ' '; return st; }
	template <class T, class U> ostream& operator<<(ostream& st, pair<T, U> p) { st << p.first << ' ' << p.second; return st; }

	void pr() {}
	template <typename T> void pr(const T& x) { cout << x; }
	template <typename T, typename... TArgs> void pr(const T& x, const TArgs&... rest) { cout << x << ' '; pr(rest...); }
	template <typename... TArgs> void prln(const TArgs&... args) { pr(args...); cout << '\n'; }
}
using namespace output;

namespace pairs
{
	template<class T, class U, class V> pair<T, U> operator* (pair<T, U>p, V val) { return{ p.first * val, p.second * val }; }
	template<class T, class U, class V> pair<T, U> operator/ (pair<T, U>p, V val) { return{ p.first / val, p.second / val }; }
	template<class T, class U>  pair<T, U> operator- (pair<T, U> a, pair<T, U> b) { return{ a.first - b.first, a.second - b.second }; }
	template<class T, class U>  pair<T, U> operator+ (pair<T, U> a, pair<T, U> b) { return{ a.first + b.first, a.second + b.second }; }
}
using namespace pairs;

namespace triples
{
#define TT1T2T3 template<class T1, class T2, class T3>
#define TT1T2T3T4 template<class T1, class T2, class T3, class T4>
#define TRT1T2T3  triple<T1, T2, T3>
	TT1T2T3 struct triple { T1 x; T2 y; T3 z; triple() : x(T1()), y(T2()), z(T3()) {}; triple(T1 _x, T2 _y, T3 _z) :x(_x), y(_y), z(_z) {} };
	TT1T2T3 bool operator<(const TRT1T2T3&t1, const TRT1T2T3&t2) { if (t1.x != t2.x)return t1.x < t2.x; if (t1.y != t2.y) return t1.y < t2.y; else return t1.z < t2.z; }
	TT1T2T3 bool operator>(const TRT1T2T3&t1, const TRT1T2T3&t2) { if (t1.x != t2.x)return t1.x > t2.x; if (t1.y != t2.y) return t1.y > t2.y; else return t1.z > t2.z; }
	TT1T2T3 bool operator==(const TRT1T2T3&t1, const TRT1T2T3&t2) { return (t1.x == t2.x && t1.y == t2.y && t1.z == t2.z); }
	TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t) { return os >> t.x >> t.y >> t.y; }
	TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t) { return os << t.x << " " << t.y << " " << t.z; }

	TT1T2T3 TRT1T2T3 operator+(TRT1T2T3 a, TRT1T2T3 b) { return { a.x + b.x, a.y + b.y, a.z + b.z }; }
	TT1T2T3 TRT1T2T3 operator-(TRT1T2T3 a, TRT1T2T3 b) { return { a.x - b.x, a.y - b.y, a.z - b.z }; }
	TT1T2T3T4 TRT1T2T3 operator*(TRT1T2T3 a, T4 val) { return { a.x * val, a.y * val, a.z * val }; }
	TT1T2T3T4 TRT1T2T3 operator/(TRT1T2T3 a, T4 val) { return { a.x / val, a.y / val, a.z / val }; }

#undef TT1T2T3T4
#undef TRT1T2T3
#undef TT1T2T3
	using TRII = triple<int, int, int>;
	using TRLL = triple<LL, LL, LL>;
	using TRLD = triple<LD, LD, LD>;
	using VTRII = vector<TRII>;
	using VTRLL = vector<TRLL>;
	using VTRLD = vector<TRLD>;
}
using namespace triples;

namespace geo
{
	template<class T> T dotProduct(pair<T, T> a, pair<T, T> b) { return a.first*b.first + a.second* b.second; }
	template<class T> T crossProduct(pair<T, T>a, pair<T, T> b) { return a.first * b.second - a.second * b.first; }
	template<class T> T lengthPow(pair<T, T> a) { return a.first * 1ll * a.first + a.second * 1ll * a.second; }
	template<class T> LD length(pair<T, T> a) { return sqrt(lengthPow(a)); }

	template<class T> T dotProduct(triple<T, T, T> a, triple<T, T, T> b) { return a.x*b.x + a.y* b.y + a.z*b.z; }
	template<class T> T crossProduct(triple<T, T, T> a, triple<T, T, T> b) { return { a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x }; }
	template<class T> T lengthPow(triple<T, T, T> a) { return a.x * 1ll * a.x + a.y * 1ll * a.y + a.z * 1ll * a.z; }
	template<class T> LD length(triple<T, T, T> a) { return sqrt(lengthPow(a)); }
}
using namespace geo;

template <class T> T invGeneral(T a, T b) { // 0 < a < b, gcd(a,b) = 1
	a %= b;
	if (a == 0) return b == 1 ? 0 : -1;
	T x = invGeneral(b, a);
	return x == -1 ? -1 : ((1 - (LL)b * x) / a + b) % b;
}

template<class T> struct modular {
	T val;
	explicit operator T() const { return val; }
	modular() { val = 0; }
	modular(const LL& v) {
		val = (-MOD <= v && v <= MOD) ? v : v % MOD;
		if (val < 0) val += MOD;
	}

	friend ostream& operator<<(ostream& os, const modular& a) { return os << a.val; }
	friend void pr(const modular& a) { pr(a.val); }
	friend void re(modular& a) { LL x; re(x); a = modular(x); }

	friend bool operator==(const modular& a, const modular& b) { return a.val == b.val; }
	friend bool operator!=(const modular& a, const modular& b) { return !(a == b); }
	friend bool operator<(const modular& a, const modular& b) { return a.val < b.val; }

	modular operator-() const { return modular(-val); }
	modular& operator+=(const modular& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; }
	modular& operator-=(const modular& m) { if ((val -= m.val) < 0) val += MOD; return *this; }
	modular& operator*=(const modular& m) { val = (LL)val*m.val%MOD; return *this; }
	friend modular pow(modular a, LL p) {
		modular ans = 1; for (; p; p /= 2, a *= a) if (p & 1) ans *= a;
		return ans;
	}
	friend modular inv(const modular& a) {
		auto i = invGeneral(a.val, MOD); assert(i != -1);
		return i;
	} // equivalent to return exp(b,MOD-2) if MOD is prime
	modular& operator/=(const modular& m) { return (*this) *= inv(m); }

	friend modular operator+(modular a, const modular& b) { return a += b; }
	friend modular operator-(modular a, const modular& b) { return a -= b; }
	friend modular operator*(modular a, const modular& b) { return a *= b; }
	friend modular operator/(modular a, const modular& b) { return a /= b; }
};

using MI = modular<int>;
using PMI = pair<MI, MI>;
using VMI = vector<MI>;
using VPMI = vector<PMI>;

namespace debug {
	template < typename _T > inline void _debug(const char *s, _T x) { cerr << s << " = " << x << "\n"; }
	template < typename _T, typename... args > void _debug(const char *s, _T x, args... a) { while (*s != ',') cerr << *s++; cerr << " = " << x << ','; _debug(s + 1, a...); }

#if 1  && defined(LOCAL)
#define debug(...) _debug(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) 1999
#define cerr if(0) cout
#endif
}
using namespace debug;

template <class T> bool setMax(T& v, T newV) { if (v < newV) { v = newV; return true; } return false; }
template <class T> bool setMin(T& v, T newV) { if (v > newV) { v = newV; return true; } return false; }
LL sq(LL a) { return a * a; }

const int N = 65;
const int K = 1004;
const int Q = 1e5 + 5;

LL dp[N][N];
LL dp2[N][N];
LL dist[N][N][N];
LL mini[N][N];

vector<TRII > queries[K];
int res[Q];
void solve()
{
	int n, m, q;
	re(n, m, q);

	FOR(i, m)
	{
		FOR(j, n)
		{
			FOR(k, n)
			{
				re(dist[i][j][k]);
			}
		}

		FOR(mid, n) FOR(a, n) FOR(b, n)
			setMin(dist[i][a][b], dist[i][a][mid] + dist[i][mid][b]);
	}

	FOR(a, n)FOR(b, n)
		dp[a][b] = INF;

	FOR(s, m)FOR(a, n)FOR(b, n)
		setMin(dp[a][b], dist[s][a][b]);

	FOR(a, n)
	{
		FOR(b, n)
		{
			mini[a][b] = dp[a][b];
		}
	}

	FOR(i, q)
	{
		int a, b, r;
		re(a, b, r);
		--a, --b;
		if (r >= 70)
			r = 70;

		queries[r].push_back({ a,b,i });
	}

	for (int k = 1; k < K; ++k)
	{
		debug(queries[k - 1]);
		for (auto u : queries[k - 1])
		{
			auto[a, b, i] = u;
			res[i] = dp[a][b];
		}

		FOR(a, n) FOR(b, n)
			dp2[a][b] = INF;
		FOR(mid, n) FOR(a, n) FOR(b, n)
			setMin(dp2[a][b], dp[a][mid] + mini[mid][b]);

		FOR(a, n) FOR(b, n)
			dp[a][b] = dp2[a][b];

	}

	FOR(i, q)
		prln(res[i]);

}

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

	BOOST;
	cout << fixed << setprecision(50);
	int t = 1;
	// cin >> t;
	while (t--)
	{
		solve();
	}
	return 0;
}


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

const int maxn = 65;
const int inf = 1e9;
int n, m, r;
int dp[maxn][maxn][maxn];
int a[maxn][maxn][maxn];

int main() {
	ios::sync_with_stdio(false);
	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 k = 0; k < n; k++)
				cin >> a[i][j][k];

	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] = inf;

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

	return 0;
}


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

const int maxn = 65;
const int inf = 1e9;
int n, m, r;
int dp[maxn][maxn][maxn];
int a[maxn][maxn][maxn];

int main() {
	ios::sync_with_stdio(false);
	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 k = 0; k < n; k++)
				cin >> a[i][j][k];

	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] = inf;

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

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll maxn = 70;
const ll INF = ((ll)1e9);

ll n,m,r;

ll dis[maxn][maxn][maxn];
ll dp[maxn][maxn][maxn];

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

	cin>>n>>m>>r;
	for(int i=0;i<maxn;i++)
		for(int j=0;j<maxn;j++)
			for(int k=0;k<maxn;k++)
				dp[i][j][k] = INF;

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

		for(int j=0;j<n;j++)
			for(int k=0;k<n;k++)
				for(int q=0;q<n;q++)
					dis[k][q][i] = min(dis[k][q][i], dis[k][j][i] + dis[j][q][i]);
	}

	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			for(int q=0;q<m;q++)
				dp[i][j][0] = min(dp[i][j][0],dis[i][j][q]);

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

	while(r--)
	{
		ll v,u,k;
		cin>>v>>u>>k;

		if(k>=maxn)
			k = maxn-1;

		cout<<dp[v-1][u-1][k]<<"\n";
	}

	return 0;
}


#include <bits/stdc++.h>
#define ll int
using namespace std;
ll n , m , r;
ll d[62][62][62];
const ll INF = 1e8;
vector<pair< pair<ll,ll> , ll> > v[100];
ll dp[62][62][62];
void flod(ll id)
{
        for(ll k = 1 ; k <= n; k++){
                for(ll i = 1 ; i <=n;i++)
                        for(ll j = 1 ; j<= n; j++)
                {
                        d[id][i][j] = min(d[id][i][j] , d[id][i][k] + d[id][k][j]);
                }
        }
}
void go()
{
        for(ll k =1 ; k <= n; k++)
        {
                for(ll i = 1 ; i <= n; i++)
                {
                        for(ll j = 1 ; j <= n ; j++)
                        {
                                for(ll b = 1 ; b <= n ; b++)
                                {
                                        dp[k][i][j] = min(dp[k-1][i][b] + dp[0][b][j] , dp[k][i][j]);
                                }
                        }
                }
        }
}
int main()
{
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cin >> n >> m >> r;
        for(ll i =1 ; i<= m ; i++)
        {
                for(ll j = 1 ; j<=n;j++)
                {
                        for(ll k =1 ; k <= n; k++) cin >> d[i][j][k];
                }
        }//
        for(ll i =1 ;i <= m;  i++) flod(i);
        for(ll i =1 ; i<=n;i++) for(ll j =1 ; j<=n;j++) for(ll k =1 ;k<=n;k++) dp[i][j][k] = INF;
        for(ll i =1 ;i <= n; i++)
        { /////
                for(ll j =1 ; j <=n ; j++)
                {
                        ll x = INF;
                        for(ll k = 1 ; k <= m ; k++) x = min(x , d[k][i][j]);
                        dp[0][i][j] = x;
                        //cout << i << "  " << j << "  " << dp[0][i][j] << endl;
                }
        }
        go();
        ll s , t , k;
        ll i = 1;
        while(i <= r)
        {
                cin >> s >> t >> k;
                if(k < n) {
                        cout << dp[k][s][t] << endl;
                }else cout << dp[n][s][t] << endl;

                i++;
        }
        //for(ll ii =1 ; ii<= r ; ii++) cout << ans[ii] << endl;
        return 0;
}


#include <bits/stdc++.h>
using namespace std;
int d[61][61];
int md[61][61];
int d2[61][61][61];
int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int n, m, r;
	cin >> n >> m >> r;
	for(auto &x : md) for(auto &y : x) y = 1e9;
	for(int t = 0; t < m; ++t) {
		for(int i = 1; i <= n; ++i)
			for(int j = 1; j <= n; ++j)
				cin >> d[i][j];
		for(int k = 1; k <= n; ++k)
			for(int i = 1; i <= n; ++i)
				for(int j = 1; j <= n; ++j)
					d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
		for(int i = 1; i <= n; ++i)
			for(int j = 1; j <= n; ++j)
				md[i][j] = min(md[i][j], d[i][j]);
	}
	for(auto &x : d2) for(auto &y : x) for(auto &z : y) z = 1e9;
	for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= n; ++j)
			d2[0][i][j] = md[i][j];
	for(int t = 1; t <= n; ++t)
		for(int k = 1; k <= n; ++k)
			for(int i = 1; i <= n; ++i)
				for(int j = 1; j <= n; ++j)
					d2[t][i][j] = min(d2[t][i][j], d2[t-1][i][k] + d2[0][k][j]);
	for(int i = 0; i < r; ++i) {
		int s, t, k;
		cin >> s >> t >> k;
		cout << d2[min(k, n)][s][t] << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int d[61][61];
int md[61][61];
int d2[61][61][61];
int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int n, m, r;
	cin >> n >> m >> r;
	for(auto &x : md) for(auto &y : x) y = 1e9;
	for(int t = 0; t < m; ++t) {
		for(int i = 1; i <= n; ++i)
			for(int j = 1; j <= n; ++j)
				cin >> d[i][j];
		for(int k = 1; k <= n; ++k)
			for(int i = 1; i <= n; ++i)
				for(int j = 1; j <= n; ++j)
					d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
		for(int i = 1; i <= n; ++i)
			for(int j = 1; j <= n; ++j)
				md[i][j] = min(md[i][j], d[i][j]);
	}
	for(auto &x : d2) for(auto &y : x) for(auto &z : y) z = 1e9;
	for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= n; ++j)
			d2[0][i][j] = md[i][j];
	for(int t = 1; t <= n; ++t)
		for(int k = 1; k <= n; ++k)
			for(int i = 1; i <= n; ++i)
				for(int j = 1; j <= n; ++j)
					d2[t][i][j] = min(d2[t][i][j], d2[t-1][i][k] + d2[0][k][j]);
	for(int i = 0; i < r; ++i) {
		int s, t, k;
		cin >> s >> t >> k;
		cout << d2[min(k, n)][s][t] << '\n';
	}
	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
using ld = long double;

#ifdef LOCAL
template <typename A, typename B> string to_string(pair<A, B> p);
template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {return '"' + s + '"';}
string to_string(const char* s) {return to_string((string) s);}
string to_string(bool b) {return (b ? "true" : "false");}
string to_string(vector<bool> v) {bool first = true;string res = "{";for (int i = 0; i < static_cast<int>(v.size()); i++) {if (!first) {res += ", ";}first = false;res += to_string(v[i]);}res += "}";return res;}
template <size_t N>string to_string(bitset<N> v) {string res = "";for (size_t i = 0; i < N; i++) {res += static_cast<char>('0' + v[i]);}return res;}
template <typename A>string to_string(A v) {bool first = true;string res = "{";for (const auto &x : v) {if (!first) {res += ", ";}first = false;res += to_string(x);}res += "}";return res;}
template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";}
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";}
void debug_out() { cerr << "\n"; }
template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) {cerr << " " << to_string(H);debug_out(T...);}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define trace(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 0; i < n; i++) cerr << a[i] << ", "; cerr << "]\n"
#else
#define debug(...) 42
#define trace(a, n) 42
#endif

#define int long long
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define uint unsigned int
#define pi pair<int, int>
#define pl pair<ll, ll>

template <class L, class R> istream& operator>> (istream& is, pair<L, R>& P) { return is >> P.ff >> P.ss; }
template <class T> istream& operator>> (istream& is, vector<T>& V) { for (auto& e : V) is >> e; return is; }
template <class L, class R> ostream& operator<< (ostream& os, pair<L, R>& P) { return os << P.ff << " " << P.ss; }
template <class T> ostream& operator<< (ostream& os, vector<T>& V) { for (auto& e : V) os << e << " "; return os; }
template <class T = int> int bsl(vector<T> a, T x) { int lo = 0; int hi = sz(a) - 1; int req = -1; int mid = lo + (hi - lo) / 2; while (lo <= hi) { if (a[mid] <= x) { req = mid; lo = mid + 1; } else { hi = mid - 1; } mid = lo + (hi - lo) / 2; } return req; }
template <class T = int> int bsg(vector<T> a, T x) { int lo = 0; int hi = sz(a) - 1; int req = -1; int mid = lo + (hi - lo) / 2; while (lo <= hi) { if (a[mid] >= x) { req = mid; hi = mid - 1; } else { lo = mid + 1; } mid = lo + (hi - lo) / 2; } return req; }

template <typename Map>bool map_compare (Map const &lhs, Map const &rhs) { return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());}
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937_64 rng(seed);
inline long long randint (long long l, long long r) { uniform_int_distribution<long long> dis(l, r); return dis(rng); }
inline double random (double l, double r) { uniform_real_distribution<double> dis(l, r); return dis(rng); }
inline double randn() { normal_distribution<double> dis; return dis(rng); }
inline double rdn (double std_dev, double mean) { return std_dev * randn() + mean; }

const int infi = 2e9;
const long long infl = 4e18;
const int md = 998244353;
const double PI = 3.14159265358979323846;

inline long long modpow(long long x, long long n) { long long res = 1; while (n > 0) { if (n % 2) res = (res * x) % md; x = (x * x) % md; n /= 2; } return res; }
inline long long inv(long long x) {return modpow(x, md-2);}
inline bool imax (long long& a, long long b) { bool res = (b > a); a = max(a, b); return res;}
inline bool emax (long long& a, long long b) { bool res = (b >= a); a = max(a, b); return res;}
inline bool imin (long long& a, long long b) { bool res = (b < a); a = min(a, b); return res;}
inline bool emin (long long& a, long long b) { bool res = (b <= a); a = min(a, b); return res;}
inline long long log_2 (long long n) { return (n > 1ll)? 1ll + log_2(n/2): 0ll;}
inline long long log_10 (long long n) { return (n > 9ll)? 1ll + log_10(n/10): 0ll;}
inline void add(long long& a, long long b) {a = ((a % md) + (b % md)) % md;}
inline void sub(long long& a, long long b) {a = ((a % md) - (b % md) + md) % md;}
inline void mul(long long& a, long long b) {a = ((a % md) * (b % md)) % md;}
inline long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r;} return a; }

template <class T>
struct cmp {
  bool operator() (const T& x, const T& y) const {
    return x > y;
  }
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};

int f[66][66][66];
int dp[66][66][66];

signed main() {
#ifdef READ_FILE
  freopen("input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(10);
  int n, m, r;
  cin >> n >> m >> r;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= n; k++) {
        dp[i][j][k] = infi;
      }
    }
  }
  for (int it = 0; it < m; it++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> f[it][i][j];
      }
    }
  }
  for (int it = 0; it < m; it++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          imin(f[it][i][j], f[it][i][k] + f[it][k][j]);
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int it = 0; it < m; it++) {
        imin(dp[0][i][j], f[it][i][j]);
      }
    }
  }
  for (int it = 1; it <= n; it++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          imin(dp[it][i][j], dp[0][i][k] + dp[it - 1][k][j]);
          imin(dp[it][i][j], dp[it - 1][i][k] + dp[0][k][j]);
        }
      }
    }
  }
  while (r--) {
    int s, t, k;
    cin >> s >> t >> k;
    --s; --t;
    k = min(n, k);
    cout << dp[k][s][t] << "\n";
  }
  return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
using ld = long double;

#ifdef LOCAL
template <typename A, typename B> string to_string(pair<A, B> p);
template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {return '"' + s + '"';}
string to_string(const char* s) {return to_string((string) s);}
string to_string(bool b) {return (b ? "true" : "false");}
string to_string(vector<bool> v) {bool first = true;string res = "{";for (int i = 0; i < static_cast<int>(v.size()); i++) {if (!first) {res += ", ";}first = false;res += to_string(v[i]);}res += "}";return res;}
template <size_t N>string to_string(bitset<N> v) {string res = "";for (size_t i = 0; i < N; i++) {res += static_cast<char>('0' + v[i]);}return res;}
template <typename A>string to_string(A v) {bool first = true;string res = "{";for (const auto &x : v) {if (!first) {res += ", ";}first = false;res += to_string(x);}res += "}";return res;}
template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";}
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";}
void debug_out() { cerr << "\n"; }
template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) {cerr << " " << to_string(H);debug_out(T...);}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define trace(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 0; i < n; i++) cerr << a[i] << ", "; cerr << "]\n"
#else
#define debug(...) 42
#define trace(a, n) 42
#endif

#define int long long
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define uint unsigned int
#define pi pair<int, int>
#define pl pair<ll, ll>

template <class L, class R> istream& operator>> (istream& is, pair<L, R>& P) { return is >> P.ff >> P.ss; }
template <class T> istream& operator>> (istream& is, vector<T>& V) { for (auto& e : V) is >> e; return is; }
template <class L, class R> ostream& operator<< (ostream& os, pair<L, R>& P) { return os << P.ff << " " << P.ss; }
template <class T> ostream& operator<< (ostream& os, vector<T>& V) { for (auto& e : V) os << e << " "; return os; }
template <class T = int> int bsl(vector<T> a, T x) { int lo = 0; int hi = sz(a) - 1; int req = -1; int mid = lo + (hi - lo) / 2; while (lo <= hi) { if (a[mid] <= x) { req = mid; lo = mid + 1; } else { hi = mid - 1; } mid = lo + (hi - lo) / 2; } return req; }
template <class T = int> int bsg(vector<T> a, T x) { int lo = 0; int hi = sz(a) - 1; int req = -1; int mid = lo + (hi - lo) / 2; while (lo <= hi) { if (a[mid] >= x) { req = mid; hi = mid - 1; } else { lo = mid + 1; } mid = lo + (hi - lo) / 2; } return req; }

template <typename Map>bool map_compare (Map const &lhs, Map const &rhs) { return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());}
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937_64 rng(seed);
inline long long randint (long long l, long long r) { uniform_int_distribution<long long> dis(l, r); return dis(rng); }
inline double random (double l, double r) { uniform_real_distribution<double> dis(l, r); return dis(rng); }
inline double randn() { normal_distribution<double> dis; return dis(rng); }
inline double rdn (double std_dev, double mean) { return std_dev * randn() + mean; }

const int infi = 2e9;
const long long infl = 4e18;
const int md = 998244353;
const double PI = 3.14159265358979323846;

inline long long modpow(long long x, long long n) { long long res = 1; while (n > 0) { if (n % 2) res = (res * x) % md; x = (x * x) % md; n /= 2; } return res; }
inline long long inv(long long x) {return modpow(x, md-2);}
inline bool imax (long long& a, long long b) { bool res = (b > a); a = max(a, b); return res;}
inline bool emax (long long& a, long long b) { bool res = (b >= a); a = max(a, b); return res;}
inline bool imin (long long& a, long long b) { bool res = (b < a); a = min(a, b); return res;}
inline bool emin (long long& a, long long b) { bool res = (b <= a); a = min(a, b); return res;}
inline long long log_2 (long long n) { return (n > 1ll)? 1ll + log_2(n/2): 0ll;}
inline long long log_10 (long long n) { return (n > 9ll)? 1ll + log_10(n/10): 0ll;}
inline void add(long long& a, long long b) {a = ((a % md) + (b % md)) % md;}
inline void sub(long long& a, long long b) {a = ((a % md) - (b % md) + md) % md;}
inline void mul(long long& a, long long b) {a = ((a % md) * (b % md)) % md;}
inline long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r;} return a; }

template <class T>
struct cmp {
  bool operator() (const T& x, const T& y) const {
    return x > y;
  }
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};

int f[66][66][66];
int dp[66][66][66];

signed main() {
#ifdef READ_FILE
  freopen("input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(10);
  int n, m, r;
  cin >> n >> m >> r;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= n; k++) {
        dp[i][j][k] = infi;
      }
    }
  }
  for (int it = 0; it < m; it++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> f[it][i][j];
      }
    }
  }
  for (int it = 0; it < m; it++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          imin(f[it][i][j], f[it][i][k] + f[it][k][j]);
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int it = 0; it < m; it++) {
        imin(dp[0][i][j], f[it][i][j]);
      }
    }
  }
  m = min(m, n);
  for (int it = 1; it <= m; it++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          imin(dp[it][i][j], dp[0][i][k] + dp[it - 1][k][j]);
          imin(dp[it][i][j], dp[it - 1][i][k] + dp[0][k][j]);
        }
      }
    }
  }
  while (r--) {
    int s, t, k;
    cin >> s >> t >> k;
    --s; --t;
    k = min(n, k);
    cout << dp[k][s][t] << "\n";
  }
  return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
using ld = long double;

#ifdef LOCAL
template <typename A, typename B> string to_string(pair<A, B> p);
template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {return '"' + s + '"';}
string to_string(const char* s) {return to_string((string) s);}
string to_string(bool b) {return (b ? "true" : "false");}
string to_string(vector<bool> v) {bool first = true;string res = "{";for (int i = 0; i < static_cast<int>(v.size()); i++) {if (!first) {res += ", ";}first = false;res += to_string(v[i]);}res += "}";return res;}
template <size_t N>string to_string(bitset<N> v) {string res = "";for (size_t i = 0; i < N; i++) {res += static_cast<char>('0' + v[i]);}return res;}
template <typename A>string to_string(A v) {bool first = true;string res = "{";for (const auto &x : v) {if (!first) {res += ", ";}first = false;res += to_string(x);}res += "}";return res;}
template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";}
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";}
void debug_out() { cerr << "\n"; }
template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) {cerr << " " << to_string(H);debug_out(T...);}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define trace(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 0; i < n; i++) cerr << a[i] << ", "; cerr << "]\n"
#else
#define debug(...) 42
#define trace(a, n) 42
#endif

#define int long long
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define uint unsigned int
#define pi pair<int, int>
#define pl pair<ll, ll>

template <class L, class R> istream& operator>> (istream& is, pair<L, R>& P) { return is >> P.ff >> P.ss; }
template <class T> istream& operator>> (istream& is, vector<T>& V) { for (auto& e : V) is >> e; return is; }
template <class L, class R> ostream& operator<< (ostream& os, pair<L, R>& P) { return os << P.ff << " " << P.ss; }
template <class T> ostream& operator<< (ostream& os, vector<T>& V) { for (auto& e : V) os << e << " "; return os; }
template <class T = int> int bsl(vector<T> a, T x) { int lo = 0; int hi = sz(a) - 1; int req = -1; int mid = lo + (hi - lo) / 2; while (lo <= hi) { if (a[mid] <= x) { req = mid; lo = mid + 1; } else { hi = mid - 1; } mid = lo + (hi - lo) / 2; } return req; }
template <class T = int> int bsg(vector<T> a, T x) { int lo = 0; int hi = sz(a) - 1; int req = -1; int mid = lo + (hi - lo) / 2; while (lo <= hi) { if (a[mid] >= x) { req = mid; hi = mid - 1; } else { lo = mid + 1; } mid = lo + (hi - lo) / 2; } return req; }

template <typename Map>bool map_compare (Map const &lhs, Map const &rhs) { return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());}
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937_64 rng(seed);
inline long long randint (long long l, long long r) { uniform_int_distribution<long long> dis(l, r); return dis(rng); }
inline double random (double l, double r) { uniform_real_distribution<double> dis(l, r); return dis(rng); }
inline double randn() { normal_distribution<double> dis; return dis(rng); }
inline double rdn (double std_dev, double mean) { return std_dev * randn() + mean; }

const int infi = 2e9;
const long long infl = 4e18;
const int md = 998244353;
const double PI = 3.14159265358979323846;

inline long long modpow(long long x, long long n) { long long res = 1; while (n > 0) { if (n % 2) res = (res * x) % md; x = (x * x) % md; n /= 2; } return res; }
inline long long inv(long long x) {return modpow(x, md-2);}
inline bool imax (long long& a, long long b) { bool res = (b > a); a = max(a, b); return res;}
inline bool emax (long long& a, long long b) { bool res = (b >= a); a = max(a, b); return res;}
inline bool imin (long long& a, long long b) { bool res = (b < a); a = min(a, b); return res;}
inline bool emin (long long& a, long long b) { bool res = (b <= a); a = min(a, b); return res;}
inline long long log_2 (long long n) { return (n > 1ll)? 1ll + log_2(n/2): 0ll;}
inline long long log_10 (long long n) { return (n > 9ll)? 1ll + log_10(n/10): 0ll;}
inline void add(long long& a, long long b) {a = ((a % md) + (b % md)) % md;}
inline void sub(long long& a, long long b) {a = ((a % md) - (b % md) + md) % md;}
inline void mul(long long& a, long long b) {a = ((a % md) * (b % md)) % md;}
inline long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r;} return a; }

template <class T>
struct cmp {
  bool operator() (const T& x, const T& y) const {
    return x > y;
  }
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};

int f[66][66][66];
int dp[66][66][66];

signed main() {
#ifdef READ_FILE
  freopen("input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(10);
  int n, m, r;
  cin >> n >> m >> r;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= n; k++) {
        dp[i][j][k] = infi;
      }
    }
  }
  for (int it = 0; it < m; it++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> f[it][i][j];
      }
    }
  }
  for (int it = 0; it < m; it++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          imin(f[it][i][j], f[it][i][k] + f[it][k][j]);
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int it = 0; it < m; it++) {
        imin(dp[0][i][j], f[it][i][j]);
      }
    }
  }
  m = min(m, n);
  for (int it = 1; it <= m; it++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          imin(dp[it][i][j], dp[0][i][k] + dp[it - 1][k][j]);
          imin(dp[it][i][j], dp[it - 1][i][k] + dp[0][k][j]);
        }
      }
    }
  }
  while (r--) {
    int s, t, k;
    cin >> s >> t >> k;
    --s; --t;
    k = min(m, k);
    cout << dp[k][s][t] << "\n";
  }
  return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
using ld = long double;

#ifdef LOCAL
template <typename A, typename B> string to_string(pair<A, B> p);
template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {return '"' + s + '"';}
string to_string(const char* s) {return to_string((string) s);}
string to_string(bool b) {return (b ? "true" : "false");}
string to_string(vector<bool> v) {bool first = true;string res = "{";for (int i = 0; i < static_cast<int>(v.size()); i++) {if (!first) {res += ", ";}first = false;res += to_string(v[i]);}res += "}";return res;}
template <size_t N>string to_string(bitset<N> v) {string res = "";for (size_t i = 0; i < N; i++) {res += static_cast<char>('0' + v[i]);}return res;}
template <typename A>string to_string(A v) {bool first = true;string res = "{";for (const auto &x : v) {if (!first) {res += ", ";}first = false;res += to_string(x);}res += "}";return res;}
template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";}
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";}
void debug_out() { cerr << "\n"; }
template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) {cerr << " " << to_string(H);debug_out(T...);}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define trace(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 0; i < n; i++) cerr << a[i] << ", "; cerr << "]\n"
#else
#define debug(...) 42
#define trace(a, n) 42
#endif

#define int long long
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define uint unsigned int
#define pi pair<int, int>
#define pl pair<ll, ll>

template <class L, class R> istream& operator>> (istream& is, pair<L, R>& P) { return is >> P.ff >> P.ss; }
template <class T> istream& operator>> (istream& is, vector<T>& V) { for (auto& e : V) is >> e; return is; }
template <class L, class R> ostream& operator<< (ostream& os, pair<L, R>& P) { return os << P.ff << " " << P.ss; }
template <class T> ostream& operator<< (ostream& os, vector<T>& V) { for (auto& e : V) os << e << " "; return os; }
template <class T = int> int bsl(vector<T> a, T x) { int lo = 0; int hi = sz(a) - 1; int req = -1; int mid = lo + (hi - lo) / 2; while (lo <= hi) { if (a[mid] <= x) { req = mid; lo = mid + 1; } else { hi = mid - 1; } mid = lo + (hi - lo) / 2; } return req; }
template <class T = int> int bsg(vector<T> a, T x) { int lo = 0; int hi = sz(a) - 1; int req = -1; int mid = lo + (hi - lo) / 2; while (lo <= hi) { if (a[mid] >= x) { req = mid; hi = mid - 1; } else { lo = mid + 1; } mid = lo + (hi - lo) / 2; } return req; }

template <typename Map>bool map_compare (Map const &lhs, Map const &rhs) { return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());}
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937_64 rng(seed);
inline long long randint (long long l, long long r) { uniform_int_distribution<long long> dis(l, r); return dis(rng); }
inline double random (double l, double r) { uniform_real_distribution<double> dis(l, r); return dis(rng); }
inline double randn() { normal_distribution<double> dis; return dis(rng); }
inline double rdn (double std_dev, double mean) { return std_dev * randn() + mean; }

const int infi = 2e9;
const long long infl = 4e18;
const int md = 998244353;
const double PI = 3.14159265358979323846;

inline long long modpow(long long x, long long n) { long long res = 1; while (n > 0) { if (n % 2) res = (res * x) % md; x = (x * x) % md; n /= 2; } return res; }
inline long long inv(long long x) {return modpow(x, md-2);}
inline bool imax (long long& a, long long b) { bool res = (b > a); a = max(a, b); return res;}
inline bool emax (long long& a, long long b) { bool res = (b >= a); a = max(a, b); return res;}
inline bool imin (long long& a, long long b) { bool res = (b < a); a = min(a, b); return res;}
inline bool emin (long long& a, long long b) { bool res = (b <= a); a = min(a, b); return res;}
inline long long log_2 (long long n) { return (n > 1ll)? 1ll + log_2(n/2): 0ll;}
inline long long log_10 (long long n) { return (n > 9ll)? 1ll + log_10(n/10): 0ll;}
inline void add(long long& a, long long b) {a = ((a % md) + (b % md)) % md;}
inline void sub(long long& a, long long b) {a = ((a % md) - (b % md) + md) % md;}
inline void mul(long long& a, long long b) {a = ((a % md) * (b % md)) % md;}
inline long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r;} return a; }

template <class T>
struct cmp {
  bool operator() (const T& x, const T& y) const {
    return x > y;
  }
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};

int f[66][66][66];
int dp[66][66][66];

signed main() {
#ifdef READ_FILE
  freopen("input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(10);
  int n, m, r;
  cin >> n >> m >> r;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= n; k++) {
        dp[i][j][k] = infi;
      }
    }
  }
  for (int it = 0; it < m; it++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> f[it][i][j];
      }
    }
  }
  for (int it = 0; it < m; it++) {

        for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
          imin(f[it][i][j], f[it][i][k] + f[it][k][j]);
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int it = 0; it < m; it++) {
        imin(dp[0][i][j], f[it][i][j]);
      }
    }
  }
  for (int it = 1; it <= n; it++) {
    for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {

          imin(dp[it][i][j], dp[0][i][k] + dp[it - 1][k][j]);
          imin(dp[it][i][j], dp[it - 1][i][k] + dp[0][k][j]);
        }
      }
    }
  }
  while (r--) {
    int s, t, k;
    cin >> s >> t >> k;
    --s; --t;
    k = min(n, k);
    cout << dp[k][s][t] << "\n";
  }
  return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
using ld = long double;

#ifdef LOCAL
template <typename A, typename B> string to_string(pair<A, B> p);
template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {return '"' + s + '"';}
string to_string(const char* s) {return to_string((string) s);}
string to_string(bool b) {return (b ? "true" : "false");}
string to_string(vector<bool> v) {bool first = true;string res = "{";for (int i = 0; i < static_cast<int>(v.size()); i++) {if (!first) {res += ", ";}first = false;res += to_string(v[i]);}res += "}";return res;}
template <size_t N>string to_string(bitset<N> v) {string res = "";for (size_t i = 0; i < N; i++) {res += static_cast<char>('0' + v[i]);}return res;}
template <typename A>string to_string(A v) {bool first = true;string res = "{";for (const auto &x : v) {if (!first) {res += ", ";}first = false;res += to_string(x);}res += "}";return res;}
template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";}
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";}
void debug_out() { cerr << "\n"; }
template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) {cerr << " " << to_string(H);debug_out(T...);}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define trace(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 0; i < n; i++) cerr << a[i] << ", "; cerr << "]\n"
#else
#define debug(...) 42
#define trace(a, n) 42
#endif

#define int long long
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define uint unsigned int
#define pi pair<int, int>
#define pl pair<ll, ll>

template <class L, class R> istream& operator>> (istream& is, pair<L, R>& P) { return is >> P.ff >> P.ss; }
template <class T> istream& operator>> (istream& is, vector<T>& V) { for (auto& e : V) is >> e; return is; }
template <class L, class R> ostream& operator<< (ostream& os, pair<L, R>& P) { return os << P.ff << " " << P.ss; }
template <class T> ostream& operator<< (ostream& os, vector<T>& V) { for (auto& e : V) os << e << " "; return os; }
template <class T = int> int bsl(vector<T> a, T x) { int lo = 0; int hi = sz(a) - 1; int req = -1; int mid = lo + (hi - lo) / 2; while (lo <= hi) { if (a[mid] <= x) { req = mid; lo = mid + 1; } else { hi = mid - 1; } mid = lo + (hi - lo) / 2; } return req; }
template <class T = int> int bsg(vector<T> a, T x) { int lo = 0; int hi = sz(a) - 1; int req = -1; int mid = lo + (hi - lo) / 2; while (lo <= hi) { if (a[mid] >= x) { req = mid; hi = mid - 1; } else { lo = mid + 1; } mid = lo + (hi - lo) / 2; } return req; }

template <typename Map>bool map_compare (Map const &lhs, Map const &rhs) { return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());}
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937_64 rng(seed);
inline long long randint (long long l, long long r) { uniform_int_distribution<long long> dis(l, r); return dis(rng); }
inline double random (double l, double r) { uniform_real_distribution<double> dis(l, r); return dis(rng); }
inline double randn() { normal_distribution<double> dis; return dis(rng); }
inline double rdn (double std_dev, double mean) { return std_dev * randn() + mean; }

const int infi = 2e9;
const long long infl = 4e18;
const int md = 998244353;
const double PI = 3.14159265358979323846;

inline long long modpow(long long x, long long n) { long long res = 1; while (n > 0) { if (n % 2) res = (res * x) % md; x = (x * x) % md; n /= 2; } return res; }
inline long long inv(long long x) {return modpow(x, md-2);}
inline bool imax (long long& a, long long b) { bool res = (b > a); a = max(a, b); return res;}
inline bool emax (long long& a, long long b) { bool res = (b >= a); a = max(a, b); return res;}
inline bool imin (long long& a, long long b) { bool res = (b < a); a = min(a, b); return res;}
inline bool emin (long long& a, long long b) { bool res = (b <= a); a = min(a, b); return res;}
inline long long log_2 (long long n) { return (n > 1ll)? 1ll + log_2(n/2): 0ll;}
inline long long log_10 (long long n) { return (n > 9ll)? 1ll + log_10(n/10): 0ll;}
inline void add(long long& a, long long b) {a = ((a % md) + (b % md)) % md;}
inline void sub(long long& a, long long b) {a = ((a % md) - (b % md) + md) % md;}
inline void mul(long long& a, long long b) {a = ((a % md) * (b % md)) % md;}
inline long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r;} return a; }

template <class T>
struct cmp {
  bool operator() (const T& x, const T& y) const {
    return x > y;
  }
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};

int f[66][66][66];
int dp[66][66][66];

signed main() {
#ifdef READ_FILE
  freopen("input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(10);
  int n, m, r;
  cin >> n >> m >> r;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= n; k++) {
        dp[i][j][k] = infi;
      }
    }
  }
  for (int it = 0; it < m; it++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> f[it][i][j];
      }
    }
  }
  for (int it = 0; it < m; it++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          imin(f[it][i][j], f[it][i][k] + f[it][k][j]);
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int it = 0; it < m; it++) {
        imin(dp[0][i][j], f[it][i][j]);
      }
    }
  }
  for (int it = 1; it <= n; it++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          imin(dp[it][i][j], dp[0][i][k] + dp[it - 1][k][j]);
          imin(dp[it][i][j], dp[it - 1][i][k] + dp[0][k][j]);
        }
      }
    }
  }
  while (r--) {
    int s, t, k;
    cin >> s >> t >> k;
    --s; --t;
    k = min(n - 1, k);
    cout << dp[k][s][t] << "\n";
  }
  return 0;
}


//### In The Name Of GOD ###

//%% Remember; (((He))) is The (((Accepter))) %%

// GOD IS GREATER ...

#include <bits/stdc++.h>

using namespace std;

typedef long long ll ;
typedef long double ld ;

#define int ll
#define pb push_back
#define bp pop_back
#define pf push_front
#define fp pop_front
#define X first
#define Y second
#define sz(a) (int)(a.size())
#define sg(a, b) (a + b - 1) / b

const int MOD = 1000 * 1000 * 1000 + 7;
const int INF = 1000 * 1000 * 1000 + 10 ;
const int MAXN = 61;
const int MLOG = 17;
const int NMAX = 70;

signed main() {
	int n, m, r, dis[MAXN][MAXN];
	int dp[NMAX][MAXN][MAXN];
	cin >> n >> m >> r;
	for(int i = 0; i < MAXN; i++)
		for(int j = 0; j < MAXN; j++)
			for(int k = 0; k < NMAX; k++)
				dp[k][i][j] = INF;
	for(int i = 0; i < m; i++) {
		for(int x = 0; x < n; x++)
			for(int y = 0; y < n; y++)
				cin >> dis[x][y];
		for(int k = 0; k < n; k++)
			for(int i = 0 ; i < n; i++)
				for(int j = 0; j < n; j++)
					dis[i][j] = min(dis[i][j], dis[i][k] + dis[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], dis[i][j]);
	}
	for(int t = 1; t < NMAX; t++)
		for(int m = 0; m < n; m++)
			for(int i = 0; i < n; i++)
				for(int j = 0; j < n; j++)
					dp[t][i][j] = min(dp[t][i][j], dp[t - 1][i][m] + dp[0][m][j]);
	for(int i = 0; i < r; i++) {
		int s, t, k;
		cin >> t >> s >> k;
		k = min(MAXN - 1, k);
		cout << dp[k][t - 1][s - 1] << endl;
	}
}


//### In The Name Of GOD ###

//%% Remember; (((He))) is The (((Accepter))) %%

// GOD IS GREATER ...

#include <bits/stdc++.h>

using namespace std;

typedef long long ll ;
typedef long double ld ;

#define int ll
#define pb push_back
#define bp pop_back
#define pf push_front
#define fp pop_front
#define X first
#define Y second
#define sz(a) (int)(a.size())
#define sg(a, b) (a + b - 1) / b

const int MOD = 1000 * 1000 * 1000 + 7;
const int INF = 1000 * 1000 * 1000 + 10 ;
const int MAXN = 61;
const int MLOG = 17;
const int NMAX = 70;

signed main() {
	int n, m, r, dis[MAXN][MAXN];
	int dp[MAXN][MAXN][NMAX];
	cin >> n >> m >> r;
	for(int i = 0; i < MAXN; i++)
		for(int j = 0; j < MAXN; j++)
			for(int k = 0; k < NMAX; k++)
				dp[i][j][k] = INF;
	for(int i = 0; i < m; i++) {
		for(int x = 0; x < n; x++)
			for(int y = 0; y < n; y++)
				cin >> dis[x][y];
		for(int k = 0; k < n; k++)
			for(int i = 0 ; i < n; i++)
				for(int j = 0; j < n; j++)
					dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
		for(int i = 0 ; i < n; i++)
			for(int j = 0 ; j < n; j++)
				dp[i][j][0] = min(dp[i][j][0], dis[i][j]);
	}
	for(int t = 1; t < NMAX; t++)
		for(int m = 0; m < n; m++)
			for(int i = 0; i < n; i++)
				for(int j = 0; j < n; j++)
					dp[i][j][t] = min(dp[i][j][t], dp[i][m][t - 1] + dp[m][j][0]);
	for(int i = 0; i < r; i++) {
		int s, t, k;
		cin >> t >> s >> k;
		k = min(MAXN - 1, k);
		cout << dp[t - 1][s - 1][k] << endl;
	}
}


#include<bits/stdc++.h>
#define LL long long
#define  N 100005
using namespace std;
inline int ra()
{
    int x=0,f=1; char ch=getchar();
    while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
    while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}
int f[65][65][65],g[65][65][65];
int main()
{
    int n=ra(),m=ra(),r=ra();
    memset(f,0x7f,sizeof(f));
    for (int i=1; i<=m; i++)
        for (int j=1; j<=n; j++)
            for (int k=1; k<=n; k++)
                g[i][j][k]=ra();
    for (int id=1; id<=m; id++)
        for (int k=1; k<=n; k++)
            for (int i=1; i<=n; i++)
                for (int j=1; j<=n; j++)
                    g[id][i][j]=min(g[id][i][j],g[id][i][k]+g[id][k][j]);
        for (int i=1; i<=n; i++)
            for (int j=1; j<=n; j++)
                for (int k=1; k<=m; k++)
                    f[i][j][0]=min(f[i][j][0],g[k][i][j]);
        for (int e=1; e<=n; e++)
            for (int k=1 ;k<=n; k++)
                for (int i=1; i<=n; i++)
                    for (int j=1; j<=n; j++)
                        f[i][j][e]=min(f[i][j][e],f[i][k][e-1]+f[k][j][0]);
        for (int i=1; i<=r; i++)
        {
            int s=ra(),t=ra(),k=ra();
            k=min(k,n);
            printf("%d\n",f[s][t][k]);
        }
        return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define pb emplace_back
#define fi first
#define se second
#define int long long int
const int mod = 1e9 + 7;
#define pii pair<int,int>
#define all(x) (x).begin(),(x).end()
typedef long double f80;
const int inf = 1e9;

const int N = 200005;
pair<int,int> h[N];
inline bool cmp(const pair<int,int> &a, const pair<int,int> &b){
    return a.fi - a.se > b.fi - b.se;
}
int pre[N], pre1[N];
signed main(){
    //freopen("inp.txt", "r", stdin);
    int n, a, b;
    scanf("%lld %lld %lld", &n, &a, &b);
    a = 1 << a;
    for(int i = 1; i <= n; i++)
        scanf("%lld %lld", &h[i].fi, &h[i].se);
    sort(h + 1, h + n + 1, cmp);
    for(int i = 1; i <= n; i++){
        pre[i] = pre[i - 1] + h[i].fi;
        pre1[i] = pre1[i - 1] + h[i].se;
    }
    int j = 0;
    for(int i = 1; i <= b; i++){
        if(h[i].fi - h[i].se > 0)
            j = i;
        else
            break;
    }
    int ans = pre1[n];
    for(int i = 1; i <= j; i++){
        int val = pre[j] + (a - 1) * h[i].fi + pre1[n] - pre1[j];
        ans = max(ans, val);
    }
    if(j > 0)
    for(int i = j + 1; i <= n; i++){
        int val = pre1[n] - h[i].se + a * h[i].fi;
        val += pre[j - 1] - pre1[j - 1];
        ans = max(ans, val);
    }
    printf("%lld", ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define pb emplace_back
#define fi first
#define se second
#define int long long int
const int mod = 1e9 + 7;
#define pii pair<int,int>
#define all(x) (x).begin(),(x).end()
typedef long double f80;
const int inf = 1e9;

const int N = 200005;
pair<int,int> h[N];
inline bool cmp(const pair<int,int> &a, const pair<int,int> &b){
    return a.fi - a.se > b.fi - b.se;
}
int pre[N], pre1[N];
signed main(){
    //freopen("inp.txt", "r", stdin);
    int n, a, b;
    scanf("%lld %lld %lld", &n, &a, &b);
    a = 1 << a;
    for(int i = 1; i <= n; i++)
        scanf("%lld %lld", &h[i].fi, &h[i].se);
    sort(h + 1, h + n + 1, cmp);
    for(int i = 1; i <= n; i++){
        pre[i] = pre[i - 1] + h[i].fi;
        pre1[i] = pre1[i - 1] + h[i].se;
    }
    int j = 0;
    for(int i = 1; i <= b; i++){
        if(h[i].fi - h[i].se > 0)
            j = i;
        else
            break;
    }
    int ans = pre1[n];
    for(int i = 1; i <= j; i++){
        int val = pre[j] + (a - 1) * h[i].fi + pre1[n] - pre1[j];
        ans = max(ans, val);
    }
    if(b > 0)
    for(int i = j + 1; i <= n; i++){
        int val = pre1[n] - h[i].se + a * h[i].fi;
        if(j > 0)
        val += pre[j - 1] - pre1[j - 1];
        ans = max(ans, val);
    }
    printf("%lld", ans);
    return 0;
}


#include <bits/stdc++.h>

#ifdef DEBUG
#define LOG(var, sep) (cerr << #var << " = " << (var) << (sep))

#define LOGM(msg) (cerr << (msg))

#define LOGV(vec, sep) {                                        \
    cerr << #vec << " =";                                       \
    for (const auto& v : (vec)) cerr << " " << v;               \
    cerr << (sep); }

#define LOGA(arr, N, sep) {                                     \
        cerr << #arr << " =";                                   \
        for (int i = 0; i < N; ++i) cerr << " " << (arr)[i];    \
        cerr << (sep); }

#define LOGA2(arr, N, M, sep) {                                 \
        cerr << #arr << " =\n";                                 \
        for (int i = 0; i < N; ++i) {                           \
            for (int j = 0; j < M; ++j)                         \
                cerr << (arr)[i][j] << " ";                     \
            cerr << endl;                                       \
        } cerr << (sep); }
#else
#define LOG(var, sep)
#define LOGM(msg)
#define LOGV(vec, sep)
#define LOGA(arr, N, sep)
#define LOGA2(arr, N, M, sep)
#endif

using namespace std;
using ll = long long;
using ii = pair<ll, ll>;

ostream& operator<<(ostream& os, const ii& p)
{
    os << "(" << p.first << ", " << p.second << ")";

    return os;
}

ll solve(int N, int A, int B, const vector<ii>& xs)
{
    ll ans = 0, best = 0, v = 0, j = -1;

    for (int i = 0; i < N; ++i)
    {
        auto [h, dmg] = xs[i];

        for (int k = 0; k < A; ++k)
            h *= 2;

        if (h > dmg and h - dmg > best)
        {
            best = h - dmg;
            v = h;
            j = i;
        }
    }

    LOGM("=== "); LOG(best, ' '); LOG(j, endl);

    if (j >= 0 and B)
    {
        if (v > xs[j].second)
            --B;

        ans += v;
    } else
        j = -1;

    LOG(ans, ' '); LOG(B, endl);

    priority_queue<ii> pq;

    for (int i = 0; i < N; ++i)
    {
        if (i == j)
            continue;

        auto [h, dmg] = xs[i];

        pq.push(ii(h - dmg, i));
    }

    while (not pq.empty())
    {
        auto [x, i] = pq.top();
        pq.pop();

        LOGM("--- "); LOG(x, ' '); LOG(i, ' '); LOG(xs[i], endl);

        if (x > 0 and B)
        {
            ans += xs[i].first;
            --B;
        } else
            ans += xs[i].second;

        LOG(ans, ' '); LOG(B, endl);
    }

    return ans;

}

int main()
{
    ios::sync_with_stdio(false);

    int N, A, B;
    cin >> N >> A >> B;

    vector<ii> xs(N);

    for (int i = 0; i < N; ++i)
        cin >> xs[i].first >> xs[i].second;

    auto ans = solve(N, A, B, xs);

    cout << ans << '\n';

    return 0;
}


#include <bits/stdc++.h>

#ifdef DEBUG
#define LOG(var, sep) (cerr << #var << " = " << (var) << (sep))

#define LOGM(msg) (cerr << (msg))

#define LOGV(vec, sep) {                                        \
    cerr << #vec << " =";                                       \
    for (const auto& v : (vec)) cerr << " " << v;               \
    cerr << (sep); }

#define LOGA(arr, N, sep) {                                     \
        cerr << #arr << " =";                                   \
        for (int i = 0; i < N; ++i) cerr << " " << (arr)[i];    \
        cerr << (sep); }

#define LOGA2(arr, N, M, sep) {                                 \
        cerr << #arr << " =\n";                                 \
        for (int i = 0; i < N; ++i) {                           \
            for (int j = 0; j < M; ++j)                         \
                cerr << (arr)[i][j] << " ";                     \
            cerr << endl;                                       \
        } cerr << (sep); }
#else
#define LOG(var, sep)
#define LOGM(msg)
#define LOGV(vec, sep)
#define LOGA(arr, N, sep)
#define LOGA2(arr, N, M, sep)
#endif

using namespace std;
using ll = long long;
using ii = pair<ll, ll>;
using iii = tuple<int, int, int>;

ostream& operator<<(ostream& os, const ii& p)
{
    os << "(" << p.first << ", " << p.second << ")";

    return os;
}

const int MAX { 410 }, MAXA { 22 }, MAXB { 22 };

ll st[MAX][MAXA][MAXB];
map<iii, int> ps;

ll dp(int i, int a, int b, int N, const vector<ii>& xs)
{
    if (i == N)
        return 0;

    if (st[i][a][b] != -1)
        return st[i][a][b];

    auto [h, dmg] = xs[i];

    auto res = dp(i + 1, a, b, N, xs);

    if (b > 0)
    {
        auto x = h;

        for (int k = 1; k <= a; ++k)
        {
            x *= 2;

            if (x > dmg)
            {
                auto sol = dp(i + 1, a - k, b - 1, N, xs) + (x - dmg);

                if (sol > res)
                {
                    res = sol;
                    ps[iii(i, a, b)] = k;
                }
            }
        }
    }

    st[i][a][b] = res;
    return res;
}

ll solve(int N, int A, int B, const vector<ii>& xs)
{
    priority_queue<ii, vector<ii>, greater<ii>> min_heap;

    for (int i = 0; i < N; ++i)
    {
        auto [h, dmg] = xs[i];

        for (int k = 1; k <= A; ++k)
        {
            h *= 2;

            if (h > dmg)
            {
                min_heap.push(ii(h - dmg, i));

                if (min_heap.size() > MAX)
                    min_heap.pop();
            }
        }
    }

    vector<ii> ys;
    vector<ll> is;
    set<ll> marked;

    while (not min_heap.empty())
    {
        auto [_, j] = min_heap.top();
        min_heap.pop();

        if (not marked.count(j))
        {
            marked.insert(j);
            ys.push_back(xs[j]);
            is.push_back(j);
        }
    }

    ll ans = 0;
    set<ll> used;
    memset(st, -1, sizeof st);

    LOGV(ys, endl);
    LOGV(is, endl);

    dp(0, A, min(B, A), (int) ys.size(), ys);

    int i = 0, a = A, b = min(B, A);

    while (i < N)
    {
        auto k = ps[iii(i, a, b)];

        if (k)
        {
            used.insert(is[i]);

            auto [x, y] = ys[i];

            for (int r = 0; r < k; ++r)
                x *= 2;

            ans += x;

            a -= k;
            b -= 1;
            --B;
        }

        ++i;
    }

    LOG(ans, endl);
    LOGV(used, endl);

    priority_queue<iii> pq;

    for (int j = 0; j < N; ++j)
    {
        if (used.count(j))
            continue;

        auto [h, dmg] = xs[j];

        if (h > dmg)
            pq.push(iii(h - dmg, -dmg, j));
    }

    while (B and not pq.empty())
    {
        auto [x, h, j] = pq.top();
        pq.pop();

        ans += xs[j].first;
        used.insert(j);
        --B;
    }

    for (int j = 0; j < N; ++j)
        if (used.count(j) == 0)
            ans += xs[j].second;

    return ans;

}

int main()
{
    ios::sync_with_stdio(false);

    int N, A, B;
    cin >> N >> A >> B;

    vector<ii> xs(N);

    for (int i = 0; i < N; ++i)
        cin >> xs[i].first >> xs[i].second;

    auto ans = solve(N, A, B, xs);

    cout << ans << '\n';

    return 0;
}


#include <bits/stdc++.h>

#ifdef DEBUG
#define LOG(var, sep) (cerr << #var << " = " << (var) << (sep))

#define LOGM(msg) (cerr << (msg))

#define LOGV(vec, sep) {                                        \
    cerr << #vec << " =";                                       \
    for (const auto& v : (vec)) cerr << " " << v;               \
    cerr << (sep); }

#define LOGA(arr, N, sep) {                                     \
        cerr << #arr << " =";                                   \
        for (int i = 0; i < N; ++i) cerr << " " << (arr)[i];    \
        cerr << (sep); }

#define LOGA2(arr, N, M, sep) {                                 \
        cerr << #arr << " =\n";                                 \
        for (int i = 0; i < N; ++i) {                           \
            for (int j = 0; j < M; ++j)                         \
                cerr << (arr)[i][j] << " ";                     \
            cerr << endl;                                       \
        } cerr << (sep); }
#else
#define LOG(var, sep)
#define LOGM(msg)
#define LOGV(vec, sep)
#define LOGA(arr, N, sep)
#define LOGA2(arr, N, M, sep)
#endif

using namespace std;
using ll = long long;
using ii = pair<ll, ll>;
using iii = tuple<int, int, int>;

ostream& operator<<(ostream& os, const ii& p)
{
    os << "(" << p.first << ", " << p.second << ")";

    return os;
}

const int MAX { 410 }, MAXA { 22 }, MAXB { 22 };

ll st[MAX][MAXA][MAXB];
map<iii, int> ps;

ll dp(int i, int a, int b, int N, const vector<ii>& xs)
{
    if (i == N)
        return 0;

    if (st[i][a][b] != -1)
        return st[i][a][b];

    auto [h, dmg] = xs[i];

    auto res = dp(i + 1, a, b, N, xs);

    if (b > 0)
    {
        auto x = h;

        for (int k = 1; k <= a; ++k)
        {
            x *= 2;

            if (x > dmg)
            {
                auto sol = dp(i + 1, a - k, b - 1, N, xs) + (x - dmg);

                if (sol > res)
                {
                    res = sol;
                    ps[iii(i, a, b)] = k;
                }
            }
        }
    }

    st[i][a][b] = res;
    return res;
}

ll solve(int N, int A, int B, const vector<ii>& xs)
{
    priority_queue<ii, vector<ii>, greater<ii>> min_heap;

    for (int i = 0; i < N; ++i)
    {
        auto [h, dmg] = xs[i];

        for (int k = 1; k <= A; ++k)
        {
            h *= 2;

            if (h > dmg)
            {
                min_heap.push(ii(h - dmg, i));

                if (min_heap.size() > MAX)
                    min_heap.pop();
            }
        }
    }

    vector<ii> ys;
    vector<ll> is;
    set<ll> marked;

    while (not min_heap.empty())
    {
        auto [_, j] = min_heap.top();
        min_heap.pop();

        if (not marked.count(j))
        {
            marked.insert(j);
            ys.push_back(xs[j]);
            is.push_back(j);
        }
    }

    ll ans = 0;
    set<ll> used;
    memset(st, -1, sizeof st);

    LOGV(ys, endl);
    LOGV(is, endl);

    dp(0, A, min(B, A), (int) ys.size(), ys);

    int i = 0, a = A, b = min(B, A);

    while (i < N)
    {
        auto k = ps[iii(i, a, b)];

        if (k)
        {
            used.insert(is[i]);

            auto [x, y] = ys[i];

            for (int r = 0; r < k; ++r)
                x *= 2;

            ans += x;

            a -= k;
            b -= 1;
            --B;
        }

        ++i;
    }

    LOG(ans, endl);
    LOGV(used, endl);

    priority_queue<iii> pq;

    for (int j = 0; j < N; ++j)
    {
        if (used.count(j))
            continue;

        auto [h, dmg] = xs[j];

        if (h > dmg)
            pq.push(iii(h - dmg, h, j));
    }

    while (B and not pq.empty())
    {
        auto [x, h, j] = pq.top();
        pq.pop();

        ans += xs[j].first;
        used.insert(j);
        --B;
    }

    for (int j = 0; j < N; ++j)
        if (used.count(j) == 0)
            ans += xs[j].second;

    return ans;

}

int main()
{
    ios::sync_with_stdio(false);

    int N, A, B;
    cin >> N >> A >> B;

    vector<ii> xs(N);

    for (int i = 0; i < N; ++i)
        cin >> xs[i].first >> xs[i].second;

    auto ans = solve(N, A, B, xs);

    cout << ans << '\n';

    return 0;
}


#include <bits/stdc++.h>

#ifdef DEBUG
#define LOG(var, sep) (cerr << #var << " = " << (var) << (sep))

#define LOGM(msg) (cerr << (msg))

#define LOGV(vec, sep) {                                        \
    cerr << #vec << " =";                                       \
    for (const auto& v : (vec)) cerr << " " << v;               \
    cerr << (sep); }

#define LOGA(arr, N, sep) {                                     \
        cerr << #arr << " =";                                   \
        for (int i = 0; i < N; ++i) cerr << " " << (arr)[i];    \
        cerr << (sep); }

#define LOGA2(arr, N, M, sep) {                                 \
        cerr << #arr << " =\n";                                 \
        for (int i = 0; i < N; ++i) {                           \
            for (int j = 0; j < M; ++j)                         \
                cerr << (arr)[i][j] << " ";                     \
            cerr << endl;                                       \
        } cerr << (sep); }
#else
#define LOG(var, sep)
#define LOGM(msg)
#define LOGV(vec, sep)
#define LOGA(arr, N, sep)
#define LOGA2(arr, N, M, sep)
#endif

using namespace std;
using ll = long long;
using ii = pair<ll, ll>;

ostream& operator<<(ostream& os, const ii& p)
{
    os << "(" << p.first << ", " << p.second << ")";

    return os;
}

ll solve(int N, int A, int B, vector<ii>& xs)
{
    sort(xs.begin(), xs.end(), [](const ii& x, const ii& y) {
        auto a = x.first - x.second;
        auto b = y.first - y.second;

        if (a != b)
            return a > b;

        return x.second > y.second;
    });

    LOGV(xs, endl);

    ll sum = 0;

    for (int i = 0; i < N; ++i)
    {
        auto [h, dmg] = xs[i];

        sum += (i < B ? max(h, dmg) : dmg);
    }

    LOG(sum, endl);

    if (A == 0)
        return sum;

    ll ans = sum, k = (1 << A);

    for (int i = 0; i < N; ++i)
    {
        auto res = sum;
        auto [h, dmg] = xs[i];

        if (i < B)
        {
            res -= max(h, dmg);
            LOG(h, ' '); LOG(dmg, ' '); LOG(max(h, dmg), ' '); LOG(k*h, endl);
            res += max(k*h, dmg);
        } else if (B and k*h > dmg)
        {
            res -= dmg;
            res += k*h;
            res -= max(xs[B - 1].first, xs[B - 1].second);
            res += xs[B - 1].second;
        }

        LOG(i, ' '); LOG(res, endl);
        ans = max(ans, res);
    }

    return ans;

}

int main()
{
    ios::sync_with_stdio(false);

    int N, A, B;
    cin >> N >> A >> B;

    vector<ii> xs(N);

    for (int i = 0; i < N; ++i)
        cin >> xs[i].first >> xs[i].second;

    auto ans = solve(N, A, B, xs);

    cout << ans << '\n';

    return 0;
}


#include <bits/stdc++.h>

#ifdef DEBUG
#define LOG(var, sep) (cerr << #var << " = " << (var) << (sep))

#define LOGM(msg) (cerr << (msg))

#define LOGV(vec, sep) {                                        \
    cerr << #vec << " =";                                       \
    for (const auto& v : (vec)) cerr << " " << v;               \
    cerr << (sep); }

#define LOGA(arr, N, sep) {                                     \
        cerr << #arr << " =";                                   \
        for (int i = 0; i < N; ++i) cerr << " " << (arr)[i];    \
        cerr << (sep); }

#define LOGA2(arr, N, M, sep) {                                 \
        cerr << #arr << " =\n";                                 \
        for (int i = 0; i < N; ++i) {                           \
            for (int j = 0; j < M; ++j)                         \
                cerr << (arr)[i][j] << " ";                     \
            cerr << endl;                                       \
        } cerr << (sep); }
#else
#define LOG(var, sep)
#define LOGM(msg)
#define LOGV(vec, sep)
#define LOGA(arr, N, sep)
#define LOGA2(arr, N, M, sep)
#endif

using namespace std;
using ll = long long;
using ii = pair<ll, ll>;
using iii = tuple<int, int, int>;

ostream& operator<<(ostream& os, const ii& p)
{
    os << "(" << p.first << ", " << p.second << ")";

    return os;
}

ll solve(int N, int A, int B, const vector<ii>& xs)
{
    ll ans = 0, best = 0, j = -1;

    for (int i = 0; i < N; ++i)
    {
        auto [h, dmg] = xs[i];

        for (int k = 0; k < A; ++k)
            h *= 2;

        if (h > dmg and h - dmg > best)
        {
            best = h - dmg;
            j = i;
        }
    }

    LOGM("=== "); LOG(best, ' '); LOG(j, endl);

    if (j >= 0 and B)
    {
        --B;
        ans += (best + xs[j].second);
    } else
        j = -1;

    LOG(ans, ' '); LOG(B, endl);

    priority_queue<ii> pq;

    for (int i = 0; i < N; ++i)
    {
        if (i == j)
            continue;

        auto [h, dmg] = xs[i];

        pq.push(ii(h - dmg, i));
    }

    while (not pq.empty())
    {
        auto [x, i] = pq.top();
        pq.pop();

        LOGM("--- "); LOG(x, ' '); LOG(i, ' '); LOG(xs[i], endl);

        if (x > 0 and B)
        {
            ans += xs[i].first;
            --B;
        } else
            ans += xs[i].second;

        LOG(ans, ' '); LOG(B, endl);
    }

    return ans;

}

int main()
{
    ios::sync_with_stdio(false);

    int N, A, B;
    cin >> N >> A >> B;

    vector<ii> xs(N);

    for (int i = 0; i < N; ++i)
        cin >> xs[i].first >> xs[i].second;

    auto ans = solve(N, A, B, xs);

    cout << ans << '\n';

    return 0;
}