#include <iostream>
using namespace std;

void SortShell(short *arr,short ns){
  int m=ns,p=0;
  while (m>0){
    m/=2;
    for (int j=0;j<ns;j++){
        if (j+m<ns){
                p=j;
                if (arr[p]>arr[p+m]){
                swap (arr[p],arr[p+m]);
                while (p-m>=0){
                      if (arr[p] <arr[p-m]){swap (arr[p],arr[p-m]);p-=m;}else break;
                  }
                }
        }else break;
    }
  }
}

int main(){

short n=0;
cin>>n;
short  *a=new short [n];
short  *b=new short [n];
short  *c=new short [n];
short *metka=new short [n];

for(int i=0;i<n;i++){
    cin>>a[i];
    c[i]=0;
}

for(int i=0;i<n;i++){
    c[a[i]-1]++;
    cin>>b[i];
}

for(int i=0;i<n;i++){
    if(c[b[i]-1]==0 && c[a[i]-1]>1){c[a[i]-1]--;a[i]=b[i];}
}
int lk=0;
for(int i=0;i<n;i++){
    if(c[a[i]-1]>1)lk++;
}
if(lk>0){
        lk=0;
    for(int i=1;i<=n;i++){
        if(c[i-1]==0){c[i-1]=i;metka[lk]=i;lk++;}
    }
lk=0;
    for(int i=0;i<n;i++){
        if(c[a[i]-1]>1){c[a[i]-1]--;a[i]=c[metka[lk]-1];lk++;}
    }
    bool f=false;
    for(int i=n-1;i>=0;i++){
        if(a[i-1]<a[i]){f=true;break;}
    }
    if (f)SortShell(a,n);
}

for(int i=0;i<n;i++){
    cout<<a[i]<<" ";
}
delete []metka;
delete []c;
delete []a;
delete []b;
return 0;
}


#include <iostream>
using namespace std;

