//#pragma GCC optimize(2)


#include<bits/stdc++.h>
using namespace std;
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
//#include<ext/pb_ds/hash_policy.hpp>
//#include<ext/pb_ds/trie_policy.hpp>
//#include<ext/pb_ds/priority_queue.hpp>
//#include<ext/rope>
//using namespace __gnu_cxx;
//using namespace __gnu_pbds;
//void err(istream_iterator<string> it){cerr<<endl;}
//template<typename T, typename... Args>void err(istream_iterator<string> it, T a, Args... args){cerr << *it << " = " << a << " , ";err(++it, args...);}
//#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define mem(a,b) memset((a),b,sizeof((a)))
#define fpre(x) cout<<fixed<<setprecision(x)
#define clr(v) (v).clear()
#define pii pair<int,int>
#define pdd pair<double,double>
#define pli pair<ll,int>
#define pll pair<ll,ll>
#define mp make_pair
#define eb emplace_back
#define pb emplace_back
#define ll long long
#define ld long double
#define ull unsigned long long
#define uint unsigned int
#define ushort unsigned short
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define lowbit(i) (i&(-i))
#define lson (rt<<1)
#define rson lson|1
#define fi first
#define se second
const ld eps=1e-10;
const ld pi=acos(-1);
inline int dcmp(ld x)
{
    if(x<-eps)
        return -1;
    if(x>eps)
        return 1;
    return 0;
}

//-----------------------------------------------head

int ca[3],cb[3];
char s[305][305];
int main()
{
    int _;
    for(cin>>_;_;--_)
    {
        int n;
        cin>>n;
        mem(ca,0);
        mem(cb,0);
        int tot=n*n;
        for(int i=1;i<=n;++i)
            for(int j=1;j<=n;++j)
            {
                cin>>s[i][j];
                if(s[i][j]=='X')
                    ++ca[(i+j)%3];
                else if(s[i][j]=='O')
                    ++cb[(i+j)%3];
                else --tot;
            }
        tot/=3;
        int col;
        char u,v;
        if(ca[0]+cb[1]+cb[2]<=tot)
            col=0,u='X',v='O';
        else if(cb[0]+ca[1]+cb[2]<=tot)
            col=1,u='X',v='O';
        else if(cb[0]+cb[1]+ca[2]<=tot)
            col=2,u='X',v='O';
        else if(cb[0]+ca[1]+ca[2]<=tot)
            col=0,u='O',v='X';
        else if(ca[0]+cb[1]+ca[2]<=tot)
            col=1,u='O',v='X';
        else if(ca[0]+ca[1]+cb[2]<=tot)
            col=2,u='O',v='X';
        else assert(1/0);
        for(int i=1;i<=n;++i)
            for(int j=1;j<=n;++j)
            {
                if(s[i][j]=='.')
                    cout<<'.';
                else
                {
                    int c=(i+j)%3;
                    if(c==col)
                        cout<<v;
                    else cout<<u;
                }
                if(j==n)
                    cout<<endl;
            }

    }
}
