#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
template <class _E> inline void read(_E &e)
{
e=0;bool ck=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')ck=1;ch=getchar();}
while(ch<='9'&&ch>='0'){e=e*10+ch-'0';ch=getchar();}
if(ck)e=-e;
}
int t,n;
char inf[]={"infty"};
int dis[101][101];
int cnt[101][101];
inline void floyd1()
{
for (int k=1;k<=n;++k)
for (int i=1;i<=n;++i)
for (int j=1;j<=n;++j)
if (i!=j && j!=k && i!=k)
if (dis[i][j]==dis[i][k]+dis[k][j])
++cnt[i][j];
}
int main()
{
read(t);
while (t--)
{
read(n);
memset(cnt,0,sizeof cnt);
for (int i=1;i<=n;++i)
for (int j=1;j<=n;++j)
read(dis[i][j]);
floyd1();
for (int i=1;i<=n;++i)
{
for (int j=1;j<=n;++j)
{
if (cnt[i][j])
printf("%s ",inf);
else printf("%d ",dis[i][j]);
}
puts("");
}
}
return 0;
}