int main(){

short n=0;
cin>>n;
short  *a=new short [n];
short  *b=new short [n];
short *c1=new short [n];

for(int i=0;i<n;i++){
    cin>>a[i];
    c1[i]=0;
}

for(int i=0;i<n;i++){
    c1[a[i]-1]++;
    cin>>b[i];
}

for(int i=0;i<n;i++){
    if(c1[b[i]-1]==0 && c1[a[i]-1]>1){a[i]=b[i];c1[a[i]-1]--;}
}

for(int i=0;i<n;i++){
    cout<<a[i]<<" ";
}

}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
using namespace std;
const int maxn=1e5+7;
int q2[2000];
int q1[2000];
map<int,int>gg;
int main(){
	//freopen("in.txt","r",stdin);
	int i,j,k,f1,f2,f3,f4,t1,t2,t3,sum1,sum2;
	int m,n;
	cin >> n;
	f1=0;f2=0;
	gg.clear();
	for(i=1;i<=n;i++){
        cin >> q1[i];
        gg[q1[i]]=1;}
    t1=0;
    for(i=1;i<=n;i++){
        cin >> q2[i];
        gg[q2[i]]=1;
        if(q1[i]!=q2[i]){
                t1++;
        if(t1==1){
            t2=i;
            f1=q1[i];f2=q2[i];
        }else{
            t3=i;
            f3=q1[i];f4=q2[i];
        }
    }
    }
    if(t1==1){
        i=0;
        while(1){
            i++;
            if(gg[i]!=1){
                q1[t2]=i;break;
            }
        }
    }else if(t1==2){
    if(q1[t2]==q2[t3])
    q1[t2]=q2[t2];
    else
    q1[t2]=q2[t3];
    }
    for(i=1;i<=n;i++)
        cout << q1[i] << " ";
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
using namespace std;
const int maxn=1e5+7;
int q2[2000];
int q1[2000];
map<int,int>gg;
int main(){
	//freopen("in.txt","r",stdin);
	int i,j,k,f1,f2,f3,f4,t1,t2,t3,sum1,sum2;
	int m,n;
	cin >> n;
	f1=0;f2=0;
	gg.clear();
	for(i=1;i<=n;i++){
        cin >> q1[i];
        }
    t1=0;
    for(i=1;i<=n;i++){
        cin >> q2[i];
        if(q1[i]!=q2[i]){
                t1++;
        if(t1==1){
            t2=i;
            f1=q1[i];f2=q2[i];
        }else{
            t3=i;
            f3=q1[i];f4=q2[i];
        }
    }else{
        gg[q2[i]]=1;
    }
    }
    if(t1==1){
        i=0;
        while(1){
            i++;
            if(gg[i]!=1){
                q1[t2]=i;break;
            }
        }
    }else{
       // cout <<f1<<"    " <<f2<< "   " <<f3 <<"    " <<f4<<endl;
        for(i=1;i<=1000;i++)
        for(j=1;j<=1000;j++){
                if(i==j||gg[i]==1||gg[j]==1)continue;
            if(i==f1&&i!=f2&&j==f4&&j!=f3){
                q1[t2]=i;q1[t3]=j;break;
            }else if(i==f2&&i!=f1&&j==f3&&j!=f4){
                q1[t2]=i;q1[t3]=j;break;
            }
        }
    }
    for(i=1;i<=n;i++)
        cout << q1[i] << " ";
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
using namespace std;
const int maxn=1e5+7;
int q2[2000];
int q1[2000];
map<int,int>gg;
int main(){
	//freopen("in.txt","r",stdin);
	int i,j,k,f1,f2,f3,f4,t1,t2,t3,sum1,sum2;
	int m,n;
	cin >> n;
	f1=0;f2=0;
	gg.clear();
	for(i=1;i<=n;i++){
        cin >> q1[i];
        gg[q1[i]]=1;}
    t1=0;
    for(i=1;i<=n;i++){
        cin >> q2[i];
        gg[q2[i]]=1;
        if(q1[i]!=q2[i]){
                t1++;
        if(t1==1){
            t2=i;
            f1=q1[i];f2=q2[i];
        }else{
            t3=i;
            f3=q1[i];f4=q2[i];
        }
    }
    }
    if(t1==1){
        i=0;
        while(1){
            i++;
            if(i!=f1&&i!=f2){
                q1[t2]=i;break;
            }
        }
    }else{
        for(i=1;i<=n;i++)
        for(j=1;j<=n;j++){
            if(i==f1&&i!=f2&&j==f4&&j!=f3){
                q1[t2]=i;q2[t3]=j;break;
            }else if(i==f2&&i!=f1&&j==f3&&j!=f4){
                q1[t2]=i;q2[t3]=j;break;
            }
        }
    }
    for(i=1;i<=n;i++)
        cout << q1[i] << " ";
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
using namespace std;
const int maxn=1e5+7;
int q2[2000];
int q1[2000];
map<int,int>gg;
int main(){
	//freopen("in.txt","r",stdin);
	int i,j,k,f1,f2,f3,f4,t1,t2,t3,sum1,sum2;
	int m,n;
	cin >> n;
	f1=0;f2=0;
	for(i=1;i<=n;i++){
        cin >> q1[i];
        gg[q1[i]]=1;}
    t1=0;
    for(i=1;i<=n;i++){
        cin >> q2[i];
        gg[q2[i]]=1;
        if(q1[i]!=q2[i]){
                t1++;
        if(t1==1){
            t2=i;
            f1=q1[i];f2=q2[i];
        }else{
            t3=i;
            f3=q1[i];f4=q2[i];
        }
    }
    }
    if(t1==1){
        i=0;
        while(1){
            i++;
            if(gg[i]!=1){
                q1[t2]=i;break;
            }
        }
    }else if(t1==2){
    //cout << t1 << "  t1 " <<endl;
    q1[t2]=q2[t3];
    }
    for(i=1;i<=n;i++)
        cout << q1[i] << " ";
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
using namespace std;
const int maxn=1e5+7;
int q2[2000];
int q1[2000];
map<int,int>gg;
int main(){
	//freopen("in.txt","r",stdin);
	int i,j,k,f1,f2,f3,f4,t1,t2,t3,sum1,sum2;
	int m,n;
	cin >> n;
	f1=0;f2=0;
	gg.clear();
	for(i=1;i<=n;i++){
        cin >> q1[i];
        gg[q1[i]]=1;}
    t1=0;
    for(i=1;i<=n;i++){
        cin >> q2[i];
        gg[q2[i]]=1;
        if(q1[i]!=q2[i]){
                t1++;
        if(t1==1){
            t2=i;
            f1=q1[i];f2=q2[i];
        }else{
            t3=i;
            f3=q1[i];f4=q2[i];
        }
    }
    }
    if(t1==1){
        i=0;
        while(1){
            i++;
            if(gg[i]!=1){
                q1[t2]=i;break;
            }
        }
    }else if(t1==2){
    q1[t2]=q2[t2];
    }
    for(i=1;i<=n;i++)
        cout << q1[i] << " ";
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
using namespace std;
const int maxn=1e5+7;
int q2[2000];
int q1[2000];
map<int,int>gg;
int main(){
	//freopen("in.txt","r",stdin);
	int i,j,k,f1,f2,f3,f4,t1,t2,t3,sum1,sum2;
	int m,n;
	cin >> n;
	f1=0;f2=0;
	gg.clear();
	for(i=1;i<=n;i++){
        cin >> q1[i];
        gg[q1[i]]=1;}
    t1=0;
    for(i=1;i<=n;i++){
        cin >> q2[i];
        gg[q2[i]]=1;
        if(q1[i]!=q2[i]){
                t1++;
        if(t1==1){
            t2=i;
            f1=q1[i];f2=q2[i];
        }else{
            t3=i;
            f3=q1[i];f4=q2[i];
        }
    }
    }
    if(t1==1){
        i=0;
        while(1){
            i++;
            if(i!=f1&&i!=f2){
                q1[t2]=i;break;
            }
        }
    }else if(t1==2){
    q1[t2]=q2[t2];
    }
    for(i=1;i<=n;i++)
        cout << q1[i] << " ";
	return 0;
}


#include<cstdio>
#include<iostream>

using namespace std;

const int maxn = 1000 + 10;

int n;
int A[maxn];
int B[maxn];
int C[maxn];

int get(int x, int y)
{
	for(int i= 1; i< n; i++)
		if(i != x && i != y) return i;
}

int main()
{
	//freopen("in.txt", "r", stdin);

	cin >> n;

	for(int i= 0; i< n; i++)
		cin >> A[i];

	int cnt = 0;
	for(int i= 0; i< n; i++)
	{
		cin >> B[i];
		if(B[i] != A[i]) cnt ++;
	}

	if(cnt == 1)
	{
		if(A[0] == B[0]) cout << A[0];
		else cout << get(A[0], B[0]);

		for(int i= 1; i< n; i++)
		{
			if(A[i] == B[i]) cout << " " << A[i];
			else cout << " " << get(A[i], B[i]);
		}

		return 0;
	}

	bool flag = false;
	for(int i= 0; i< n; i++)
	{
		if(flag) C[i] = A[i];
		else C[i] = B[i];

		if(A[i] != B[i]) flag = true;
	}

	cout << C[0];
	for(int i= 1; i< n; i++)
		cout << " " << C[i];

	return 0;
}


#include<cstdio>
#include<iostream>

using namespace std;

const int maxn = 1000 + 10;

int n;
int A[maxn];
int B[maxn];
int C[maxn];

int get(int x, int y)
{
	for(int i= 1; i<= n; i++)
		if(i != x && i != y) return i;
}

int main()
{
	//freopen("in.txt", "r", stdin);

	cin >> n;

	for(int i= 0; i< n; i++)
		cin >> A[i];

	int cnt = 0;
	for(int i= 0; i< n; i++)
	{
		cin >> B[i];
		if(B[i] != A[i]) cnt ++;
	}

	if(cnt == 1)
	{
		if(A[0] == B[0]) cout << A[0];
		else cout << get(A[0], B[0]);

		for(int i= 1; i< n; i++)
		{
			if(A[i] == B[i]) cout << " " << A[i];
			else cout << " " << get(A[i], B[i]);
		}

		return 0;
	}

	bool flag = false;
	for(int i= 0; i< n; i++)
	{
		if(flag) C[i] = A[i];
		else C[i] = B[i];

		if(A[i] != B[i]) flag = true;
	}

	cout << C[0];
	for(int i= 1; i< n; i++)
		cout << " " << C[i];

	return 0;
}


#include<cstdio>
#include<iostream>

using namespace std;

const int maxn = 1000 + 10;

int n;
int A[maxn];
int B[maxn];
int C[maxn];
int vis[maxn];

int get()
{
	for(int i= 1; i<= n; i++)
		if(!vis[i]) return i;
}

int main()
{
	cin >> n;

	for(int i= 0; i< n; i++)
	{
		cin >> A[i];
		vis[A[i]] ++;
	}

	int cnt = 0;
	for(int i= 0; i< n; i++)
	{
		cin >> B[i];
		if(B[i] != A[i]) cnt ++;
	}

	if(cnt == 1)
	{
		if(A[0] == B[0]) cout << A[0];
		else cout << get();

		for(int i= 1; i< n; i++)
		{
			if(A[i] == B[i]) cout << " " << A[i];
			else cout << " " << get();
		}

		return 0;
	}

	bool flag = true;
	int s = -1, t;

	for(int i= 0; i< n; i++)
	{
		if(A[i] == B[i]) C[i] = A[i];
		else
		{
			if(flag)
			{
				if(vis[B[i]] > 0)
				{
					C[i] = A[i];

					if(s < 0) s = i;
					else t = i;
				}
				else
				{
					C[i] = B[i];
					vis[B[i]] ++;
					vis[A[i]] --;
					flag = false;
				}
			}

			else C[i] = get();
		}
	}

	if(flag) swap(A[s], A[t]);

	cout << C[0];
	for(int i= 1; i< n; i++)
		cout << " " << C[i];

	return 0;
}


#include<cstdio>
#include<iostream>

using namespace std;

const int maxn = 1000 + 10;

int n;
int A[maxn];
int B[maxn];
int C[maxn];

int main()
{
	//freopen("in.txt", "r", stdin);

	cin >> n;

	for(int i= 0; i< n; i++)
		cin >> A[i];

	for(int i= 0; i< n; i++)
		cin >> B[i];

	int flag = false;
	for(int i= 0; i< n; i++)
	{
		if(flag) C[i] = A[i];
		else C[i] = B[i];

		if(A[i] != B[i]) flag = true;
	}

	cout << C[0];
	for(int i= 1; i< n; i++)
		cout << " " << C[i];

	return 0;
}


#include<cstdio>
#include<iostream>

using namespace std;

const int maxn = 1000 + 10;

int n;
int A[maxn];
int B[maxn];
int C[maxn];
int vis[maxn];

int get()
{
	for(int i= 1; i<= n; i++)
		if(!vis[i]) return i;

	return -1;
}

int main()
{
	cin >> n;

	for(int i= 0; i< n; i++)
	{
		cin >> A[i];
		vis[A[i]] ++;
	}

	int cnt = 0;
	for(int i= 0; i< n; i++)
	{
		cin >> B[i];
		if(B[i] != A[i]) cnt ++;
	}

	if(cnt == 1)
	{
		if(A[0] == B[0]) cout << A[0];
		else cout << get();

		for(int i= 1; i< n; i++)
		{
			if(A[i] == B[i]) cout << " " << A[i];
			else cout << " " << get();
		}

		return 0;
	}

	bool flag = true;
	int s = -1, t;

	for(int i= 0; i< n; i++)
	{
		if(A[i] == B[i]) C[i] = A[i];

		else if(flag)
		{

			if(vis[B[i]] > 0)
			{
				C[i] = A[i];

				if(s < 0) s = i;
				else t = i;
			}
			else
			{
				C[i] = B[i];
				vis[B[i]] ++;
				vis[A[i]] --;
				flag = false;
			}
		}

		else C[i] = get() > 0 ? get() : A[i];
	}

	if(flag) swap(A[s], A[t]);

	cout << C[0];
	for(int i= 1; i< n; i++)
		cout << " " << C[i];

	return 0;
}


#include<cstdio>
#include<iostream>

using namespace std;

const int maxn = 1000 + 10;

int n;
int A[maxn];
int B[maxn];
int C[maxn];
bool vis[maxn];

int get()
{
	for(int i= 1; i<= n; i++)
		if(!vis[i]) return i;

	return -1;
}

int main()
{
	//freopen("in.txt", "r", stdin);

	cin >> n;

	for(int i= 0; i< n; i++)
	{
		cin >> A[i];
		vis[A[i]] = true;
	}

	int cnt = 0;
	for(int i= 0; i< n; i++)
	{
		cin >> B[i];
		if(B[i] != A[i]) cnt ++;
	}

	if(cnt == 1)
	{
		if(A[0] == B[0]) cout << A[0];
		else cout << get();

		for(int i= 1; i< n; i++)
		{
			if(A[i] == B[i]) cout << " " << A[i];
			else cout << " " << get() > 0 ? get() : A[i];
		}

		return 0;
	}

	int * a = A, *b = B;
	bool flag = false;

	for(int i= 0; i< n; i++)
	{
		if(flag) C[i] = a[i];
		else
		{
			if(a[i] != b[i])
			{
				if(!vis[b[i]]) C[i] = b[i];
				else C[i] = a[i];
				flag = true;
			}
			else C[i] = a[i];
		}
	}

	cout << C[0];
	for(int i= 1; i< n; i++)
		cout << " " << C[i];

	return 0;
}


#include<cstdio>
#include<iostream>

using namespace std;

const int maxn = 1000 + 10;

int n;
int A[maxn];
int B[maxn];
int C[maxn];
bool vis[maxn];

int get()
{
	for(int i= 1; i<= n; i++)
		if(!vis[i]) return i;

	return -1;
}

int main()
{
	//freopen("in.txt", "r", stdin);

	cin >> n;

	for(int i= 0; i< n; i++)
	{
		cin >> A[i];
		vis[A[i]] = true;
	}

	int cnt = 0;
	for(int i= 0; i< n; i++)
	{
		cin >> B[i];
		if(B[i] != A[i]) cnt ++;
	}

	if(cnt == 1)
	{
		if(A[0] == B[0]) cout << A[0];
		else cout << get();

		for(int i= 1; i< n; i++)
		{
			if(A[i] == B[i]) cout << " " << A[i];
			else cout << " " << get();
		}

		return 0;
	}

	bool flag = false;
	int s = -1, t;

	for(int i= 0; i< n; i++)
	{
		if(flag) C[i] = A[i];
		else
		{
			if(A[i] == B[i]) C[i] = A[i];
			else
			{
				if(vis[B[i]])
				{
					C[i] = A[i];

					if(s < 0) s = i;
					else t = i;
				}
				else
				{
					C[i] = B[i];
					flag = true;
				}
			}
		}
	}

	if(!flag) swap(A[s], A[t]);

	cout << C[0];
	for(int i= 1; i< n; i++)
		cout << " " << C[i];

	return 0;
}


#include<cstdio>
#include<iostream>

using namespace std;

const int maxn = 1000 + 10;

int n;
int A[maxn];
int B[maxn];
int C[maxn];
bool vis[maxn];

int get()
{
	for(int i= 1; i<= n; i++)
		if(!vis[i]) return i;

	return -1;
}

int main()
{
	//freopen("in.txt", "r", stdin);

	cin >> n;

	for(int i= 0; i< n; i++)
	{
		cin >> A[i];
		vis[A[i]] = true;
	}

	int cnt = 0;
	for(int i= 0; i< n; i++)
	{
		cin >> B[i];
		if(B[i] != A[i]) cnt ++;
	}

	if(cnt == 1)
	{
		if(A[0] == B[0]) cout << A[0];
		else cout << get();

		for(int i= 1; i< n; i++)
		{
			if(A[i] == B[i]) cout << " " << A[i];
			else cout << " " << get() > 0 ? get() : A[i];
		}

		return 0;
	}

	bool flag = false;

	for(int i= 0; i< n; i++)
	{
		if(flag) C[i] = A[i];
		else
		{
			if(A[i] == B[i]) C[i] = A[i];
			else
			{
				if(get() > 0) C[i] = get();
				else C[i] = A[i];
				flag = true;
			}
		}
	}

	cout << C[0];
	for(int i= 1; i< n; i++)
		cout << " " << C[i];

	return 0;
}


#include<cstdio>
#include<iostream>

using namespace std;

const int maxn = 1000 + 10;

int n;
int A[maxn];
int B[maxn];
int C[maxn];
int vis[maxn];

int get()
{
	for(int i= 1; i<= n; i++)
		if(!vis[i]) return i;

	return -1;
}

int main()
{
	cin >> n;

	for(int i= 0; i< n; i++)
	{
		cin >> A[i];
		vis[A[i]] ++;
	}

	int cnt = 0;
	for(int i= 0; i< n; i++)
	{
		cin >> B[i];
		if(B[i] != A[i]) cnt ++;
	}

	if(cnt == 1)
	{
		if(A[0] == B[0]) cout << A[0];
		else cout << get();

		for(int i= 1; i< n; i++)
		{
			if(A[i] == B[i]) cout << " " << A[i];
			else cout << " " << get();
		}

		return 0;
	}

	bool flag = false;
	int s = -1, t;

	for(int i= 0; i< n; i++)
	{
		if(A[i] == B[i]) C[i] = A[i];
		else if(!flag)
		{
			if(vis[B[i]] <= 0 && vis[A[i]] > 1)
			{
				C[i] = B[i];
				flag = true;
			}
			else
			{
				C[i] = A[i];

				if(s < 0) s = i;
				else t = i;
			}
		}
		else C[i] = A[i];
	}

	if(!flag) swap(A[s], A[t]);

	cout << C[0];
	for(int i= 1; i< n; i++)
		cout << " " << C[i];

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	int n;
	cin>>n;
	vector<int> a(n),b(n);
	for (int i=0; i<n; i++)
		cin>>a[i];
	for (int i=0; i<n; i++)
		cin>>b[i];
	vector<int> f;

	f.assign(n+1,0);
	int anum,ai1,ai2;
	for (int i=0; i<n; i++)
	{
		if (f[a[i]]==1)
		{
			anum = a[i];
			ai2 = i;
			break;
		}
		f[a[i]]++;
	}
	for (int i=0; i<n; i++)
	{
		if (a[i]==anum)
		{
			ai1 = i;
			break;
		}
	}

	f.clear();
	f.assign(n+1,0);
	int bnum,bi1,bi2;
	for (int i=0; i<n; i++)
	{
		if (f[b[i]]==1)
		{
			bnum = b[i];
			bi2 = i;
			break;
		}
		f[b[i]]++;
	}
	for (int i=0; i<n; i++)
	{
		if (b[i]==bnum)
		{
			bi1 = i;
			break;
		}
	}

	vector<int> c;
	int cnt=0;
	for (int i=0; i<n; i++)
	{
		if (a[i]==b[i])
			c.push_back(a[i]);
		else
		{
			c.push_back(0);
			cnt++;
		}
	}

	if (ai1 == bi1 && ai2 ==  bi2)
	{
		c[ai1] = anum;
		c[bi2] = bnum;
	}
	else
	{
		if (cnt==1)
		{
			sort(a.begin(),a.end());
			int i;
			for (i=1; i<=n; i++)
			{
				if (a[i-1]!=i)
					break;
			}
			int numm = i;
			for (i=0; i<n; i++)
				if (c[i] == 0)
					c[i] = numm;
		}
		else
		{
			if (b[ai1]==anum)
			{
				c[ai2] = bnum;
				c[bi1] = a[bi1];
 			}
 			else
 			{
 				c[ai1] = bnum;
 				c[bi2] = a[bi2];

 			}
		}
	}
	for (int i=0; i<n; i++)
		cout<<c[i]<<" ";
	cout<<"\n";
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	int n;
	cin>>n;
	vector<int> a(n),b(n);
	for (int i=0; i<n; i++)
		cin>>a[i];
	for (int i=0; i<n; i++)
		cin>>b[i];
	vector<int> f;

	f.assign(n+1,0);
	int anum,ai1,ai2;
	for (int i=0; i<n; i++)
	{
		if (f[a[i]]==1)
		{
			anum = a[i];
			ai2 = i;
			break;
		}
		f[a[i]]++;
	}
	for (int i=0; i<n; i++)
	{
		if (a[i]==anum)
		{
			ai1 = i;
			break;
		}
	}

	f.clear();
	f.assign(n+1,0);
	int bnum,bi1,bi2;
	for (int i=0; i<n; i++)
	{
		if (f[b[i]]==1)
		{
			bnum = b[i];
			bi2 = i;
			break;
		}
		f[b[i]]++;
	}
	for (int i=0; i<n; i++)
	{
		if (b[i]==bnum)
		{
			bi1 = i;
			break;
		}
	}

	vector<int> c;
	int cnt=0;
	for (int i=0; i<n; i++)
	{
		if (a[i]==b[i])
			c.push_back(a[i]);
		else
		{
			c.push_back(0);
			cnt++;
		}
	}

	if (ai1 == bi1 && ai2 ==  bi2)
	{
		c[ai1] = anum;
		c[bi2] = bnum;
	}
	else
	{
		if (cnt==1)
		{
			sort(a.begin(),a.end());
			int i;
			for (i=1; i<=n; i++)
			{
				if (a[i-1]!=i)
					break;
			}
			int numm = i;
			for (i=0; i<n; i++)
				if (c[i] == 0)
					c[i] = numm;
		}
		else
		{
			if (b[ai1]==anum)
			{
				c[ai2] = bnum;
				c[bi2] = a[bi2];
 			}
 			else
 			{
 				c[ai1] = bnum;
 				c[bi1] = a[bi1];

 			}
		}
	}
	for (int i=0; i<n; i++)
		cout<<c[i]<<" ";
	cout<<"\n";
	cout<<ai1<<" "<<ai2<<" "<<bi1<<" "<<bi2;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	int n;
	cin>>n;
	vector<int> a(n),b(n);
	for (int i=0; i<n; i++)
		cin>>a[i];
	for (int i=0; i<n; i++)
		cin>>b[i];
	vector<int> f;

	f.assign(n+1,0);
	int anum,ai1,ai2;
	for (int i=0; i<n; i++)
	{
		if (f[a[i]]==1)
		{
			anum = a[i];
			ai2 = i;
			break;
		}
		f[a[i]]++;
	}
	for (int i=0; i<n; i++)
	{
		if (a[i]==anum)
		{
			ai1 = i;
			break;
		}
	}

	f.clear();
	f.assign(n+1,0);
	int bnum,bi1,bi2;
	for (int i=0; i<n; i++)
	{
		if (f[b[i]]==1)
		{
			bnum = b[i];
			bi2 = i;
			break;
		}
		f[b[i]]++;
	}
	for (int i=0; i<n; i++)
	{
		if (b[i]==bnum)
		{
			bi1 = i;
			break;
		}
	}

	vector<int> c;
	int cnt=0;
	for (int i=0; i<n; i++)
	{
		if (a[i]==b[i])
			c.push_back(a[i]);
		else
		{
			c.push_back(0);
			cnt++;
		}
	}

	if (ai1 == bi1 && ai2 ==  bi2)
	{
		c[ai1] = anum;
		c[bi2] = bnum;
	}
	else
	{
		if (cnt==1)
		{
			sort(a.begin(),a.end());
			int i;
			for (i=1; i<=n; i++)
			{
				if (a[i-1]!=i)
					break;
			}
			int numm = i;
			for (i=0; i<n; i++)
				if (c[i] == 0)
					c[i] = numm;
		}
		else
		{
			if (b[ai1]==anum)
			{
				c[ai2] = bnum;
				c[bi2] = a[bi2];
 			}
 			else
 			{
 				c[ai1] = bnum;
 				c[bi1] = a[bi1];

 			}
		}
	}
	for (int i=0; i<n; i++)
		cout<<c[i]<<" ";
	cout<<"\n";
	//cout<<ai1<<" "<<ai2<<" "<<bi1<<" "<<bi2;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	int n;
	cin>>n;
	vector<int> a(n),b(n);
	for (int i=0; i<n; i++)
		cin>>a[i];
	for (int i=0; i<n; i++)
		cin>>b[i];
	vector<int> f;

	f.assign(n+1,0);
	int anum,ai1,ai2;
	for (int i=0; i<n; i++)
	{
		if (f[a[i]]==1)
		{
			anum = a[i];
			ai2 = i;
			break;
		}
		f[a[i]]++;
	}
	for (int i=0; i<n; i++)
	{
		if (a[i]==anum)
		{
			ai1 = i;
			break;
		}
	}

	f.clear();
	f.assign(n+1,0);
	int bnum,bi1,bi2;
	for (int i=0; i<n; i++)
	{
		if (f[b[i]]==1)
		{
			bnum = b[i];
			bi2 = i;
			break;
		}
		f[b[i]]++;
	}
	for (int i=0; i<n; i++)
	{
		if (b[i]==bnum)
		{
			bi1 = i;
			break;
		}
	}

	vector<int> c;
	int cnt=0;
	for (int i=0; i<n; i++)
	{
		if (a[i]==b[i])
			c.push_back(a[i]);
		else
		{
			c.push_back(0);
			cnt++;
		}
	}

	if (ai1 == bi1 && ai2 ==  bi2)
	{
		c[ai1] = anum;
		c[bi2] = bnum;
	}
	else
	{
		if (cnt==1)
		{
			sort(a.begin(),a.end());
			int i;
			for (i=1; i<=n; i++)
			{
				if (a[i-1]!=i)
					break;
			}
			int numm = i;
			for (i=0; i<n; i++)
				if (c[i] == 0)
					c[i] = numm;
		}
		else
		{
			if (a[ai1]==b[ai1])
				a[ai2] = b[ai2];
			else
			{
				if (a[ai2] == b[ai2])
					a[ai1] = b[ai1];
				else
				{
					if (b[ai1]==bnum)
						a[ai2] = b[ai2];
					else
						a[ai1] = b[ai1];
				}
			}
			for (int i=0; i<n; i++)
				cout<<a[i]<<" ";
			cout<<"\n";
			return 0;
		}
	}
	for (int i=0; i<n; i++)
		cout<<c[i]<<" ";
	cout<<"\n";
	//cout<<ai1<<" "<<ai2<<" "<<bi1<<" "<<bi2;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	int n;
	cin>>n;
	vector<int> a(n),b(n);
	for (int i=0; i<n; i++)
		cin>>a[i];
	for (int i=0; i<n; i++)
		cin>>b[i];
	vector<int> f;

	f.assign(n+1,0);
	int anum,ai1,ai2;
	for (int i=0; i<n; i++)
	{
		if (f[a[i]]==1)
		{
			anum = a[i];
			ai2 = i;
			break;
		}
		f[a[i]]++;
	}
	for (int i=0; i<n; i++)
	{
		if (a[i]==anum)
		{
			ai1 = i;
			break;
		}
	}

	f.clear();
	f.assign(n+1,0);
	int bnum,bi1,bi2;
	for (int i=0; i<n; i++)
	{
		if (f[b[i]]==1)
		{
			bnum = b[i];
			bi2 = i;
			break;
		}
		f[b[i]]++;
	}
	for (int i=0; i<n; i++)
	{
		if (b[i]==bnum)
		{
			bi1 = i;
			break;
		}
	}

	vector<int> c;
	int cnt=0;
	for (int i=0; i<n; i++)
	{
		if (a[i]==b[i])
			c.push_back(a[i]);
		else
		{
			c.push_back(0);
			cnt++;
		}
	}

	if (ai1 == bi1 && ai2 ==  bi2)
	{
		c[ai1] = anum;
		c[bi2] = bnum;
	}
	else
	{
		if (cnt==1)
		{
			sort(a.begin(),a.end());
			int i;
			for (i=1; i<=n; i++)
			{
				if (a[i-1]!=i)
					break;
			}
			int numm = i;
			for (i=0; i<n; i++)
				if (c[i] == 0)
					c[i] = numm;
		}
		else
		{
			if (a[ai1]==b[ai1])
				a[ai2] = b[ai2];
			else
				a[ai1] = b[ai1];
			for (int i=0; i<n; i++)
				cout<<a[i]<<" ";
			cout<<"\n";
			return 0;
		}
	}
	for (int i=0; i<n; i++)
		cout<<c[i]<<" ";
	cout<<"\n";
	//cout<<ai1<<" "<<ai2<<" "<<bi1<<" "<<bi2;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	int n;
	cin>>n;
	vector<int> a(n),b(n);
	for (int i=0; i<n; i++)
		cin>>a[i];
	for (int i=0; i<n; i++)
		cin>>b[i];
	vector<int> f;

	f.assign(n+1,0);
	int anum,ai1,ai2;
	for (int i=0; i<n; i++)
	{
		if (f[a[i]]==1)
		{
			anum = a[i];
			ai2 = i;
			break;
		}
		f[a[i]]++;
	}
	for (int i=0; i<n; i++)
	{
		if (a[i]==anum)
		{
			ai1 = i;
			break;
		}
	}

	f.clear();
	f.assign(n+1,0);
	int bnum,bi1,bi2;
	for (int i=0; i<n; i++)
	{
		if (f[b[i]]==1)
		{
			bnum = b[i];
			bi2 = i;
			break;
		}
		f[b[i]]++;
	}
	for (int i=0; i<n; i++)
	{
		if (b[i]==bnum)
		{
			bi1 = i;
			break;
		}
	}

	vector<int> c;
	int cnt=0;
	for (int i=0; i<n; i++)
	{
		if (a[i]==b[i])
			c.push_back(a[i]);
		else
		{
			c.push_back(0);
			cnt++;
		}
	}

	if (ai1 == bi1 && ai2 ==  bi2)
	{
		c[ai1] = anum;
		c[bi2] = bnum;
	}
	else
	{
		if (cnt==1)
		{
			sort(a.begin(),a.end());
			int i;
			for (i=1; i<=n; i++)
			{
				if (a[i-1]!=i)
					break;
			}
			int numm = i;
			for (i=0; i<n; i++)
				if (c[i] == 0)
					c[i] = numm;
		}
		else
		{
			if (b[ai1]==anum)
			{
				c[ai2] = bnum;
				if (c[bi1] == bnum)
					c[bi2] = a[bi2];
				else
					c[bi1] = a[bi1];
 			}
 			else
 			{
 				c[ai1] = bnum;
 				if (c[bi1] == bnum)
 					c[bi2] = a[bi2];
 				else
 					c[bi1] = a[bi1];

 			}
		}
	}
	for (int i=0; i<n; i++)
		cout<<c[i]<<" ";
	cout<<"\n";
	//cout<<ai1<<" "<<ai2<<" "<<bi1<<" "<<bi2;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	int n;
	cin>>n;
	vector<int> a(n),b(n);
	for (int i=0; i<n; i++)
		cin>>a[i];
	for (int i=0; i<n; i++)
		cin>>b[i];
	vector<int> f;

	f.assign(n+1,0);
	int anum,ai1,ai2;
	for (int i=0; i<n; i++)
	{
		if (f[a[i]]==1)
		{
			anum = a[i];
			ai2 = i;
			break;
		}
		f[a[i]]++;
	}
	for (int i=0; i<n; i++)
	{
		if (a[i]==anum)
		{
			ai1 = i;
			break;
		}
	}

	f.clear();
	f.assign(n+1,0);
	int bnum,bi1,bi2;
	for (int i=0; i<n; i++)
	{
		if (f[b[i]]==1)
		{
			bnum = b[i];
			bi2 = i;
			break;
		}
		f[b[i]]++;
	}
	for (int i=0; i<n; i++)
	{
		if (b[i]==bnum)
		{
			bi1 = i;
			break;
		}
	}

	vector<int> c;
	int cnt=0;
	for (int i=0; i<n; i++)
	{
		if (a[i]==b[i])
			c.push_back(a[i]);
		else
		{
			c.push_back(0);
			cnt++;
		}
	}

	if (ai1 == bi1 && ai2 ==  bi2)
	{
		c[ai1] = anum;
		c[bi2] = bnum;
	}
	else
	{
		if (cnt==1)
		{
			sort(a.begin(),a.end());
			int i;
			for (i=1; i<=n; i++)
			{
				if (a[i-1]!=i)
					break;
			}
			int numm = i;
			for (i=0; i<n; i++)
				if (c[i] == 0)
					c[i] = numm;
		}
		else
		{
			if (a[ai1]==b[ai1])
				a[ai2] = b[ai2];
			else
			{
				if (a[ai2] == b[ai2])
					a[ai1] = b[ai1];
				else
				{
					if (b[ai1]==anum)
						a[ai2] = b[ai2];
					else
						a[ai1] = b[ai1];
				}
			}
			for (int i=0; i<n; i++)
				cout<<a[i]<<" ";
			cout<<"\n";
			return 0;
		}
	}
	for (int i=0; i<n; i++)
		cout<<c[i]<<" ";
	cout<<"\n";
	//cout<<ai1<<" "<<ai2<<" "<<bi1<<" "<<bi2;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

void display(vector<int> &a)
{
	for(int i=0; i<a.size(); i++)
		cout<<a[i]<<" ";
	cout<<"\n";
	return;
}

bool check(vector<int> &a, vector<int> &b)
{
	int cnt=0;
	for (int i=0; i<a.size(); i++)
	{
		if (a[i]!=b[i])
			cnt++;
	}
	if (cnt<=1)
		return true;
	else
		return false;
}

int main()
{
	ios_base::sync_with_stdio(false);
	int n;
	cin>>n;
	vector<int> a(n),b(n);
	for (int i=0; i<n; i++)
		cin>>a[i];
	for (int i=0; i<n; i++)
		cin>>b[i];
	vector<int> f;

	f.assign(n+1,0);
	int anum,ai1,ai2,na;
	for (int i=0; i<n; i++)
	{
		if (f[a[i]]==1)
		{
			anum = a[i];
			ai2 = i;
		}
		f[a[i]]++;
	}
	for (int i=0; i<n; i++)
	{
		if (a[i]==anum)
		{
			ai1 = i;
			break;
		}
	}
	for (int i=1; i<=n; i++)
		if (f[i]==0)
			na=i;

	//option 1
	a[ai1] = na;
	if (check(a,b))
		display(a);
	else
	{
		a[ai1] = anum;
		a[ai2] = na;
		display(a);
	}
	//cout<<ai1<<" "<<aii2<<" "<<bi1<<" "<<bi2;
	return 0;
}


//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<vector>
#include<map>
#define N 1005
#define inf 0xfffffff
#define rep(i,n) for(i=0;i<n;i++)
#define mem(a,x) memset(a,x,sizeof(a))
const double PI=acos(-1);
const double eps=1e-9;
using namespace std;
typedef long long ll;
int ans,t,i,j,k,n,m;
int a[N],b[N],p[N],vis[N];
char s[N];
int che(int x,int y){
	for(int j=1;j<=n;j++){
		if(x!=j&&y!=j&&vis[j]==0) return j;
	}
}
int main(){
	scanf("%d",&n);
	mem(vis,0);
	rep(i,n){
		scanf("%d",&a[i]);
		vis[a[i]]=1;
	}
	int ok=0;
	ans=0;
	rep(i,n){
		scanf("%d",&b[i]);
		//vis[b[i]]=1;
		if(b[i]==a[i]){
			ans++;
		}
	}
	for(i=1;i<=n;i++) if(vis[i]==0) t=i;
	rep(i,n){
		if(ans==n-2){
			if(b[i]==a[i]){
			p[i]=a[i];
			vis[p[i]]=1;
		}
		else if(ok==0){
			if(vis[b[i]]){
				p[i]=a[i];
				vis[a[i]]=1;
			}

			else{
				p[i]=b[i];
				vis[b[i]]=1;
			}
		}

		}
		else{
			if(b[i]==a[i]){
			p[i]=a[i];
			vis[p[i]]=1;
		}
		else{
			p[i]=che(a[i],b[i]);
		}
		}
	}
	rep(i,n){
		if(i!=n-1) printf("%d ",p[i]);
	}
	printf("%d\n",p[n-1]);
	return 0;
}


//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<vector>
#include<map>
#define N 1005
#define inf 0xfffffff
#define rep(i,n) for(i=0;i<n;i++)
#define mem(a,x) memset(a,x,sizeof(a))
const double PI=acos(-1);
const double eps=1e-9;
using namespace std;
typedef long long ll;
int ans,t,i,j,k,n,m;
int a[N],b[N],p[N],vis[N];
char s[N];
int che(int x,int y){
	for(int j=1;j<=n;j++){
		if(x!=j&&y!=j) return j;
	}
}
int main(){
	scanf("%d",&n);
	mem(vis,0);
	rep(i,n){
		scanf("%d",&a[i]);
		vis[a[i]]=1;
	}
	int ok=0;
	ans=0;
	rep(i,n){
		scanf("%d",&b[i]);
		vis[b[i]]=1;
		if(b[i]==a[i]){
			ans++;
		}
	}
	for(i=1;i<=n;i++) if(vis[i]==0) t=i;
	rep(i,n){
		if(ans==n-2){
			if(b[i]==a[i]){
			p[i]=a[i];
		}
		else if(ok==0){
			p[i]=a[i];
			ok=1;
		}
		else{
			p[i]=b[i];
		}
		}
		else{
			if(b[i]==a[i]){
			p[i]=a[i];
		}

		else{
			p[i]=che(a[i],b[i]);
		}
		}
	}
	rep(i,n){
		if(i!=n-1) printf("%d ",p[i]);
	}
	printf("%d\n",p[n-1]);
	return 0;
}


//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<vector>
#include<map>
#define N 1005
#define inf 0xfffffff
#define rep(i,n) for(i=0;i<n;i++)
#define mem(a,x) memset(a,x,sizeof(a))
const double PI=acos(-1);
const double eps=1e-9;
using namespace std;
typedef long long ll;
int ans,t,i,j,k,n,m;
int a[N],b[N],p[N],vis[N],err[2];
char s[N];
int che(int x,int y){
	for(int j=1;j<=n;j++){
		if(x!=j&&y!=j&&vis[j]==0) return j;
	}
}
int main(){
	scanf("%d",&n);
	mem(vis,0);
	rep(i,n){
		scanf("%d",&a[i]);
	}
	int ok=0;
	ans=0;
	rep(i,n){
		scanf("%d",&b[i]);
		//vis[b[i]]=1;
		if(b[i]==a[i]){
			ans++;
		}
	}
	j=0;
	for(i=1;i<=n;i++) if(vis[i]==0) t=i;
	rep(i,n){
		if(ans==n-2){
			if(b[i]==a[i]){
			p[i]=a[i];
			vis[p[i]]=1;
		}
		else{
			err[j++]=i;
		}

		}
		else{
			if(b[i]==a[i]){
			p[i]=a[i];
			vis[p[i]]=1;
		}
		else{
			err[0]=i;;
		}
		}
	}
	if(ans==n-2){
		if(vis[a[err[0]]]){
			p[err[0]]=b[err[0]];
			p[err[1]]=a[err[1]];
		}
		else if(vis[a[err[1]]]){
			p[err[1]]=b[err[1]];
			p[err[0]]=a[err[0]];
		}
		else if(vis[b[err[1]]]){
			p[err[1]]=a[err[1]];
			p[err[0]]=b[err[0]];
		}
		else if(vis[b[err[0]]]){
			p[err[0]]=a[err[0]];
			p[err[1]]=b[err[1]];
		}
		else{
			p[err[0]]=a[err[0]];
			p[err[1]]=b[err[1]];
		}
	}
	else
	{
		p[err[0]]=che(a[err[0]],b[err[0]]);
	}
	rep(i,n){
		if(i!=n-1) printf("%d ",p[i]);
	}
	printf("%d\n",p[n-1]);
	return 0;
}


//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<vector>
#include<map>
#define N 1005
#define inf 0xfffffff
#define rep(i,n) for(i=0;i<n;i++)
#define mem(a,x) memset(a,x,sizeof(a))
const double PI=acos(-1);
const double eps=1e-9;
using namespace std;
typedef long long ll;
int ans,t,i,j,k,n,m;
int a[N],b[N],p[N],vis[N],err[2];
char s[N];
int che(int x,int y){
	for(int j=1;j<=n;j++){
		if(x!=j&&y!=j&&vis[j]==0) return j;
	}
}
int main(){
	scanf("%d",&n);
	mem(vis,0);
	rep(i,n){
		scanf("%d",&a[i]);
	}
	int ok=0;
	ans=0;
	rep(i,n){
		scanf("%d",&b[i]);
		//vis[b[i]]=1;
		if(b[i]==a[i]){
			ans++;
		}
	}
	j=0;
	for(i=1;i<=n;i++) if(vis[i]==0) t=i;
	rep(i,n){
		if(ans==n-2){
			if(b[i]==a[i]){
			p[i]=a[i];
			vis[p[i]]=1;
		}
		else{
			err[j++]=i;
		}

		}
		else{
			if(b[i]==a[i]){
			p[i]=a[i];
			vis[p[i]]=1;
		}
		else{
			p[i]=che(a[i],b[i]);
		}
		}
	}
	if(ans==n-2){
		if(vis[a[err[0]]]){
			p[err[0]]=b[err[0]];
			p[err[1]]=a[err[1]];
		}
		else if(vis[a[err[1]]]){
			p[err[1]]=b[err[1]];
			p[err[0]]=a[err[0]];
		}
		else if(vis[b[err[1]]]){
			p[err[1]]=a[err[1]];
			p[err[0]]=b[err[0]];
		}
		else if(vis[b[err[0]]]){
			p[err[0]]=a[err[0]];
			p[err[1]]=b[err[1]];
		}
		else{
			p[err[0]]=a[err[0]];
			p[err[1]]=b[err[1]];
		}
	}
	rep(i,n){
		if(i!=n-1) printf("%d ",p[i]);
	}
	printf("%d\n",p[n-1]);
	return 0;
}


//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<vector>
#include<map>
#define N 1005
#define inf 0xfffffff
#define rep(i,n) for(i=0;i<n;i++)
#define mem(a,x) memset(a,x,sizeof(a))
const double PI=acos(-1);
const double eps=1e-9;
using namespace std;
typedef long long ll;
int ans,t,i,j,k,n,m;
int a[N],b[N],p[N],vis[N];
char s[N];
int che(int x,int y){
	for(int j=1;j<=n;j++){
		if(x!=j&&y!=j) return j;
	}
}
int main(){
	scanf("%d",&n);
	mem(vis,0);
	rep(i,n){
		scanf("%d",&a[i]);
		vis[a[i]]=1;
	}
	int ok=0;
	ans=0;
	rep(i,n){
		scanf("%d",&b[i]);
		vis[b[i]]=1;
		if(b[i]==a[i]){
			ans++;
		}
		else if(ok==0){
			p[i]=a[i];
			ok=1;
		}
		else{
			p[i]=b[i];
		}
	}
	for(i=1;i<=n;i++) if(vis[i]==0) t=i;
	rep(i,n){
		if(ans==n-2){
			if(b[i]==a[i]){
			p[i]=a[i];
		}
		else if(ok==0){
			p[i]=a[i];
			ok=1;
		}
		else{
			p[i]=b[i];
		}
		}
		else{
			if(b[i]==a[i]){
			p[i]=a[i];
		}

		else{
			p[i]=che(a[i],b[i]);
		}
		}
	}
	rep(i,n){
		if(i!=n-1) printf("%d ",p[i]);
	}
	printf("%d\n",p[n-1]);
	return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int a[1001], b[1001], ca[1001];
vector <int> v;

int main()
{
    int n, t = 0, k, c = 0;
    cin >> n;
    for (int i = 0; i < n; i ++)
    {
         cin >> a[i];
         if (a[i] >= t) t = a[i];
         ca[a[i]] += 1;
    }
    for (int i = 0; i < n; i ++) cin >> b[i];
    for (int i = 1; i <= n; i ++)
    {
        if (ca[i] == 0)
        {
            k = i;
            break;
        }
    }
    for (int i = 0; i < n; i ++) if (a[i] != b[i]) v.push_back(i);
    for (int i = 0; i < v.size(); i ++)
        for (int j = 0; j < n; j ++)
        {
            if (c == 0)
            {
                if (a[j] == a[v[i]])
                {
                    a[v[i]] = k;
                    c += 1;
                    break;
                }
            }
        }
    for (int i = 0; i < n; i ++) cout << a[i] << " ";
}


#include <iostream>
#include <vector>

using namespace std;

int a[1001], b[1001], ca[1001];
vector <int> v;

int main()
{
    int n, t = 0, k, c = 0;
    cin >> n;
    for (int i = 0; i < n; i ++)
    {
         cin >> a[i];
         if (a[i] >= t) t = a[i];
         ca[a[i]] += 1;
    }
    for (int i = 0; i < n; i ++) cin >> b[i];
    for (int i = 1; i <= n; i ++)
    {
        if (ca[i] == 0)
        {
            k = i;
            break;
        }
    }
    for (int i = 0; i < n; i ++) if (a[i] != b[i]) v.push_back(i);
    for (int i = 0; i < v.size(); i ++)
    {
        if (ca[a[v[i]]] == 2 and b[v[i]] = k)
        {
            a[v[i]] = k;
            break;
        }
    }
    for (int i = 0; i < n; i ++) cout << a[i] << " ";
}


#include <iostream>
#include <vector>

using namespace std;

int a[1001], b[1001], ca[1001];
vector <int> v;

int main()
{
    int n, t = 0, k, c = 0;
    cin >> n;
    for (int i = 0; i < n; i ++)
    {
         cin >> a[i];
         if (a[i] >= t) t = a[i];
         ca[a[i]] += 1;
    }
    for (int i = 0; i < n; i ++) cin >> b[i];
    for (int i = 1; i <= n; i ++)
    {
        if (ca[i] == 0)
        {
            k = i;
            break;
        }
    }
    for (int i = 0; i < n; i ++) if (a[i] != b[i]) v.push_back(i);
    for (int i = 0; i < v.size(); i ++)
    {
        if (ca[a[v[i]]] == 2 and b[v[i]] == k)
        {
            a[v[i]] = k;
            break;
        }
    }
    for (int i = 0; i < n; i ++) cout << a[i] << " ";
}


    #include <iostream>
    #include <vector>

    using namespace std;

    int a[1001], b[1001], ca[1001];
    vector <int> v;

    int main()
    {
        int n, t = 0, k, c = 0;
        cin >> n;
        for (int i = 0; i < n; i ++)
        {
             cin >> a[i];
             if (a[i] >= t) t = a[i];
             ca[a[i]] += 1;
        }
        for (int i = 0; i < n; i ++) cin >> b[i];
        for (int i = 1; i <= n; i ++)
        {
            if (ca[i] == 0)
            {
                k = i;
                break;
            }
        }
        for (int i = 0; i < n; i ++) if (a[i] != b[i]) v.push_back(i);
        for (int i = 0; i < v.size(); i ++)
        {
            if (ca[a[v[i]]] == 2 and a[v[i]] != k)
            {
                a[v[i]] = k;
                break;
            }
        }
        for (int i = 0; i < n; i ++) cout << a[i] << " ";
    }


#include <iostream>
#include <vector>

using namespace std;

int a[1001], b[1001], ca[1001];
vector <int> v;

int main()
{
    int n, t = 0, k, c = 0;
    cin >> n;
    for (int i = 0; i < n; i ++)
    {
         cin >> a[i];
         if (a[i] >= t) t = a[i];
         ca[a[i]] += 1;
    }
    for (int i = 0; i < n; i ++) cin >> b[i];
    for (int i = 1; i <= n; i ++)
    {
        if (ca[i] == 0)
        {
            k = i;
            break;
        }
    }
    for (int i = 0; i < n; i ++) if (a[i] != b[i]) v.push_back(i);
    for (int i = 0; i < v.size(); i ++)
    {
        if (ca[a[v[i]]] == 2)
        {
            a[v[i]] = k;
            break;
        }
    }
    for (int i = 0; i < n; i ++) cout << a[i] << " ";
}


    #include <iostream>
    #include <vector>

    using namespace std;

    int a[1001], b[1001], ca[1001];
    vector <int> v;

    int main()
    {
        int n, t = 0, k, c = 0;
        cin >> n;
        for (int i = 0; i < n; i ++)
        {
             cin >> a[i];
             if (a[i] >= t) t = a[i];
             ca[a[i]] += 1;
        }
        for (int i = 0; i < n; i ++) cin >> b[i];
        for (int i = 1; i <= n; i ++)
        {
            if (ca[i] == 0)
            {
                k = i;
                break;
            }
        }
        for (int i = 0; i < n; i ++) if (a[i] != b[i]) v.push_back(i);
        if (v.size() == 1) a[v[0]] = k;
        else
        {
            for (int i = 0; i < v.size(); i ++)
            {
                if (ca[a[v[i]]] == 2 and b[v[i]] == k)
                {
                    a[v[i]] = k;
                    break;
                }
            }
        }
        for (int i = 0; i < n; i ++) cout << a[i] << " ";
    }


#include<bits/stdc++.h>
using namespace std;
int aaa[300],bbb[300],ccc[300];
#define sfi ({int a;scanf("%d",&a);a;})
map<int,int>mpp;
int main()
{
    int n=sfi;
    int aaa[n+5],bbb[n+5];
    for(int i=1;i<=n;i++)aaa[i]=sfi;
    for(int i=1;i<=n;i++)bbb[i]=sfi;

    int cnt=0,pos[n+5];
    for(int i=1;i<=n;i++)
    {
        if(aaa[i]!=bbb[i])
        {
            pos[cnt++]=i;
        }
        else
        {
            mpp[aaa[i]]=1;
        }
    }

    if(cnt==1)
    {
        int p=pos[0];
        for(int j=1;j<=n;j++)
        {
            if(mpp[j]==0)
            {
                mpp[j]=1;
                aaa[p]=j;
                break;
            }
        }
    }
    else if(cnt==2)
    {

        int p1=pos[0],p2=pos[1];
        //cout<<p1<<' '<<p2<<endl;
        int x1=aaa[p1],y1=aaa[p2];
        int x2=bbb[p1],y2=bbb[p2];

        if(mpp[x1]==0 and mpp[y1]==0 and x1!=y1){
            aaa[p1]=x1,aaa[p2]=y1;
            //cout<<1<<endl;
        }
        else if(mpp[x2]==0 and mpp[y2==0] and x2!=y2){
            aaa[p1]=x2,aaa[p2]=y2;
            //cout<<2<<endl;
        }
        else if(mpp[x1]==0 and mpp[y2]==0 and x1!=y1){
            aaa[p1]=x1,aaa[p2]=y2;
            //cout<<3<<endl;
        }
        else if(mpp[x2]==0 and mpp[y1]==0 and x2!=y1){
            aaa[p1]=x2,aaa[p2]=y1;
            //cout<<4<<endl;
            //cout<<x2<<' '<<y1<<endl;
        }
    }

    for(int i=1;i<=n;i++)
    {
        cout<<aaa[i]<<' ';
    }
}


#include<bits/stdc++.h>
using namespace std;
int aaa[300],bbb[300],ccc[300];
#define sfi ({int a;scanf("%d",&a);a;})
map<int,int>mpp;
int main()
{
    int n=sfi;
    int aaa[n+5],bbb[n+5];
    for(int i=1;i<=n;i++)aaa[i]=sfi;
    for(int i=1;i<=n;i++)bbb[i]=sfi;

    int cnt=0,pos[n+5];
    for(int i=1;i<=n;i++)
    {
        if(aaa[i]!=bbb[i])
        {
            pos[cnt++]=i;
        }
        else
        {
            mpp[aaa[i]]=1;
        }
    }

    if(cnt==1)
    {
        int p=pos[0];
        for(int j=1;j<=n;j++)
        {
            if(mpp[j]==0)
            {
                mpp[j]=1;
                aaa[p]=j;
                break;
            }
        }
    }
    else if(cnt==2)
    {

        int p1=pos[0],p2=pos[1];
        //cout<<p1<<' '<<p2<<endl;
        int x1=aaa[p1],y1=aaa[p2];
        int x2=bbb[p1],y2=bbb[p2];

        if(mpp[x1]==0 and mpp[y1]==0){
            aaa[p1]=x1,aaa[p2]=y1;
            //cout<<1<<endl;
        }
        else if(mpp[x2]==0 and mpp[y2==0]){
            aaa[p1]=x2,aaa[p2]=y2;
            //cout<<2<<endl;
        }
        else if(mpp[x1]==0 and mpp[y2]==0){
            aaa[p1]=x1,aaa[p2]=y2;
            //cout<<3<<endl;
        }
        else if(mpp[x2]==0 and mpp[y1]==0){
            aaa[p1]=x2,aaa[p2]=y1;
            //cout<<4<<endl;
            //cout<<x2<<' '<<y1<<endl;
        }
    }

    for(int i=1;i<=n;i++)
    {
        cout<<aaa[i]<<' ';
    }
}


#include<bits/stdc++.h>
using namespace std;
int aaa[300],bbb[300],ccc[300];
#define sfi ({int a;scanf("%d",&a);a;})
map<int,int>mpp;
int main()
{
    int n=sfi;
    int aaa[n+5],bbb[n+5];
    for(int i=1;i<=n;i++)aaa[i]=sfi;
    for(int i=1;i<=n;i++)bbb[i]=sfi;

    int cnt=0,pos[n+5];
    for(int i=1;i<=n;i++)
    {
        if(aaa[i]!=bbb[i])
        {
            pos[cnt++]=i;
        }
        else
        {
            mpp[aaa[i]]=1;
        }
    }

    if(cnt==1)
    {
        int p=pos[0];
        for(int j=1;j<=n;j++)
        {
            if(mpp[j]==0)
            {
                mpp[j]=1;
                aaa[p]=j;
                break;
            }
        }
    }
    else if(cnt==2)
    {
        int p1=pos[0],p2=pos[1];
        int x1=aaa[p1],y1=bbb[p1];
        int x2=aaa[p2],y2=bbb[p2];

        if(mpp[x1]==0 and mpp[y1]==0)
            aaa[p1]=x1,aaa[p2]=y1;
        else if(mpp[x2]==0 and mpp[y2==0])
            aaa[p1]=x2,aaa[p2]=y2;
        else if(mpp[x2]==0 and mpp[y1]==0)
            aaa[p1]=x2,aaa[p2]=y1;
        else if(mpp[x1]==0 and mpp[y2]==0)
            aaa[p1]=x1,aaa[p2]=y2;
    }

    for(int i=1;i<=n;i++)
    {
        cout<<aaa[i]<<' ';
    }
}


#include<bits/stdc++.h>
using namespace std;
int aaa[300],bbb[300],ccc[300];
#define sfi ({int a;scanf("%d",&a);a;})
map<int,int>mpp;
int main()
{
    int n=sfi;
    int aaa[n+5],bbb[n+5];
    for(int i=1;i<=n;i++)aaa[i]=sfi;
    for(int i=1;i<=n;i++)bbb[i]=sfi;

    int cnt=0,pos[n+5];
    for(int i=1;i<=n;i++)
    {
        if(aaa[i]!=bbb[i])
        {
            pos[cnt++]=i;
        }
        else
        {
            mpp[aaa[i]]=1;
        }
    }

    if(cnt==1)
    {
        int p=pos[0];
        for(int j=1;j<=n;j++)
        {
            if(mpp[j]==0)
            {
                mpp[j]=1;
                aaa[p]=j;
                break;
            }
        }
    }
    else if(cnt==2)
    {

        int p1=pos[0],p2=pos[1];
        //cout<<p1<<' '<<p2<<endl;
        int x1=aaa[p1],y1=aaa[p2];
        int x2=bbb[p1],y2=bbb[p2];

        if(mpp[x1]==0 and mpp[y1]==0 and x1!=y1){
            aaa[p1]=x1,aaa[p2]=y1;
            //cout<<1<<endl;
        }
        else if(mpp[x2]==0 and mpp[y2==0] and x2!=y2){
            aaa[p1]=x2,aaa[p2]=y2;
            //cout<<2<<endl;
        }
        else if(mpp[x1]==0 and mpp[y2]==0 and x1!=y2){
            aaa[p1]=x1,aaa[p2]=y2;
            //cout<<3<<endl;
        }
        else if(mpp[x2]==0 and mpp[y1]==0 and x2!=y1){
            aaa[p1]=x2,aaa[p2]=y1;
            //cout<<4<<endl;
            //cout<<x2<<' '<<y1<<endl;
        }
    }

    for(int i=1;i<=n;i++)
    {
        cout<<aaa[i]<<' ';
    }
}


#include<bits/stdc++.h>
using namespace std;
int aaa[300],bbb[300],ccc[300];
#define sfi ({int a;scanf("%d",&a);a;})
map<int,int>mpp;
int main()
{
    int n=sfi;
    int aaa[n+5],bbb[n+5];
    for(int i=1;i<=n;i++)aaa[i]=sfi;
    for(int i=1;i<=n;i++)bbb[i]=sfi;

    int cnt=0,pos[n+5];
    for(int i=1;i<=n;i++)
    {
        if(aaa[i]!=bbb[i])
        {
            pos[cnt++]=i;
        }
        else
        {
            mpp[aaa[i]]=1;
        }
    }
    for(int i=0;i<cnt;i++)
    {
        int p=pos[i];
        if(mpp[aaa[p]]==0)
        {
            mpp[aaa[p]]=1;
        }
        else if(mpp[bbb[p]]==0)
        {
            mpp[bbb[p]]=1;
            aaa[p]=bbb[p];
        }
        else
        {
            for(int j=1;j<=n;j++)
            {
                if(mpp[j]==0)
                {
                    aaa[p]=j;
                    mpp[j]=1;
                    break;
                }
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        cout<<aaa[i]<<' ';
    }
}


#include <bits/stdc++.h>
using namespace std;
const int size=1e5+10;
const int INF=1e9;
int a[size];
int b[size];
int vis[size];
int vis2[size];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++) scanf("%d",&b[i]);
    int num=0;
    int pre=0,post=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]!=b[i])
        {
            num++;
            if(pre==0)
                pre=i;
            else
                post=i;
        }
        else
        {
            if(vis[a[i]])
            {
                num++;
                if(pre==0) pre=i;
                else post=i;
            }
            vis[a[i]]=1;
        }
    }
    if(num==1)
    {
        for(int i=1;i<=n;i++)
        {
            if(vis[i]==0)
            {
                a[pre]=i;
                break;
            }
        }
        for(int i=1;i<=n;i++)
            printf("%d ",a[i]);
    }
    else
    {

       memset(vis,0,sizeof(vis));
       for(int i=1;i<=n;i++)
       {
           vis[a[i]]=1;
           vis2[b[i]]=1;
       }
       int x=0,y;
       for(int i=1;i<=n;i++)
       {
           if(vis[i]==0)
            x=i;
           if(vis2[i]==0)
            y=i;
       }

       if(vis[a[pre]]==0) a[pre]=x;
       else a[post]=x;
       for(int i=1;i<=n;i++)
        printf("%d ",a[i]);
    }
}


#include <bits/stdc++.h>
using namespace std;
const int size=1e5+10;
const int INF=1e9;
int a[size];
int b[size];
int vis[size];
int vis2[size];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++) scanf("%d",&b[i]);
    int num=0;
    int pre=0,post=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]!=b[i])
        {
            num++;
            if(pre==0)
                pre=i;
            else
                post=i;
        }
        else
        {
            if(vis[a[i]])
            {
                num++;
                if(pre==0) pre=i;
                else post=i;
            }
            vis[a[i]]=1;
        }
    }
    if(num==1)
    {
        for(int i=1;i<=n;i++)
        {
            if(vis[i]==0)
            {
                a[pre]=i;
                break;
            }
        }
        for(int i=1;i<=n;i++)
            printf("%d ",a[i]);
    }
    else
    {
    int p[2000];
       memset(vis,0,sizeof(vis));
       for(int i=1;i<=n;i++)
       {
           vis[a[i]]++;
           vis2[b[i]]=1;
       }
       int x=0,y;
       for(int i=1;i<=n;i++)
       {
           if(vis[i]==0)
            x=i;
           if(vis2[i]==0)
            y=i;
       }
       for(int i=1;i<=n;i++)
        p[i]=a[i];
       p[pre]=x,p[post]=y;
       int flag=0;
       int nn1=0,nn2=0;
       for(int i=1;i<=n;i++)
       {
           if(p[i]!=a[i]) nn1++;
           if(p[i]!=b[i]) nn2++;
       }
       if(nn1!=1&&nn2!=1) swap(p[pre],p[post]);
      for(int i=1;i<=n;i++)
        printf("%d ",p[i]);
    }
}


