#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;

struct node
{
    int key;
    node *parent,*left,*right;
    node():key(-1),parent(NULL),left(NULL),right(NULL) {}
};
node *root,*nil;

node* newnode()
{
    return new node();
}

void insert(int num)
{
    node* x=root;
    node* y=nil;
    node* z=newnode();
    z->key=num;
    while(x!=nil)
    {
        y=x;
        if(num>=x->key)
            x=x->right;
        else
            x=x->left;
    }
    if(y==nil) root=z;
    else
    {
        z->parent=y;
        if(num<y->key)
            y->left=z;
        else
            y->right=z;
    }
}

node* find(node* u,int num)
{
    while(u!=nil&&num!=u->key)
    {
        if(num< u->key) u=u->left;
        else u=u->right;
    }
    return u;
}

node* minele(node* u)
{
    while(u->left!=nil)
        u=u->left;
    return u;
}

node* nextpos(node* u)
{
//	node* y=nil;
//	if(u->right!=nil) y=minele(u->right);
//	else 														//?¬¬????????\???????????????????????°?????¶???????°±???u??????????????????
//	{
//		y=u->parent;
//		while(y!=nil&&y->right==u)
//		{
//			u=y;
//			y=y->parent;
//		}
//	}
//	return y;
    return minele(u->right);
}

void Delete(node* u)
{
    node* y;
    node* x;
    if(u->left==nil||u->right==nil) y=u;
    else y=nextpos(u);
    if(y->left!=nil) x=y->left;
    else x=y->right;

    if(x!=nil) x->parent=y->parent;

    if(y->parent==nil) root=x;
    else
    {
        if(y==y->parent->left) y->parent->left=x;
        else y->parent->right=x;
    }
    if(y!=u)
        u->key=y->key;
    delete y;
}

void inorder(node* u)
{
    if(u->left!=nil) inorder(u->left);
    printf(" %d",u->key);
    if(u->right!=nil) inorder(u->right);
    return;
}

void preorder(node* u)
{
    printf(" %d",u->key);
    if(u->left!=nil) preorder(u->left);
    if(u->right!=nil) preorder(u->right);
    return;
}

int main()
{
    scanf("%d",&n);
    for(int i=1; i<=n; ++i)
    {
        char s[11];
        scanf("%s",s);
        if(s[0]=='i')
        {
            int x;
            scanf("%d",&x);
            insert(x);
        }
        else if(s[0]=='f')
        {
            int x;
            scanf("%d",&x);
            node* u=(find(root,x));
            if(u!=nil)
                printf("yes\n");
            else
                printf("no\n");
        }
        else if(s[0]=='d')
        {
            int x;
            scanf("%d",&x);
            Delete(find(root,x));
        }
        else if(s[0]=='p')
        {
            inorder(root);
            printf("\n");
            preorder(root);
            printf("\n");
        }
    }
    return 0;
}
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define repr(i,n) for(ll i=(n-1);i>=0;i--)
#define all(x) x.begin(),x.end()
#define br cout << "\n";
using namespace std;
const long long INF = 1e10;
const long long MOD = 1e9+7;
using Graph = vector<vector<ll>>;
using pll = pair<ll, ll>;
template<class T> inline bool chmin(T &a, T b) { if(a > b){ a = b; return true;} return false;}
template<class T> inline bool chmax(T &a, T b) { if(a < b){ a = b; return true;} return false;}

// 0 false, 1 true 
// string to int : -48
// a to A : -32
// ceil(a)  1.2->2.0
// c++17	g++ -std=c++17 a.cpp

vector<ll> tate; vector<ll> yoko;

void f(vector<vector<string>> chess, ll count){
    
    if(count == 8){
        bool flag = true;
        rep(i, tate.size()){
            if(chess[tate[i]][yoko[i]] != "Q") flag = false;
        }

        if(flag){
            rep(i, 8){
                rep(j, 8){
                    cout << chess[i][j];
                }
                br;
            }
        }
    }else{
        rep(j, 8){
            //縦
            bool tate_flag = true;
            for(ll k = count-1; k >= 0; k--){
                if(chess[k][j] == "Q") tate_flag = false;
            }  

            bool naname_flag = true;
            for(ll k = count, l = j; k >= 0 && l >= 0; k--, l--){
                if(chess[k][l] == "Q") naname_flag = false;
            }
            for(ll k = count, l = j; k >= 0 && l < 8; k--, l++){
                if(chess[k][l] == "Q") naname_flag = false;
            }
            for(ll k = count, l = j; k < 8 && l >= 0; k++, l--){
                if(chess[k][l] == "Q") naname_flag = false;
            }
            for(ll k = count, l = j; k < 0 && l < 0; k++, l++){
                if(chess[k][l] == "Q") naname_flag = false;
            }

            if(tate_flag && naname_flag){
                chess[count][j] = "Q";
                f(chess, count+1);
                chess[count][j] = ".";
            }
        }
    }
    
}


int main() {
    std::cout << std::fixed << std::setprecision(15);
    ll k; cin >> k;
    
    rep(i, k){
        ll a, b;
        cin >> a >> b;
        tate.push_back(a); yoko.push_back(b);
    }

    vector<vector<string>> chess(8, vector<string>(8));
    rep(i, 8)rep(j, 8){
        chess[i][j] = ".";
    }

    rep(i, 8){
        chess[0][i] = "Q";
        f(chess, 1);
        chess[0][i] = ".";
    }

}
