#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

#define int long long
#define ceildiv(a, b) ((a) / (b) + ((a) % (b) ? 1 : 0))
#define mod(n, M) ((n % M) + M) % M

int n;
string m[310];
int visited[310][310];

char get(int x, int y) {
    if (x < 0 || y < 0 || x >= n || y >= n)
        return '.';
    return m[x][y];
}

void paint(int x, int y, int color) {
    if (visited[x][y])
        return;
    visited[x][y] = color;
    for (int i = -1; i <= 1; i++) {
        for (int j = -1; j <= 1; j++) {
            if (get(x, y) == get(x + i, y + j)) {
                paint(x + i, y + j, color);
            }
        }
    }
}

vector<pair<int, int>> conn(int x, int y, int color) {
    paint(x, y, color);
    vector<pair<int, int>> ret;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (visited[i][j] == color)
                ret.push_back({i, j});
        }
    }
    return ret;
}

int countCell(int x, int y) {
    int ret = 0;
    for (int i = x - 2; i <= x; i++) {
        if (get(i, y) == get(i + 1, y) && get(i, y) == get(i + 2, y) &&
            get(i, y) != '.') {
            ret++;
        }
    }
    for (int i = y - 2; i <= y; i++) {
        if (get(x, i) == get(x, i + 1) && get(x, i) == get(x, i + 2) &&
            get(x, i) != '.') {
            ret++;
        }
    }
    return ret;
}

void solve() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> m[i];
        for (int j = 0; j < n; j++)
            visited[i][j] = 0;
    }

    vector<vector<pair<int, int>>> connectives;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (visited[i][j])
                continue;
            if (get(i, j) == '.')
                continue;
            connectives.push_back(conn(i, j, connectives.size() + 1));
        }
    }

    for (int i = 0; i < connectives.size(); i++) {
        int bestk = 0;
        int best = -1;
        for (int k = 0; k < 3; k++) {
            int ans = 0;
            for (int j = 0; j < connectives[i].size(); j++) {
                auto node = connectives[i][j];
                if ((node.first * n + (n % 3 == 0 ? node.first : 0) +
                     node.second) %
                        3 ==
                    k)
                    ans++;
            }
            if (best == -1 || best > ans) {
                best = ans;
                bestk = k;
            }
        }

        for (int j = 0; j < connectives[i].size(); j++) {
            auto node = connectives[i][j];
            if ((node.first * n + (n % 3 == 0 ? node.first : 0) + node.second) %
                    3 ==
                bestk)
                m[node.first][node.second] = 'O';
        }
    }

    for (int i = 0; i < n; i++)
        cout << m[i] << endl;
}

signed main() {
    int t;
    cin >> t;
    while (t--)
        solve();
}