#include <bits/stdc++.h>
using namespace std;
const int size=1e5+10;
const int INF=1e9;
int a[size];
int b[size];
int vis[size];
int vis2[size];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++) scanf("%d",&b[i]);
    int num=0;
    int pre=0,post=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]!=b[i])
        {
            num++;
            if(pre==0)
                pre=i;
            else
                post=i;
        }
        else
        {
            if(vis[a[i]])
            {
                num++;
                if(pre==0) pre=i;
                else post=i;
            }
            vis[a[i]]=1;
        }
    }
    if(num==1)
    {
        for(int i=1;i<=n;i++)
        {
            if(vis[i]==0)
            {
                a[pre]=i;
                break;
            }
        }
        for(int i=1;i<=n;i++)
            printf("%d ",a[i]);
    }
    else
    {
    int p[2000];
       memset(vis,0,sizeof(vis));
       for(int i=1;i<=n;i++)
       {
           vis[a[i]]++;
           vis2[b[i]]=1;
       }
       int x=0,y;
       for(int i=1;i<=n;i++)
       {
           if(vis[i]==0)
            x=i;
           if(vis2[i]==0)
            y=i;
       }
       for(int i=1;i<=n;i++)
        p[i]=a[i];
       p[pre]=x;
       int flag=0;
       int nn1=0,nn2=0;
       for(int i=1;i<=n;i++)
       {
           if(p[i]!=a[i]) nn1++;
           if(p[i]!=b[i]) nn2++;
       }
//       printf("%d %d\n",nn1,nn2);
       if(nn1!=1||nn2!=1) swap(p[pre],p[post]);
      for(int i=1;i<=n;i++)
        printf("%d ",p[i]);
    }
}


