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

string grid[9];
int n;

bool is_valid() {
  for (int j = 0; j < 9; j++) {
    int sum = 0;
    int signature = 0;
    int sum2 = 0;
    int signature2 = 0;
    for (int i = 0; i < 9; i++) {
      sum += grid[j][i];
      signature ^= grid[j][i];
      sum2 += grid[i][j];
      signature2 ^= grid[i][j];
    }
    if (sum != 477 || sum2 != 477 || signature != 49 || signature2 != 49) {
      return false;
    }
  }

  for (int j = 0; j < 3; j++) {
    for (int i = 0; i < 3; i++) {
      int sum = 0;
      int signature = 0;
      for (int m = 0; m < 3; m++) {
        for (int n = 0; n < 3; n++) {
          sum += grid[j * 3 + m][i * 3 + n];
          signature ^= grid[j * 3 + m][i * 3 + n];
        }
      }
      if (sum != 477 || signature != 49) {
        return false;
      }
    }
  }
  return true; 
}

void print() {
  for (int j = 0; j < 9; j++) {
      cout << grid[j] << '\n';
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  while (n--) {
    vector<pair<int, int>> pos;
    pos.clear();
    for (int j = 0; j < 9; j++) {
      cin >> grid[j];
      for (int i = 0; i < 9; i++) {
        if (grid[j][i] == '0') {
          pos.push_back({j, i});
        }
      }
    }
    bool valid = false;
    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++) {
              grid[pos[0].first][pos[0].second] = a;
              grid[pos[1].first][pos[1].second] = b;
              grid[pos[2].first][pos[2].second] = c;
              grid[pos[3].first][pos[3].second] = d;
              grid[pos[4].first][pos[4].second] = e;
              if (is_valid()) {
                valid = true;
                goto outtahere;
              }
            }
          }
        }
      }
    }
outtahere:
    if (valid) {
      print();
    } else {
      cout << "Could not complete this grid.\n";
    }
    cout << '\n';
  }
  return 0;
}