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

typedef pair<int,int> pii;

string b[9];

inline bool ok9(const bool c[9]) {
  bool r = true;
  for (int i = 0; i < 9; i++)
    r = (r && c[i]);
  return r;
}

int main() {
  ios::sync_with_stdio(0);
  int n; cin >> n;
  while (n--) {
    vector<pii> v;
    for (int i = 0; i < 9; i++) {
      cin >> b[i];
      for (int j = 0; j < 9; j++)
        if (b[i][j] == '0')
          v.push_back(make_pair(i,j));
    }
    for (char A = '1'; A <= '9'; A++) {
      for (char B = '1'; B <= '9'; B++) {
        for (char C = '1'; C <= '9'; C++) {
          for (char D = '1'; D <= '9'; D++) {
            for (char E = '1'; E <= '9'; E++) {
              b[v[0].first][v[0].second] = A;
              b[v[1].first][v[1].second] = B;
              b[v[2].first][v[2].second] = C;
              b[v[3].first][v[3].second] = D;
              b[v[4].first][v[4].second] = E;
              bool ok = true;
              for (int i = 0; i < 9; i++) {
                bool r[9];
                memset(r, 0, sizeof r);
                for (int j = 0; j < 9; j++)
                  r[b[i][j]-'1'] = true;
                if (!ok9(r)) {
                  ok = false;
                  break;
                }
                memset(r, 0, sizeof r);
                for (int j = 0; j < 9; j++)
                  r[b[j][i]-'1'] = true;
                if (!ok9(r)) {
                  ok = false;
                  break;
                }
              }
              for (int i = 0; i < 9; i += 3) {
                for (int j = 0; j < 9; j += 3) {
                  bool r[9];
                  memset(r, 0, sizeof r);
                  for (int k = i; k < i+3; k++)
                    for (int l = j; l < j+3; l++)
                      r[b[k][l]-'1'] = true;
                  if (!ok9(r)) {
                    ok = false;
                    goto VELOCIRAPTOR;
                  }
                }
              }
VELOCIRAPTOR:
              if (ok) {
                for (int i = 0; i < 9; i++)
                  cout << b[i] << "\n";
                goto GOOD_CONTROL_FLOW;
              }
            }
          }
        }
      }
    }
    cout << "Could not complete this grid.\n";
GOOD_CONTROL_FLOW:
    cout << "\n";
  }
  return 0;
}