#include <bits/stdc++.h>
using namespace std;
const int size=1e5+10;
const int INF=1e9;
int a[size];
int b[size];
int vis[size];
int vis2[size];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++) scanf("%d",&b[i]);
    int num=0;
    int pre=0,post=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]!=b[i])
        {
            num++;
            if(pre==0)
                pre=i;
            else
                post=i;
        }
        else
        {
            if(vis[a[i]])
            {
                num++;
                if(pre==0) pre=i;
                else post=i;
            }
            vis[a[i]]=1;
        }
    }
    if(num==1)
    {
        for(int i=1;i<=n;i++)
        {
            if(vis[i]==0)
            {
                a[pre]=i;
                break;
            }
        }
        for(int i=1;i<=n;i++)
            printf("%d ",a[i]);
    }
    else
    {
    int p[2000];
       memset(vis,0,sizeof(vis));
       for(int i=1;i<=n;i++)
       {
           vis[a[i]]++;
           vis2[b[i]]=1;
       }
       int x=0,y;
       for(int i=1;i<=n;i++)
       {
           if(vis[i]==0)
            x=i;
           if(vis2[i]==0)
            y=i;
       }
       for(int i=1;i<=n;i++)
        p[i]=a[i];
       p[pre]=x,p[post]=y;
       int flag=0;
       int nn1=0,nn2=0;
       for(int i=1;i<=n;i++)
       {
           if(p[i]!=a[i]) nn1++;
           if(p[i]!=b[i]) nn2++;
       }
       if(nn1!=nn2) swap(p[pre],p[post]);
      for(int i=1;i<=n;i++)
        printf("%d ",p[i]);
    }
}


