#include <iostream>
#include <algorithm>
#include <climits>
using namespace std;
int A[510][510][24];
int dist[510][24];
bool sptSet[510];
int minDistance(int i, int V)
{
// Initialize min value
int _min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (sptSet[v] == false && dist[v][i] <= _min)
_min = dist[v][i], min_index = v;
return min_index;
}
void dijkstra(int x, int V, int src)
{
// Distance of source vertex from itself is always 0
dist[src][x] = 0;
// Find shortest path for all vertices
for (int count = 0; count < V-1; count++)
{
// Pick the minimum distance vertex from the set of vertices not
// yet processed. u is always equal to src in first iteration.
int u = minDistance(x, V);
// Mark the picked vertex as processed
sptSet[u] = true;
// Update dist value of the adjacent vertices of the picked vertex.
for (int v = 0; v < V; v++)
// Update dist[v] only if is not in sptSet, there is an edge from
// u to v, and total weight of path from src to v through u is
// smaller than current value of dist[v]
if (!sptSet[v] && A[u][v][x] && dist[u][x] != INT_MAX
&& dist[u][x]+A[u][v][x] < dist[v][x])
dist[v][x] = dist[u][x] + A[u][v][x];
}
}
int main() {
// your code goes here
int N, K, T, M, x, y, i, j, D, S, k, t=1;
cin>>T;
while(T--)
{
cin>>N>>M>>K;
for(i=0; i<N; ++i)
{
for(j=0; j<24; ++j)
dist[i][j]=INT_MAX;
}
for(i=0; i<N; ++i)
for(j=0; j<N; ++j)
for(k=0; k<24; ++k)
A[i][j][k]=0;
for(i=0; i<M; ++i)
{
cin>>x>>y;
for(j=0; j<24; ++j)
{
cin>>A[x-1][y-1][j];
A[y-1][x-1][j]=A[x-1][y-1][j];
}
}
for(i=0; i<24; ++i)
{
for(j=0; j<N; ++j)
sptSet[j]=false;
dijkstra(i, N, 0);
}
cout<<"Case #"<<t<<": ";
while(K--)
{
cin>>D>>S;
if(dist[D-1][S]==INT_MAX)
cout<<"-1 ";
else
cout<<dist[D-1][S]<<" ";
}
cout<<endl;
t++;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8Y2xpbW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DEzIDEyIDExIDEwIDkgOAoxMiAxMwozIDE0IDI0IDIzIDIzIDIyIDIxIDIwIDE5IDE4IDE3IDE2IDE1IDE0IDEzIDEyIDExIDEwIDkgOCA3IDYgNSA0CjE0IDE1CjEgMTQgMjIgMjIgMjEgMjAgMTkgMTggMTcgMTYgMTUgMTQgMTMgMTIgMTEgMTAgOSA4IDcgNiA1IDQgMyAyCjIgMgoyIDcKMiAxOAoyIDIwCjIgMjIKNCA5CjQgMjAKNCAyMQo0IDIzCjUgNwo1IDgKNSAxNQo1IDE2CjUgMTkKNSAyMQo2IDMKNiA0CjYgNQo2IDIwCjggMwo4IDgKOCAxMwo4IDE0CjggMjMKOSAxCjkgNQo5IDYKOSA3CjEwIDE1CjEwIDIyCjExIDcKMTEgMTUKMTEgMTgKMTIgMAoxMiA3CjEyIDExCjEyIDEzCjEyIDE1CjEyIDIxCjEzIDIKMTMgNQoxMyAxNwoxMyAyMQoxNCAxMQoxNCAxNAoxNSAzCjE1IDEwCjE1IDEzCjE1IDE2CjE1IDIx
1
15 40 50
1 2
10 24 30 29 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11
1 5
5 13 21 21 21 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6
1 10
5 20 20 19 21 23 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6
1 12
9 20 27 28 28 27 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10
1 14
9 8 25 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10
2 3
2 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3
2 5
9 16 15 24 24 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10
2 6
5 18 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6
2 7
8 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9
2 8
8 28 27 27 26 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9
2 11
7 28 27 26 25 24 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8
2 13
9 15 20 29 28 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10
3 4
6 27 27 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7
3 11
9 14 23 27 28 27 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10
3 12
1 22 22 21 20 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2
3 14
3 20 21 20 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4
4 7
9 21 30 29 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10
4 8
10 16 26 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11
4 12
8 30 29 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9
4 13
7 20 23 26 25 24 24 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8
5 6
7 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8
5 15
3 21 23 22 21 20 20 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4
6 8
6 14 26 25 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7
6 9
4 9 22 22 22 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5
6 11
4 15 21 24 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5
6 12
8 21 29 28 27 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9
7 10
9 23 26 27 26 27 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10
7 11
8 26 27 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9
7 12
10 20 31 30 29 28 27 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11
7 14
3 19 18 21 20 21 20 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4
8 9
2 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3
8 10
4 8 19 18 17 20 19 20 19 18 17 16 16 15 14 13 12 11 10 9 8 7 6 5
8 12
3 11 20 21 22 21 20 19 18 17 16 15 15 14 13 12 11 10 9 8 7 6 5 4
8 14
9 10 27 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10
9 12
5 26 25 25 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6
9 15
3 2 15 18 17 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4
10 11
5 22 21 23 24 23 22 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6
11 15
7 11 13 24 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8
12 13
3 14 24 23 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4
14 15
1 14 22 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2
2 2
2 7
2 18
2 20
2 22
4 9
4 20
4 21
4 23
5 7
5 8
5 15
5 16
5 19
5 21
6 3
6 4
6 5
6 20
8 3
8 8
8 13
8 14
8 23
9 1
9 5
9 6
9 7
10 15
10 22
11 7
11 15
11 18
12 0
12 7
12 11
12 13
12 15
12 21
13 2
13 5
13 17
13 21
14 11
14 14
15 3
15 10
15 13
15 16
15 21