#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool memBeg;
constexpr int maxn = 5005;
int n;
char mp[maxn][maxn], ret[maxn][maxn];
vector<int> buc[maxn];
struct segment_tree {
int maxx[maxn << 2], tag[maxn << 2];
void push_up(int cur) {
maxx[cur] = max(maxx[cur << 1], maxx[cur << 1 | 1]) + tag[cur];
}
void build(int tl, int tr, int cur) {
tag[cur] = 0;
if (tl == tr) return maxx[cur] = tl, void();
int mid = (tl + tr) >> 1;
build(tl, mid, cur << 1);
build(mid + 1, tr, cur << 1 | 1);
push_up(cur);
}
void modify(int tl, int tr, int cur, int l, int r, int v) {
if (l <= tl && tr <= r) return maxx[cur] += v, tag[cur] += v, void();
int mid = (tl + tr) >> 1;
if (l <= mid) modify(tl, mid, cur << 1, l, r, v);
if (r > mid) modify(mid + 1, tr, cur << 1 | 1, l, r, v);
push_up(cur);
}
int query(int tl, int tr, int cur, int l, int r) {
if (l <= tl && tr <= r) return maxx[cur];
int mid = (tl + tr) >> 1;
if (l > mid) return query(mid + 1, tr, cur << 1 | 1, l, r) + tag[cur];
else if (r <= mid) return query(tl, mid, cur << 1, l, r) + tag[cur];
else return max(query(tl, mid, cur << 1, l, r),
query(mid + 1, tr, cur << 1 | 1, l, r)) + tag[cur];
}
} T;
void mian() {
scanf("%d", &n);
for (int i = 0; i <= n; i++) buc[i].clear();
for (int i = 1; i <= n; i++) {
scanf("%s", mp[i] + 1);
ret[i][i + 1] = 0;
for (int j = 1; j <= i; j++) {
if (mp[i][j] == '0') {
ret[i][j] = '-';
buc[i - j].emplace_back(j);
}
}
}
for (int i = n; i >= 2; i--) {
static int stk[maxn];
int top = 0;
for (int j = 1; j <= i; j++) {
if (mp[i][j] == '0') stk[++top] = j;
}
if (!top) {
return puts("Impossible!"), 0;
}
fill(ret[i] + 1, ret[i] + stk[1], '3');
fill(ret[i] + stk[top] + 1, ret[i] + i + 1, '1');
T.build(0, i - 1, 1);
auto ins = [&](int col) {
for (int v : buc[i - 1 - col]) {
if (v >= col) break;
T.modify(0, i - 1, 1, 0, v - 1, 1);
}
if (mp[i - 1][col] == '0') T.modify(0, i - 1, 1, 0, col - 1, 1);
};
for (int k = 1; k < stk[1]; k++) ins(k);
for (int j = 1; j < top; j++) {
int pos = stk[j];
for (int k = stk[j]; k < stk[j + 1]; k++) {
ins(k); if (T.query(0, i - 1, 1, 0, stk[j] - 1) == k) pos = k + 1;
}
// printf("i = %d, j = %d, l = %d, r = %d, pos = %d\n",
// i, j, stk[j], stk[j + 1], pos);
if (pos == stk[j + 1] || mp[i - 1][pos] == '0') return puts("Impossible!"), 0;
fill(ret[i] + stk[j] + 1, ret[i] + pos + 1, '1');
fill(ret[i] + pos + 1, ret[i] + stk[j + 1], '3');
mp[i - 1][pos] = '0'; ret[i - 1][pos] = '2';
T.modify(0, i - 1, 1, 0, pos - 1, 1);
}
}
for (int i = 1; i <= n; i++) {
printf("%s\n", ret[i] + 1);
}
}
bool memEn;
void fl() {
freopen("triangle.in","r",stdin);
freopen("triangle.out","w",stdout);
}
int main() {
fprintf(stderr,"%.24lf\n",fabs(&memEn-&memBeg)/1024.0/1024.0);
// fl();
int _; scanf("%d", &_);
while (_--) mian();
return 0;
}