#include <bits/stdc++.h>
using namespace std;
const int size=1e5+10;
const int INF=1e9;
int a[size];
int b[size];
int vis[size];
int vis2[size];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++) scanf("%d",&b[i]);
    int num=0;
    int pre=0,post=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]!=b[i])
        {
            num++;
            if(pre==0)
                pre=i;
            else
                post=i;
        }
        else
        {
            if(vis[a[i]])
            {
                num++;
                if(pre==0) pre=i;
                else post=i;
            }
            vis[a[i]]=1;
        }
    }
    if(num==1)
    {
        for(int i=1;i<=n;i++)
        {
            if(vis[i]==0)
            {
                a[pre]=i;
                break;
            }
        }
        for(int i=1;i<=n;i++)
            printf("%d ",a[i]);
    }
    else
    {
    int p[2000];
       memset(vis,0,sizeof(vis));
       for(int i=1;i<=n;i++)
       {
           vis[a[i]]++;
           vis2[b[i]]=1;
       }
       int x=0,y;
       for(int i=1;i<=n;i++)
       {
           if(vis[i]==0)
            x=i;
           if(vis2[i]==0)
            y=i;
       }
       for(int i=1;i<=n;i++)
        p[i]=a[i];
       p[pre]=x;
       p[post]=y;
       int flag=0;
       int nn1=0,nn2=0;
       for(int i=1;i<=n;i++)
       {
           if(p[i]!=a[i]) nn1++;
           if(p[i]!=b[i]) nn2++;
       }
//       printf("%d %d\n",nn1,nn2);
       if(nn1!=1||nn2!=1) swap(p[pre],p[post]);
      for(int i=1;i<=n;i++)
        printf("%d ",p[i]);
    }
}


#include <bits/stdc++.h>
using namespace std;
const int size=1e5+10;
const int INF=1e9;
int a[size];
int b[size];
int vis[size];
int vis2[size];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++) scanf("%d",&b[i]);
    int num=0;
    int pre=0,post=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]!=b[i])
        {
            num++;
            if(pre==0)
                pre=i;
            else
                post=i;
        }
        else
        {
            if(vis[a[i]])
            {
                num++;
                if(pre==0) pre=i;
                else post=i;
            }
            vis[a[i]]=1;
        }
    }
    if(num==1)
    {
        for(int i=1;i<=n;i++)
        {
            if(vis[i]==0)
            {
                a[pre]=i;
                break;
            }
        }
        for(int i=1;i<=n;i++)
            printf("%d ",a[i]);
    }
    else
    {
       memset(vis,0,sizeof(vis));
       for(int i=1;i<=n;i++)
       {
           vis[a[i]]=1;
           vis2[b[i]]=1;
       }
       int x,y;
       for(int i=1;i<=n;i++)
       {
           if(vis[i]==0)
            x=i;
           if(vis2[i]==0)
            y=i;
       }
       a[pre]=x;
       for(int i=1;i<=n;i++)
        printf("%d ",a[i]);
    }
}


