/*
这个题目稀里糊涂地就过了,是zhc说那种方法可以过,结果真的AC
但是想想感觉是可以的,每次去最大的进行更新,有点像Dijsk的,只是Dijsk 是每次去最小进行更新
可能是由于是小于1的小数吧
可是课件上的提示感觉就是用不起来的
用Floyd 超时了。。。
*/
#include <iostream>//2568534 2010-07-03 23:55:15 Accepted 1596 984MS 6880K 1356 B C++ 悔惜晟
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N = 1005;
double dis[N];
double cost[N][N];
bool hash[N];
struct node
{
int num;
double dis;
friend bool operator < (node a, node b)
{
return a.dis < b.dis;
}
};
double BFS(int start ,int end, int n)
{
priority_queue<node>Q;
node P, M;
memset(hash, false, sizeof(hash));
for(int i= 1; i <= n; i++)
{
dis[i] = cost[start][i];
if(i != start)
{
M.num = i;
M.dis = dis[i];
Q.push(M);
}
}
hash[start] = true;
while(!Q.empty())
{
P = Q.top();
Q.pop();
if(hash[end])
{
//return P.dis;
break;
}
hash[P.num] = true;
for(int i = 1; i <= n; i++)
{
if(!hash[i] && P.dis * cost[P.num][i] > dis[i])
{
dis[i] = P.dis * cost[P.num][i];
M.dis = dis[i];
M.num = i;
Q.push(M);
}
}
}
if(dis[end] == 0)
return 0;
else
return dis[end];
}
int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
scanf("%lf", &cost[i][j]);
int q;
scanf("%d", &q);
int a, b;
while(q--)
{
scanf("%d %d", &a, &b);
double t = BFS(a, b, n);
if(t == 0)
printf("What a pity!/n");
else
printf("%0.3lf/n", t);
}
}
}