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

#define fi first
#define se second
#define pb push_back
#define sz(x) ((int) x.size())
#define all(x) (x).begin(), (x).end()
#define re return
#define mp make_pair
#define makeunique(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define endl '\n'

typedef pair<int, int> ii;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef vector<string> vs;

template <class T> T abs (T x) { re x > 0 ? x : -x; }
template <class T> T sqr (T x) { re x * x; }

const double eps = 1e-8;
const double pi = acos(-1.);
const int inf = 1e9 + 7;
const int N = 3e5 + 20;

int n, m;
int was[301][301];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

void dfs(int x, int y, vector<ii>& cur, vs& s) {
    was[x][y] = 1;
    cur.pb({x, y});
    for (int i = 0; i < 4; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx >= 0 && nx < n && ny >= 0 && ny < n && s[nx][ny] == 'X' && !was[nx][ny]) {
            dfs(nx, ny, cur, s);           
        }
    }
}

void solve() {
    cin >> n;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) was[i][j] = 0;
    vs s(n);
    for (int i = 0; i < n; i++) cin >> s[i];
    vector<vector<ii>> comps;
    int cntx = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) {
            if (s[i][j] == 'X') cntx++;
            if (!was[i][j] && s[i][j] == 'X') {
                comps.pb({});
                dfs(i, j, comps.back(), s);
            }
        }
    m = sz(comps);
    int cnt = 0;
    for (int z = 0; z < m; z++) {
        //for (auto x : comps[z]) cout << "(" << x.fi << ", " << x.se << ") ";
        //cout << endl; 
        int ans = inf;
        ii pos = {0, 0};
        for (int i = 0; i < 3; i++) {
            int cur = 0;
            for (auto x : comps[z])
                if ((x.fi + x.se) % 3 == i) cur++;
            if (ans > cur) {
                pos = {0, i};
                ans = cur;
            }
        }
        for (int i = 0; i < 3; i++) {
            int cur = 0;
            for (auto x : comps[z])
                if (((x.fi - x.se) % 3 + 3) % 3  == i) cur++;
            if (ans > cur) {
                pos = {1, i};
                ans = cur;
            }
        }

        if (pos.fi == 0) {
            for (auto x : comps[z])
                if ((x.fi + x.se) % 3 == pos.se) s[x.fi][x.se] = 'O';
        } else {
            for (auto x : comps[z])
                if (((x.fi - x.se) % 3 + 3) % 3  == pos.se) s[x.fi][x.se] = 'O';
        }
        cnt += ans;
    }
    assert(cnt <= cntx / 3);
    for (int i = 0; i < n; i++) cout << s[i] << endl;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int t; cin >> t;
    while (t--) solve();    
}