#include <bits/stdc++.h>
using namespace std;
const int size=1e5+10;
const int INF=1e9;
int a[size];
int b[size];
int vis[size];
int vis2[size];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++) scanf("%d",&b[i]);
    int num=0;
    int pre=0,post=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]!=b[i])
        {
            num++;
            if(pre==0)
                pre=i;
            else
                post=i;
        }
        else
        {
            if(vis[a[i]])
            {
                num++;
                if(pre==0) pre=i;
                else post=i;
            }
            vis[a[i]]=1;
        }
    }
    if(num==1)
    {
        for(int i=1;i<=n;i++)
        {
            if(vis[i]==0)
            {
                a[pre]=i;
                break;
            }
        }
        for(int i=1;i<=n;i++)
            printf("%d ",a[i]);
    }
    else
    {

       memset(vis,0,sizeof(vis));
       for(int i=1;i<=n;i++)
       {
           vis[a[i]]++;
           vis2[b[i]]=1;
       }
       int x=0,y;
       for(int i=1;i<=n;i++)
       {
           if(vis[i]==0)
            x=i;
           if(vis2[i]==0)
            y=i;
       }

       if(vis[a[pre]]==2) a[pre]=x;
       else a[post]=x;
       for(int i=1;i<=n;i++)
        printf("%d ",a[i]);
    }
}


#include <bits/stdc++.h>
using namespace std;
const int size=1e5+10;
const int INF=1e9;
int a[size];
int b[size];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++) scanf("%d",&b[i]);
    int num=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]!=b[i])
            num++;
    }
    if(num==0)
    {
        printf("%d ",a[1]+1);
        for(int i=2;i<=n;i++)
            printf("%d ",a[i]);
    }
    else if(num==2)
    {
        int flag=0;
        for(int i=1;i<=n;i++)
        {
            if(a[i]==b[i])
                printf("%d ",a[i]);
            else
            {
                if(!flag)
                   printf("%d ",b[i]);
                else
                    printf("%d ",a[i]);
                flag=1;
            }
        }
    }
    else if(num==1)
    {
        for(int i=1;i<=n;i++)
        {
              if(a[i]==b[i])
                printf("%d ",a[i]);
              else
              {
                  for(int j=1;j<=3;j++)
                  {
                      if(j!=a[i]&&j!=b[i])
                      {
                          printf("%d ",j);
                          break;
                      }
                  }
              }
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
const int size=1e5+10;
const int INF=1e9;
int a[size];
int b[size];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++) scanf("%d",&b[i]);
    int num=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]!=b[i])
            num++;
    }
    if(num==0)
    {
        printf("%d ",a[1]-1);
        for(int i=2;i<=n;i++)
            printf("%d ",a[i]);
    }
    else if(num==2)
    {
        int flag=0;
        for(int i=1;i<=n;i++)
        {
            if(a[i]==b[i])
                printf("%d ",a[i]);
            else
            {
                if(!flag)
                   printf("%d ",b[i]);
                else
                    printf("%d ",a[i]);
                flag=1;
            }
        }
    }
    else if(num==1)
    {
        for(int i=1;i<=n;i++)
        {
              if(a[i]==b[i])
                printf("%d ",a[i]);
              else
              {
                  for(int j=1;j<=3;j++)
                  {
                      if(j!=a[i]&&j!=b[i])
                      {
                          printf("%d ",j);
                          break;
                      }
                  }
              }
        }
    }
}


#include<bits/stdc++.h>
#define mp make_pair
#define f first
#define se second
#define pb push_back
#define ms memset
#define MOD 1000000007
using namespace std;
typedef long long ll;
bool pr[1000007];
void sieve(){pr[0]=1;pr[1]=1;for(int i=2;i<sqrt(1000007);i++){for(int j=2*i;j<=1000007;j+=i){pr[j]=1;}}}
ll fashtpower(ll x,ll y){ll res=1;while(y){if(y&1)res=res*x;res%=MOD;y=y>>1;x=x*x;x%=MOD;}return res;}
int ac[1003],bc[1003];
int main(){
		int n;
		cin>>n;
		int a[n+1],b[n+1];
		for(int i=0;i<n;i++){
			cin>>a[i];
			ac[i]++;
		}
		for(int i=0;i<n;i++){
			cin>>b[i];
			bc[b[i]]++;
		}int ca=0,cb=0,ea[3]={0},k=0,eb[3]={0};
		for(int i=1;i<=n;i++){
			if(ac[i]&&bc[i]) cout<<i<<" ";
			if(ac[i]&&!bc[i] || bc[i]&&!ac[i]){
				ea[k++]=i;
				ca++;
			}
		}
		if(ca==1){
			cout<<ea[0];
		}else{
			cout<<ea[0]<<" "<<ea[1];
		}

}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <bitset>
#include <set>
#include <map>
#include <queue>
#include <deque>
#define ll long long
using namespace std;

const int maxM = 100013;

map<pair<int, int>, int> ans;

int n, m;
vector<int> vec[maxM], a[maxM];

void paint(int num){
	vector<int> v = vec[num];
	a[num].resize(n);
	int color = 0;
	for(int i = 0; i < n - 1; i++){
		a[num][i] = color;
		if(v[i] > v[i + 1])color++;
	}
	a[num][n - 1] = color;
}

int main(){
//	freopen("test.txt", "r", stdin);
	ans.clear();
	cin >> n >> m;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			int t;
			cin >> t;
			vec[j].push_back(t);
		}
	}
	for(int i = 0; i < m; i++)
		paint(i);
	int k;
	cin >> k;
	for(int i = 0; i < k; i++){
		int l, r;
		cin >> l >> r;
		l--; r--;
		int x = ans[make_pair(l, r)];
		if(x == 2){
			cout << "Yes \n";
			continue;
		}
		if(x == 1){
			cout << "No \n";
			continue;
		}
		bool flag = 0;
		for(int j = 0; j < m; j++){
			if(a[j][l] == a[j][r]){
				cout << "Yes \n";
				ans[make_pair(l, r)] = 2;
				flag = 1;
			}
			if(flag)break;
		}
		if(!flag){
			cout << "No \n";
			ans[make_pair(l, r)] = 1;
		}
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <bitset>
#include <set>
#include <map>
#include <queue>
#include <deque>
#define ll long long
using namespace std;

const int maxM = 100013;

map<pair<int, int>, int> ans;

int n, m;
vector<int> vec[maxM], a[maxM];

void paint(int num){
	vector<int> v = vec[num];
	a[num].resize(n);
	int color = 0;
	for(int i = 0; i < n - 1; i++){
		a[num][i] = color;
		if(v[i] > v[i + 1])color++;
	}
	a[num][n - 1] = color;
}

int main(){
//	freopen("test.txt", "r", stdin);
	cin >> n >> m;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			int t;
			cin >> t;
			vec[j].push_back(t);
		}
	}
	for(int i = 0; i < m; i++)
		paint(i);
	int k;
	cin >> k;
	for(int i = 0; i < k; i++){
		int l, r;
		cin >> l >> r;
		l--; r--;
		int x = ans[make_pair(l, r)];
		if(x == 2){
			cout << "Yes";
			continue;
		}
		if(x == 1){
			cout << "No";
			continue;
		}
		bool flag = 0;
		for(int j = 0; j < m; j++){
			if(a[j][l] == a[j][r]){
				cout << "Yes \n";
				ans[make_pair(l, r)] = 2;
				flag = 1;
			}
			if(flag)break;
		}
		if(!flag){
			cout << "No \n";
			ans[make_pair(l, r)] = 1;
		}
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
vector<vector<int>>a;
vector<vector<int>>dp;
vector<int>maxn;
int n, m;
int main()
{
	scanf("%d%d", &n, &m);
	a.resize(n + 2);
	dp.resize(n + 2);
	maxn.resize(n + 2);
	for (int i = 0; i <= n; i++)
		a[i].resize(m + 2), dp[i].resize(m + 2);
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
		{
			int temp;
			scanf("%d", &temp);
			a[i][j] = temp;
			dp[i][j] = 1;
		}
	for(int i=0;i<m;i++)
		for (int j = 1; j < n; j++)
		{
			if (a[j][i] >= a[j - 1][i]) dp[j][i] = max(dp[j][i], dp[j - 1][i] + 1);
			else dp[j][i] = 1;
		}
	for (int i = 0; i<n; i++)
		for (int j = 0; j < m; j++)
		{
			maxn[i] = max(maxn[i], dp[i][j]);
		}
	int k;
	int l, r;
	scanf("%d", &k);
	for (int i = 0; i < k; i++)
	{
		scanf("%d%d", &l, &r);
		l--;
		r--;
		if (r - l + 1 <= maxn[r]) printf("Yes\n");
		else printf("No\n");
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void main() {
	FILE *f;
	//freopen_s(&f, "input.txt", "r", stdin);

	int n, m;
	vector < vector <int> > w;

	cin >> n >> m;
	w.resize(n, vector <int> (m));

	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			cin >> w[i][j];

	int k;
	cin >> k;

	vector < vector < int > > up(m, vector <int> (n, n-1));

	for (int i = 0; i < m; i++)
		for (int j = n - 2; j >= 0; j--)
			if (w[j][i] <= w[j+1][i])
				up[i][j] = up[i][j+1];
			else
				up[i][j] = j;

	vector <int> maxx(n, -1);
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			maxx[i] = max(maxx[i], up[j][i]);

	for (int i = 0; i < k; i++) {
		int l, r;
		cin >> l >> r;
		l--; r--;
		if (maxx[l] >= r) cout << "Yes" << endl; else cout << "No" << endl;
	}

}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void main() {
	FILE *f;
	freopen_s(&f, "input.txt", "r", stdin);

	int n, m;
	vector < vector <int> > w;

	cin >> n >> m;
	w.resize(n, vector <int> (m));

	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			cin >> w[i][j];

	int k;
	cin >> k;

	vector < vector < int > > up(m, vector <int> (n, n-1));

	for (int i = 0; i < m; i++)
		for (int j = n - 2; j >= 0; j--)
			if (w[j][i] <= w[j+1][i])
				up[i][j] = up[i][j+1];
			else
				up[i][j] = j;

	vector <int> maxx(n, -1);
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			maxx[i] = max(maxx[i], up[j][i]);

	for (int i = 0; i < k; i++) {
		int l, r;
		cin >> l >> r;
		l--; r--;
		if (maxx[l] >= r) cout << "Yes" << endl; else cout << "No" << endl;
	}

}


#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
#include <bitset>
#include <stack>
#include <set>
#include <list>
#include <sstream>
#include <functional>
#define INF 0x3f3fffff
#define MAX_V 30
#define MAX_N 50000001
#define MOD 1000000007
#define TAG printf("here\n");
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;

int a[100001]; vector<int> dp[1000][1000];
int main() {
	int n, m, k, l, r;
	while (cin >> n >> m) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cin >> a[i * n + j];
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++)
				dp[i][i].push_back(j);
		}
		for (int i = 0; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				for (int k = 0; k < dp[i][j - 1].size(); k++) {
					int col = dp[i][j - 1][k];
					if (a[j * n + col] >= a[(j - 1) * n + col])
						dp[i][j].push_back(col);
				}
			}
		}
		cin >> k;
		for (int i = 0; i < k; i++) {
			cin >> l >> r;
			if (dp[l - 1][r - 1].size()) puts("Yes");
			else puts("No");
		}
	}
	return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
#include <bitset>
#include <stack>
#include <set>
#include <list>
#include <sstream>
#include <functional>
#define INF 0x3f3fffff
#define MAX_V 30
#define MAX_N 50000001
#define MOD 1000000007
#define TAG printf("here\n");
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;

int a[1000][1000]; bool dp[400][400][400];
int main() {
	int n, m, k, l, r;
	while (cin >> n >> m) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cin >> a[i][j];
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++)
				dp[i][i][j] = true;
		}
		for (int i = 0; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				for (int k = 0; k < m; k++) {
					if (dp[i][j - 1][k] && a[j][k] >= a[j - 1][k])
						dp[i][j][k] = true;
				}
			}
		}
		cin >> k;
		for (int i = 0; i < k; i++) {
			cin >> l >> r;
			bool ret = false;
			for (int i = 0; i < m; i++) {
				if (dp[l - 1][r - 1][i]) ret = true;
			}
			if (ret) puts("Yes");
			else puts("No");
		}
	}
	return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
#include <bitset>
#include <stack>
#include <set>
#include <list>
#include <sstream>
#include <functional>
#define INF 0x3f3fffff
#define MAX_V 30
#define MAX_N 50000001
#define MOD 1000000007
#define TAG printf("here\n");
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;

int a[100001]; vector<int> dp[100001];
int main() {
	int n, m, k, l, r;
	while (cin >> n >> m) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cin >> a[i * m + j];
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++)
				dp[i * m + i].push_back(j);
		}
		for (int i = 0; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				for (int k = 0; k < dp[i * m + j - 1].size(); k++) {
					int col = dp[i * m + j - 1][k];
					if (a[j * m + col] >= a[(j - 1) * m + col])
						dp[i * m + j].push_back(col);
				}
			}
		}
		cin >> k;
		for (int i = 0; i < k; i++) {
			cin >> l >> r;
			if (dp[(l - 1) * m + r - 1].size()) puts("Yes");
			else puts("No");
		}
	}
	return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
#include <bitset>
#include <stack>
#include <set>
#include <list>
#include <sstream>
#include <functional>
#define INF 0x3f3fffff
#define MAX_V 30
#define MAX_N 50000001
#define MOD 1000000007
#define TAG printf("here\n");
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;

int a[100001]; vector<int> dp[1000][1000];
int main() {
	int n, m, k, l, r;
	while (cin >> n >> m) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cin >> a[i * m + j];
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++)
				dp[i][i].push_back(j);
		}
		for (int i = 0; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				for (int k = 0; k < dp[i][j - 1].size(); k++) {
					int col = dp[i][j - 1][k];
					if (a[j * m + col] >= a[(j - 1) * m + col])
						dp[i][j].push_back(col);
				}
			}
		}
		cin >> k;
		for (int i = 0; i < k; i++) {
			cin >> l >> r;
			if (dp[l - 1][r - 1].size()) puts("Yes");
			else puts("No");
		}
	}
	return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
#include <bitset>
#include <stack>
#include <set>
#include <list>
#include <sstream>
#include <functional>
#define INF 0x3f3fffff
#define MAX_V 30
#define MAX_N 50000001
#define MOD 1000000007
#define TAG printf("here\n");
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;

int a[1000][1000]; bool dp[1000][1000][1000];
int main() {
	int n, m, k, l, r;
	while (cin >> n >> m) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cin >> a[i][j];
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++)
				dp[i][i][j] = true;
		}
		for (int i = 0; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				for (int k = 0; k < m; k++) {
					if (dp[i][j - 1][k] && a[j][k] >= a[j - 1][k])
						dp[i][j][k] = true;
				}
			}
		}
		cin >> k;
		for (int i = 0; i < k; i++) {
			cin >> l >> r;
			bool ret = false;
			for (int i = 0; i < m; i++) {
				if (dp[l - 1][r - 1][i]) ret = true;
			}
			if (ret) puts("YES");
			else puts("NO");
		}
	}
	return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
#include <bitset>
#include <stack>
#include <set>
#include <list>
#include <sstream>
#include <functional>
#define INF 0x3f3fffff
#define MAX_V 30
#define MAX_N 50000001
#define MOD 1000000007
#define TAG printf("here\n");
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;

int a[1000][1000]; bool dp[700][700][700];
int main() {
	int n, m, k, l, r;
	while (cin >> n >> m) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cin >> a[i][j];
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++)
				dp[i][i][j] = true;
		}
		for (int i = 0; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				for (int k = 0; k < m; k++) {
					if (dp[i][j - 1][k] && a[j][k] >= a[j - 1][k])
						dp[i][j][k] = true;
				}
			}
		}
		cin >> k;
		for (int i = 0; i < k; i++) {
			cin >> l >> r;
			bool ret = false;
			for (int i = 0; i < m; i++) {
				if (dp[l - 1][r - 1][i]) ret = true;
			}
			if (ret) puts("Yes");
			else puts("No");
		}
	}
	return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
#include <bitset>
#include <stack>
#include <set>
#include <list>
#include <sstream>
#include <functional>
#define INF 0x3f3fffff
#define MAX_V 30
#define MAX_N 50000001
#define MOD 1000000007
#define TAG printf("here\n");
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;

int a[100001]; vector<int> dp[100001];
int main() {
	int n, m, k, l, r;
	while (cin >> n >> m) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cin >> a[i * m + j];
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++)
				dp[i * n + i].push_back(j);
		}
		for (int i = 0; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				for (int k = 0; k < dp[i * n + j - 1].size(); k++) {
					int col = dp[i * n + j - 1][k];
					if (a[j * m + col] >= a[(j - 1) * m + col])
						dp[i * n + j].push_back(col);
				}
			}
		}
		cin >> k;
		for (int i = 0; i < k; i++) {
			cin >> l >> r;
			if (dp[(l - 1) * n + r - 1].size()) puts("Yes");
			else puts("No");
		}
	}
	return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
#include <bitset>
#include <stack>
#include <set>
#include <list>
#include <sstream>
#include <functional>
#define INF 0x3f3fffff
#define MAX_V 30
#define MAX_N 50000001
#define MOD 1000000007
#define TAG printf("here\n");
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;

int a[100001]; int dp[100001]; int ml[100001];
int main() {
	int n, m, k, l, r;
	while (cin >> n >> m) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cin >> a[i * m + j];
			}
		}
		for (int j = 0; j < m; j++) ml[j] = 1;
		dp[0] = 1;
		for (int i = 1; i < n; i++) {
			int res = 0;
			for (int j = 0; j < m; j++) {
				if (a[i * m + j] >= a[(i - 1) * m + j]) {
					ml[i * m + j] = ml[(i - 1) * m + j] + 1;
				}
				else ml[i * m + j] = 1;
				res = max(res, ml[i * m + j]);
			}
			dp[i] = res;
		}
		cin >> k;
		for (int i = 0; i < k; i++) {
			cin >> l >> r;
			if (r - l + 1 <= dp[r - 1]) puts("Yes");
			else puts("No");
		}
	}
	return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
#include <bitset>
#include <stack>
#include <set>
#include <list>
#include <sstream>
#include <functional>
#define INF 0x3f3fffff
#define MAX_V 30
#define MAX_N 50000001
#define MOD 1000000007
#define TAG printf("here\n");
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;

int a[1000][1000]; bool dp[400][400][400];
int main() {
	int n, m, k, l, r;
	while (cin >> n >> m) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cin >> a[i][j];
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++)
				dp[i][i][j] = true;
		}
		for (int i = 0; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				for (int k = 0; k < m; k++) {
					if (dp[i][j - 1][k] && a[j][k] >= a[j - 1][k])
						dp[i][j][k] = true;
				}
			}
		}
		cin >> k;
		for (int i = 0; i < k; i++) {
			cin >> l >> r;
			bool ret = false;
			for (int i = 0; i < m; i++) {
				if (dp[l - 1][r - 1][i]) ret = true;
			}
			if (ret) puts("YES");
			else puts("NO");
		}
	}
	return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
#include <bitset>
#include <stack>
#include <set>
#include <list>
#include <sstream>
#include <functional>
#define INF 0x3f3fffff
#define MAX_V 30
#define MAX_N 50000001
#define MOD 1000000007
#define TAG printf("here\n");
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;

int a[1000][1000]; vector<int> dp[1000][1000];
int main() {
	int n, m, k, l, r;
	while (cin >> n >> m) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cin >> a[i][j];
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++)
				dp[i][i].push_back(j);
		}
		for (int i = 0; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				for (int k = 0; k < dp[i][j - 1].size(); k++) {
					int col = dp[i][j - 1][k];
					if (a[j][col] >= a[j - 1][col])
						dp[i][j].push_back(col);
				}
			}
		}
		cin >> k;
		for (int i = 0; i < k; i++) {
			cin >> l >> r;
			if (dp[l - 1][r - 1].size()) puts("Yes");
			else puts("No");
		}
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <queue>
#include <iomanip>
#include <cstdio>
#include <limits>
#include <cmath>
#include <cstdlib>
#include <string.h>
#include <ctime>
#include <assert.h>

using namespace std;

#define VI vector<int>
#define VVI vector<VI >
#define VB vector<bool>
#define VVB vector<VB >
#define VS vector<string>
#define PII pair<int, int>
#define VPII vector<pair<int, int> >
#define REP(var, startFrom, endTo) for(int var = startFrom; var < (int)endTo; ++var)

template <typename T>
inline const T MAX(T a, T b) { return ((a) > (b) ? (a) : (b)); }
template <typename T>
inline const T MIN(T a, T b) { return ((a) > (b) ? (b) : (a)); }
template <typename T>
inline const T ABS(T a) { return (((a) > 0) ? (a) : (-(a))); }

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("tmp.in", "r", stdin);
	freopen("tmp.out", "w", stderr);
#endif
	int row, col;
	cin >> row >> col;

	VVI Mat = VVI(row, VI(col));
	REP(i, 0, row)
		REP(j, 0, col)
		scanf("%d", &Mat[i][j]);

	VI range = VI(row);
	REP(i, 0, col)
		REP(j, 1, row)
	{
		int init = j - 1;

		while (j < row)
		{
			if (Mat[j - 1][i] > Mat[j][i])
				break;
			++j;
		}
		if (j - init > 1)
		{
			for (int k = init; k < j; ++k)
				range[k] = MIN(range[k], j - 1);
		}
	}

	int k;
	cin >> k;
	REP(i, 0, k)
	{
		int l, r;
		scanf("%d%d", &l, &r);

		if (l == r)
		{
			printf("Yes\n");
			continue;
		}
		--l; --r;

		if (range[l] >= r)
			printf("Yes\n");
		else
			printf("No\n");
	}

	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <queue>
#include <iomanip>
#include <cstdio>
#include <limits>
#include <cmath>
#include <cstdlib>
#include <string.h>
#include <ctime>
#include <assert.h>

using namespace std;

#define VI vector<int>
#define VVI vector<VI >
#define VB vector<bool>
#define VVB vector<VB >
#define VS vector<string>
#define PII pair<int, int>
#define VPII vector<pair<int, int> >
#define REP(var, startFrom, endTo) for(int var = startFrom; var < (int)endTo; ++var)

template <typename T>
inline const T MAX(T a, T b) { return ((a) > (b) ? (a) : (b)); }
template <typename T>
inline const T MIN(T a, T b) { return ((a) > (b) ? (b) : (a)); }
template <typename T>
inline const T ABS(T a) { return (((a) > 0) ? (a) : (-(a))); }

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("tmp.in", "r", stdin);
	freopen("tmp.out", "w", stderr);
#endif
	int row, col;
	cin >> row >> col;

	VVI Mat = VVI(row, VI(col));
	REP(i, 0, row)
		REP(j, 0, col)
		cin >> Mat[i][j];

	VI range = VI(row);
	REP(i, 0, col)
		REP(j, 1, row)
	{
		int init = j - 1;

		while (j < row)
		{
			if (Mat[j - 1][i] > Mat[j][i])
				break;
			++j;
		}

		for (int k = init; k < j; ++k)
			range[k] = MAX(range[k], j - 1);

	}

	int k;
	cin >> k;
	REP(i, 0, k)
	{
		int l, r;
		cin >> l >> r;

		if (l == r)
		{
			cout << "Yes\n";
			//printf("Yes\n");
			continue;
		}
		--l; --r;

		if (range[l] >= r)
			cout << "Yes\n";
		else
			cout << "No\n";
	}

	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <queue>
#include <iomanip>
#include <cstdio>
#include <limits>
#include <cmath>
#include <cstdlib>
#include <string.h>
#include <ctime>
#include <assert.h>

using namespace std;

#define VI vector<int>
#define VVI vector<VI >
#define VB vector<bool>
#define VVB vector<VB >
#define VS vector<string>
#define PII pair<int, int>
#define VPII vector<pair<int, int> >
#define REP(var, startFrom, endTo) for(int var = startFrom; var < (int)endTo; ++var)

template <typename T>
inline const T MAX(T a, T b) { return ((a) > (b) ? (a) : (b)); }
template <typename T>
inline const T MIN(T a, T b) { return ((a) > (b) ? (b) : (a)); }
template <typename T>
inline const T ABS(T a) { return (((a) > 0) ? (a) : (-(a))); }

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("tmp.in", "r", stdin);
//	freopen("tmp.out", "w", stderr);
#endif
	int row, col;
	cin >> row >> col;

	VVI Mat = VVI(row, VI(col));
	REP(i, 0, row)
		REP(j, 0, col)
		scanf("%d", &Mat[i][j]);

	VPII ranges;
	REP(i, 0, col)
		REP(j, 1, row)
	{
		int init = j - 1, dest = j;
		while (dest < row)
		{
			if (Mat[dest - 1][i] > Mat[dest][i])
				break;
			++dest;
		}
		--dest;
		if(dest == init)
			continue;
		if (ranges.empty())
			ranges.push_back(PII(init, dest));
		else
		{
			bool valid = true;
			REP(k, 0, ranges.size())
				if (init >= ranges[k].first && dest <= ranges[k].second)
				{
					valid = false;
					break;
				}
			if(valid)
				ranges.push_back(PII(init, dest));
		}
		dest = j;
	}

	int k;
	cin >> k;

	for (int i = 0; i < k; ++i)
	{
		int left, right;
		scanf("%d%d", &left, &right);

		if (left == right)
		{
			printf("Yes\n");
			continue;
		}

		--left; --right;
		bool valid = false;
		for(int j = 0; j < ranges.size(); ++j)
			if (left >= ranges[j].first && right <= ranges[j].second)
			{
				valid = true;
				break;
			}
		if(valid)
			printf("Yes\n");
		else
			printf("No\n");
	}

	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <queue>
#include <iomanip>
#include <cstdio>
#include <limits>
#include <cmath>
#include <cstdlib>
#include <string.h>
#include <ctime>
#include <assert.h>

using namespace std;

#define VI vector<int>
#define VVI vector<VI >
#define VB vector<bool>
#define VVB vector<VB >
#define VS vector<string>
#define PII pair<int, int>
#define VPII vector<pair<int, int> >
#define REP(var, startFrom, endTo) for(int var = startFrom; var < (int)endTo; ++var)

template <typename T>
inline const T MAX(T a, T b) { return ((a) > (b) ? (a) : (b)); }
template <typename T>
inline const T MIN(T a, T b) { return ((a) > (b) ? (b) : (a)); }
template <typename T>
inline const T ABS(T a) { return (((a) > 0) ? (a) : (-(a))); }

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("tmp.in", "r", stdin);
//	freopen("tmp.out", "w", stderr);
#endif
	int row, col;
	cin >> row >> col;

	VVI Mat = VVI(row, VI(col));
	REP(i, 0, row)
		REP(j, 0, col)
		scanf("%d", &Mat[i][j]);

	VPII ranges;
	REP(i, 0, col)
		REP(j, 1, row)
	{
		int init = j - 1, dest = j;
		while (dest < row)
		{
			if (Mat[dest - 1][i] > Mat[dest][i])
				break;
			++dest;
		}
		--dest;
		if(dest == init)
			continue;
		if (ranges.empty())
			ranges.push_back(PII(init, dest));
		else
		{
			bool valid = true;
			REP(k, 0, ranges.size())
				if (init >= ranges[k].first && dest <= ranges[k].second)
				{
					valid = false;
					break;
				}
			if(valid)
				ranges.push_back(PII(init, dest));
		}
		dest = j;
	}

	int k;
	cin >> k;

	for (int i = 0; i < k; ++i)
	{
		int left, right;
		scanf("%d%d", &left, &right);

		if (left == right)
		{
			printf("YES\n");
			continue;
		}

		--left; --right;
		bool valid = false;
		for(int j = 0; j < ranges.size(); ++j)
			if (left >= ranges[j].first && right <= ranges[j].second)
			{
				valid = true;
				break;
			}
		if(valid)
			printf("YES\n");
		else
			printf("NO\n");
	}

	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <queue>
#include <iomanip>
#include <cstdio>
#include <limits>
#include <cmath>
#include <cstdlib>
#include <string.h>
#include <ctime>
#include <assert.h>

using namespace std;

#define VI vector<int>
#define VVI vector<VI >
#define VB vector<bool>
#define VVB vector<VB >
#define VS vector<string>
#define PII pair<int, int>
#define VPII vector<pair<int, int> >
#define REP(var, startFrom, endTo) for(int var = startFrom; var < (int)endTo; ++var)

template <typename T>
inline const T MAX(T a, T b) { return ((a) > (b) ? (a) : (b)); }
template <typename T>
inline const T MIN(T a, T b) { return ((a) > (b) ? (b) : (a)); }
template <typename T>
inline const T ABS(T a) { return (((a) > 0) ? (a) : (-(a))); }

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("tmp.in", "r", stdin);
	freopen("tmp.out", "w", stderr);
#endif
	int row, col;
	cin >> row >> col;

	VVI Mat = VVI(row, VI(col));
	REP(i, 0, row)
		REP(j, 0, col)
		scanf("%d", &Mat[i][j]);

	VI range = VI(row);
	REP(i, 0, col)
		REP(j, 1, row)
	{
		int init = j - 1;

		while (j < row)
		{
			if (Mat[j - 1][i] > Mat[j][i])
				break;
			++j;
		}
		if (j - 1 - init > 1)
		{
			for (int k = init; k < j; ++k)
			{
				if (range[k] < j - 1)
					range[k] = j - 1;
			}
		}
	}

	int k;
	cin >> k;
	REP(i, 0, k)
	{
		int l, r;
		scanf("%d%d", &l, &r);

		if (l == r)
		{
			printf("Yes\n");
			continue;
		}
		--l; --r;

		if (range[l] >= r)
			printf("Yes\n");
		else
			printf("No\n");
	}

	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <queue>
#include <iomanip>
#include <cstdio>
#include <limits>
#include <cmath>
#include <cstdlib>
#include <string.h>
#include <ctime>
#include <assert.h>

using namespace std;

#define VI vector<int>
#define VVI vector<VI >
#define VB vector<bool>
#define VVB vector<VB >
#define VS vector<string>
#define PII pair<int, int>
#define VPII vector<pair<int, int> >
#define REP(var, startFrom, endTo) for(int var = startFrom; var < (int)endTo; ++var)

template <typename T>
inline const T MAX(T a, T b) { return ((a) > (b) ? (a) : (b)); }
template <typename T>
inline const T MIN(T a, T b) { return ((a) > (b) ? (b) : (a)); }
template <typename T>
inline const T ABS(T a) { return (((a) > 0) ? (a) : (-(a))); }

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("tmp.in", "r", stdin);
	freopen("tmp.out", "w", stderr);
#endif
	int row, col;
	cin >> row >> col;

	VVI Mat = VVI(row, VI(col));
	REP(i, 0, row)
		REP(j, 0, col)
		scanf("%d", &Mat[i][j]);

	VI range = VI(row);
	REP(i, 0, col)
		REP(j, 1, row)
	{
		int init = j - 1;

		while (j < row)
		{
			if (Mat[j - 1][i] > Mat[j][i])
				break;
			++j;
		}

		for (int k = init; k < j; ++k)
			range[k] = MAX(range[k], j - 1);

	}

	int k;
	cin >> k;
	REP(i, 0, k)
	{
		int l, r;
		scanf("%d%d", &l, &r);

		if (l == r)
		{
			printf("Yes\n");
			continue;
		}
		--l; --r;

		if (range[l] >= r)
			printf("Yes\n");
		else
			printf("No\n");
	}

	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <queue>
#include <iomanip>
#include <cstdio>
#include <limits>
#include <cmath>
#include <cstdlib>
#include <string.h>
#include <ctime>
#include <assert.h>

using namespace std;

#define VI vector<int>
#define VVI vector<VI >
#define VB vector<bool>
#define VVB vector<VB >
#define VS vector<string>
#define PII pair<int, int>
#define VPII vector<pair<int, int> >
#define REP(var, startFrom, endTo) for(int var = startFrom; var < (int)endTo; ++var)

template <typename T>
inline const T MAX(T a, T b) { return ((a) > (b) ? (a) : (b)); }
template <typename T>
inline const T MIN(T a, T b) { return ((a) > (b) ? (b) : (a)); }
template <typename T>
inline const T ABS(T a) { return (((a) > 0) ? (a) : (-(a))); }

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("tmp.in", "r", stdin);
//	freopen("tmp.out", "w", stderr);
#endif
	int row, col;
	cin >> row >> col;

	VVI Mat = VVI(row, VI(col));
	REP(i, 0, row)
		REP(j, 0, col)
		scanf("%d", &Mat[i][j]);

	VPII ranges;
	REP(i, 0, col)
		REP(j, 1, row)
	{
		int init = j - 1, dest = j;
		while (dest < row)
		{
			if (Mat[dest - 1][i] > Mat[dest][i])
				break;
			++dest;
		}
		--dest;
		if(dest == init)
			continue;
		if (ranges.empty())
			ranges.push_back(PII(init, dest));
		else
		{
			bool valid = true;
			REP(k, 0, ranges.size())
				if (init >= ranges[k].first && dest <= ranges[k].second)
				{
					valid = false;
					break;
				}
			if(valid)
				ranges.push_back(PII(init, dest));
		}
		j = dest;
	}

	int k;
	cin >> k;

	for (int i = 0; i < k; ++i)
	{
		int left, right;
		scanf("%d%d", &left, &right);

		if (left == right)
		{
			printf("Yes\n");
			continue;
		}

		--left; --right;
		bool valid = false;
		for(int j = 0; j < ranges.size(); ++j)
			if (left >= ranges[j].first && right <= ranges[j].second)
			{
				valid = true;
				break;
			}
		if(valid)
			printf("Yes\n");
		else
			printf("No\n");
	}

	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <queue>
#include <iomanip>
#include <cstdio>
#include <limits>
#include <cmath>
#include <cstdlib>
#include <string.h>
#include <ctime>
#include <assert.h>

using namespace std;

#define VI vector<int>
#define VVI vector<VI >
#define VB vector<bool>
#define VVB vector<VB >
#define VS vector<string>
#define PII pair<int, int>
#define VPII vector<pair<int, int> >
#define REP(var, startFrom, endTo) for(int var = startFrom; var < (int)endTo; ++var)

template <typename T>
inline const T MAX(T a, T b) { return ((a) > (b) ? (a) : (b)); }
template <typename T>
inline const T MIN(T a, T b) { return ((a) > (b) ? (b) : (a)); }
template <typename T>
inline const T ABS(T a) { return (((a) > 0) ? (a) : (-(a))); }

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("tmp.in", "r", stdin);
	freopen("tmp.out", "w", stderr);
#endif
	int row, col;
	cin >> row >> col;

	VVI Mat = VVI(row, VI(col));
	REP(i, 0, row)
		REP(j, 0, col)
		scanf("%d", &Mat[i][j]);

	VI range = VI(row);
	REP(i, 0, col)
		REP(j, 1, row)
	{
		int init = j - 1;

		while (j < row)
		{
			if (Mat[j - 1][i] > Mat[j][i])
				break;
			++j;
		}
		if (j - init > 1)
		{
			for (int k = init; k < j; ++k)
				range[k] = MAX(range[k], j - 1);
		}
	}

	int k;
	cin >> k;
	REP(i, 0, k)
	{
		int l, r;
		scanf("%d%d", &l, &r);

		if (l == r)
		{
			printf("Yes\n");
			continue;
		}
		--l; --r;

		if (range[l] >= r)
			printf("Yes\n");
		else
			printf("No\n");
	}

	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <queue>
#include <iomanip>
#include <cstdio>
#include <limits>
#include <cmath>
#include <cstdlib>
#include <string.h>
#include <ctime>
#include <assert.h>

using namespace std;

#define VI vector<int>
#define VVI vector<VI >
#define VB vector<bool>
#define VVB vector<VB >
#define VS vector<string>
#define PII pair<int, int>
#define VPII vector<pair<int, int> >
#define REP(var, startFrom, endTo) for(int var = startFrom; var < (int)endTo; ++var)

template <typename T>
inline const T MAX(T a, T b) { return ((a) > (b) ? (a) : (b)); }
template <typename T>
inline const T MIN(T a, T b) { return ((a) > (b) ? (b) : (a)); }
template <typename T>
inline const T ABS(T a) { return (((a) > 0) ? (a) : (-(a))); }

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("tmp.in", "r", stdin);
	freopen("tmp.out", "w", stderr);
#endif
	int row, col;
	cin >> row >> col;

	VVI Mat = VVI(row, VI(col));
	REP(i, 0, row)
		REP(j, 0, col)
		scanf("%d", &Mat[i][j]);

	VI range = VI(row);
	REP(i, 0, col)
		REP(j, 1, row)
	{
		int init = j - 1;

		while (j < row)
		{
			if (Mat[j - 1][i] > Mat[j][i])
				break;
			++j;
		}
		if (j - init > 1)
		{
			for (int k = init; k < j; ++k)
			{
				if (range[k] < j - 1)
					range[k] = j - 1;
			}
		}
	}

	int k;
	cin >> k;
	REP(i, 0, k)
	{
		int l, r;
		scanf("%d%d", &l, &r);

		if (l == r)
		{
			printf("Yes\n");
			continue;
		}
		--l; --r;

		if (range[l] >= r)
			printf("Yes\n");
		else
			printf("No\n");
	}

	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <queue>
#include <iomanip>
#include <cstdio>
#include <limits>
#include <cmath>
#include <cstdlib>
#include <string.h>
#include <ctime>
#include <assert.h>

using namespace std;

#define VI vector<int>
#define VVI vector<VI >
#define VB vector<bool>
#define VVB vector<VB >
#define VS vector<string>
#define PII pair<int, int>
#define VPII vector<pair<int, int> >
#define REP(var, startFrom, endTo) for(int var = startFrom; var < (int)endTo; ++var)

template <typename T>
inline const T MAX(T a, T b) { return ((a) > (b) ? (a) : (b)); }
template <typename T>
inline const T MIN(T a, T b) { return ((a) > (b) ? (b) : (a)); }
template <typename T>
inline const T ABS(T a) { return (((a) > 0) ? (a) : (-(a))); }

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("tmp.in", "r", stdin);
	freopen("tmp.out", "w", stderr);
#endif
	int row, col;
	cin >> row >> col;

	VVI Mat = VVI(row, VI(col));
	REP(i, 0, row)
		REP(j, 0, col)
		scanf("%d", &Mat[i][j]);

	VI range = VI(row);
	REP(i, 0, col)
		REP(j, 1, row)
	{
		int init = j - 1;

		while (j < row)
		{
			if (Mat[j - 1][i] > Mat[j][i])
				break;
			++j;
		}

		for (int k = init; k < j; ++k)
			range[k] = MAX(range[k], j - 1);

	}

	int k;
	cin >> k;
	REP(i, 0, k)
	{
		int l, r;
		scanf("%d%d", &l, &r);

		if (l == r)
		{
			cout << "Yes\n";
			//printf("Yes\n");
			continue;
		}
		--l; --r;

		if (range[l] >= r)
			cout << "Yes\n";
		else
			cout << "No\n";
	}

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <queue>
#include <math.h>
#include <map>
#include <set>
#include <stdio.h>
#include <complex>
#define ll long long
#define ff first
#define ss second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pib pair<int, bool>
#define pdd pair<double, double>
#define pss pair<string, string>
#define ull unsigned long long
#pragma comment(linker, "/STACK:251777215")
using namespace std;
const ll maxn = 500001;
const ll maxseg = 262144;
const ll mod = 1e9 + 7;
const double pi = 3.141592653589793238462643383279502884;
const double eps = 1e-5;

int n, m, k;
vector<int> a[maxn];
vector<int> dp[maxn];
int res_dp[maxn];

int main()
{
	cin >> n >> m;
	for (int i = 0; i < n; ++i)
		a[i].resize(m), dp[i].resize(m);
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < m; ++j)
			cin >> a[i][j];
	for (int i = 0; i < m; ++i)
	{
		int last_pos = n - 1;
		dp[n - 1][i] = last_pos;
		for (int j = n - 2; j >= 0; --j)
		{
			if (a[j][i] > a[j + 1][i])
				last_pos = j;
			dp[j][i] = last_pos;
		}
	}
	for (int i = 0; i < n; ++i)
	{
		int best_dp = -1;
		for (int j = 0; j < m; ++j)
			best_dp = max(best_dp, dp[i][j]);
		res_dp[i] = best_dp;
	}
	cin >> k;
	for (int i = 0; i < k; ++i)
	{
		int x, y;
		cin >> x >> y;
		--x, --y;
		cout << ((res_dp[x] >= y) ? "Yes" : "No") << '\n';
	}
	return 0;
}
/*
413527915
*/


#include <iostream>
using namespace std ;

#define max_num 100009
int main()
{                               //各数组初始化为零
    int line_data[max_num] ;   //存储一行数据
    int col_bottom[max_num] ;  //每一列当前最底端的递减对 （所在下行）
    int line_far[max_num] ;    //距离每一行最远的递减对（所在下行）

    int row ;
    int col ;
    cin>>row>>col ;

    for( int i = 1 ; i <= row ; ++ i ){
        line_far[i] = i ;

        for( int j = 1 ; j <= col ; ++ j ){
            int number ;
            cin>>number ;

            if( number < line_data[j] )
                col_bottom[j] = i ;
            line_data[j] = number ;

            if( col_bottom[j] < line_far[i] )
                line_far[i] = col_bottom[j] ;
        }
    }

    int test_time ;
    cin>>test_time ;

    for( int i = 0 ; i < test_time ; ++ i ){
        int top ;
        int bottom ;
        cin>>top>>bottom ;

        if( line_far[bottom] <= top )
            cout<<"Yes"<<endl ;
        else cout<<"No"<<endl ;
    }

    return 0 ;
}


#ifndef 2_H_INCLUDED
#define 2_H_INCLUDED
//贪心算法
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int main() {
	int N;
	cin >> N;
	string a,b;
	cin >> a >> b;
	sort(a.begin(), a.end());
	sort(b.begin(), b.end());
	int x = 0, y=0;
	for(int i = 0, j = 0;  j < N; j++) {
		if(b[j] >= a[i]) i++;
		else x++;
	}
	for(int i = N-1, j = N-1; i >= 0; i--) {
		if(b[j] > a[i]) {
			j--;
			y++;
		}
	}
	cout << x << endl << y << endl;
	return 0;
}

int num_time ;
    for( int i = 0 ; i < num_time ; ++ num_time ){

        int top ;
        int bottom ;
        cin>>top>>bottom ;

        if( top == bottom ){
            cout<<"Yes" ;
            continue ;
        }
        bool *result = new bool[col] ;
        for( int j = 0 ; j < col ; ++ j )
            result[j] = false ;

        for( int k = 0 ; k <= row ; ++ k ){
            int j = top ;
            for( ; j < bottom ; ++ j ){   //ai,j<=ai+1,j
            if( data[j][k] <= data[j+1][k] )
                continue ;
            else break ;
            }
            if( j >= bottom )
                result[k] = true ;
        }
        int j = 0 ;
        for( ;  j <= row ; ++ j )
            if( result[j] == true ){
                cout<<"Yes" ;
                break ;
            }
        if( result[j-1] != true )
            cout<<"No" ;
    }
#endif // 2_H_INCLUDED