#include <vector>
#include <iostream>
#include <algorithm>
#include <set>
#include <time.h>
#include <map>
#include <math.h>
#include <string>
#define lol long long
#define MOD 1000000007
using namespace std;

int n;
pair<int,int> a[123456];
int b[123456];
int t[123456];

int main()
{

    ios_base::sync_with_stdio(0);
    cin>>n;
    for (int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        a[i]=make_pair(x-1,i);
    }
    sort(a,a+n);
    for (int i=0;i<n;i++)
        t[a[i].second]=i;
    for (int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        b[i]=t[x-1];
    }
    multiset<int> s1,s2;
    for (int i=0;i<n;i++)
    {
        if (b[i]<i)
            s2.insert(i-b[i]);
        else
            s1.insert(b[i]-i);
    }
    int mv=0;
    for (int i=0;i<n;i++)
    {
        cout<<min((s1.begin()!=s1.end() ? (*s1.begin())-mv:1078998787),s2.begin()!=s2.end() ? (*s2.begin())+mv:1234568799)<<endl;
        if (b[n-1-i]<n-1-i)
        {
            auto q=s2.find(n-1-i-b[n-1-i]);
            auto y=q;
            y++;
            s2.erase(q,y);
        }
        else
        {
            auto q=s2.find(n-1-i-b[n-1-i]);
            auto y=q;
            y++;
            s2.erase(q,y);
        }
         mv++;
         s1.insert(b[n-1-i]+mv);
        if(s1.find(mv)!=s1.end())
        {
            auto qq=s1.find(mv);
            while(qq!=s1.end() && *qq==mv)
            {
                s2.insert(-mv);
                qq++;
            }
            s1.erase(mv);
        }

    }

    return 0;
}


#include <vector>
#include <iostream>
#include <algorithm>
#include <set>
#include <time.h>
#include <map>
#include <math.h>
#include <string>
#define lol long long
#define MOD 1000000007
using namespace std;

int n;
pair<int,int> a[123456];
int b[123456];
int t[123456];

int main()
{

    ios_base::sync_with_stdio(0);
    cin>>n;
    for (int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        a[i]=make_pair(x-1,i);
    }
    sort(a,a+n);
    for (int i=0;i<n;i++)
        t[a[i].second]=i;
    for (int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        b[i]=t[x-1];
    }
    multiset<int> s1,s2;
    for (int i=0;i<n;i++)
    {
        if (b[i]<=i)
            s2.insert(i-b[i]);
        else
            s1.insert(b[i]-i);
    }
    int mv=0;
    for (int i=0;i<n;i++)
    {
        cout<<min((s1.begin()!=s1.end() ? (*s1.begin())-mv:1078998787),s2.begin()!=s2.end() ? (*s2.begin())+mv:1234568799)<<endl;
        if (b[n-1-i]<n-1-i)
        {
            auto q=s2.find(n-1-i-b[n-1-i]);
            auto y=q;
            y++;
            s2.erase(q,y);
        }
        else
        {
            auto q=s2.find(n-1-i-b[n-1-i]);
            auto y=q;
            y++;
            s2.erase(q,y);
        }
         mv++;
         s1.insert(b[n-1-i]+mv);
        if(s1.find(mv)!=s1.end())
        {
            auto qq=s1.find(mv);
            while(qq!=s1.end() && *qq==mv)
            {
                s2.insert(-mv);
                qq++;
            }
            s1.erase(mv);
        }

    }

    return 0;
}


#include <vector>
#include <iostream>
#include <algorithm>
#include <set>
#include <time.h>
#include <map>
#include <math.h>
#include <string>
#define lol long long
#define MOD 1000000007
using namespace std;

int n;
pair<int,int> a[123456];
int b[123456];
int t[123456];
vector<int> ans;
int main()
{

    ios_base::sync_with_stdio(0);
    cin>>n;
    for (int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        t[x-1]=i;
        //a[i]=make_pair(x-1,i);
    }
    //sort(a,a+n);
    //for (int i=0;i<n;i++)
      //  t[a[i].second]=i;
    for (int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        b[i]=t[x-1];
    }
    multiset<int> s1,s2;
    for (int i=0;i<n;i++)
    {
        if (b[i]<=i)
            s2.insert(i-b[i]);
        else
            s1.insert(b[i]-i);
    }
    int mv=0;
    for (int i=0;i<n;i++)
    {
        ans.push_back(min((s1.begin()!=s1.end() ? (*s1.begin())-mv:1078998787),s2.begin()!=s2.end() ? (*s2.begin())+mv:1234568799));
        if (b[n-1-i]<n-1-i)
        {
            auto q=s2.find(n-1-i-b[n-1-i]);
            auto y=q;
            y++;
            s2.erase(q,y);
        }
        else
        {
            auto q=s2.find(n-1-i-b[n-1-i]);
            auto y=q;
            y++;
            s2.erase(q,y);
        }
         mv++;
         s1.insert(b[n-1-i]+mv);
        if(s1.find(mv)!=s1.end())
        {
            auto qq=s1.find(mv);
            while(qq!=s1.end() && *qq==mv)
            {
                s2.insert(-mv);
                qq++;
            }
            s1.erase(mv);
        }

    }
    cout<<ans[0]<<endl;
    for (int i=n-1;i>=1;i--)
        cout<<ans[i]<<endl;
    return 0;
}


#include <vector>
#include <iostream>
#include <algorithm>
#include <set>
#include <time.h>
#include <map>
#include <math.h>
#include <string>
#define lol long long
#define MOD 1000000007
using namespace std;

int n;
pair<int,int> a[123456];
int b[123456];
int t[123456];
vector<int> ans;
int main()
{

    ios_base::sync_with_stdio(0);
    cin>>n;
    for (int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        t[x-1]=i;
        //a[i]=make_pair(x-1,i);
    }
    //sort(a,a+n);
    //for (int i=0;i<n;i++)
      //  t[a[i].second]=i;
    for (int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        b[i]=t[x-1];
    }
    multiset<int> s1,s2;
    for (int i=0;i<n;i++)
    {
        if (b[i]<=i)
            s2.insert(i-b[i]);
        else
            s1.insert(b[i]-i);
    }
    int mv=0;
    for (int i=0;i<n;i++)
    {
        ans.push_back(min((s1.begin()!=s1.end() ? (*s1.begin())-mv:1078998787),s2.begin()!=s2.end() ? (*s2.begin())+mv:1234568799));
        if (b[n-1-i]<n-1-i)
        {
            auto q=s2.find(n-1-i-b[n-1-i]);
            auto y=q;
            y++;
            s2.erase(q,y);
        }
        else
        {
            auto q=s2.find(n-1-i-b[n-1-i]);
            auto y=q;
            y++;
            s2.erase(q,y);
        }
         mv++;
         s1.insert(b[n-1-i]+mv);
        if(s1.find(mv)!=s1.end())
        {
            auto qq=s1.find(mv);
            while(qq!=s1.end() && *qq==mv)
            {
                s2.insert(-mv);
                qq++;
            }
            s1.erase(mv);
        }

    }
    cout<<endl<<ans[0]<<endl;
    for (int i=n-1;i>=1;i--)
        cout<<ans[i]<<"   ";
    return 0;
}


#include<stdio.h>
#include<map>
#include<algorithm>
#include<stdlib.h>
using namespace std;
int n,a[100010],b[100010],_b[100010];

map<int,int> Map;

int main(){
	int x;
	scanf("%d",&n);
	for(int i=0; i<n; i++){
		scanf("%d",&x);--x;
		a[x]=i;
	}
	for(int i=0; i<n; i++){
		scanf("%d",&x);--x;
		b[x]=i;
		_b[i]=x;
	}
	for(int i=0; i<n; i++)
		Map[b[i]-a[i]]++;
	for(int t=0; t<n; t++){
		map<int,int>::iterator it=Map.lower_bound(t);
		int res=n;
		if(it!=Map.end()){
			res=min(res,(int)labs(it->first-t));
		}
		if(it!=Map.begin()){
			--it;
			res=min(res,(int)labs(it->first-t));
		}
		printf("%d\n",res);
		x=_b[t];
		int num=b[x]-a[x];
		if(--Map[num]==0)Map.erase(num);
		Map[num+n]++;
	}
	return 0;
}


#include<stdio.h>
#include<map>
#include<algorithm>
#include<stdlib.h>
using namespace std;
int n,a[100010],b[100010],_b[100010];

map<int,int> Map;

int main(){
	int x;
	scanf("%d",&n);
	for(int i=0; i<n; i++){
		scanf("%d",&x);--x;
		a[x]=i;
	}
	for(int i=0; i<n; i++){
		scanf("%d",&x);--x;
		b[x]=i;
		_b[i]=x;
	}
	for(int i=0; i<n; i++)
		Map[b[i]-a[i]]++;
	for(int t=0; t<n; t++){
		map<int,int>::iterator it=Map.lower_bound(-t);
		int res=n;
		if(it!=Map.end()){
			res=min(res,(int)labs(it->first+t));
		}
		if(it!=Map.begin()){
			--it;
			res=min(res,(int)labs(it->first+t));
		}
		printf("%d\n",res);
		x=_b[n-1-t];
		int num=b[x]-a[x];
		if(--Map[num]==0)Map.erase(num);
		Map[num-n]++;
	}
	return 0;
}


#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

#define ll long long
#define ls rt<<1
#define rs ls|1
#define lson l,mid,ls
#define rson mid+1,r,rs
#define middle (l+r)>>1
#define eps (1e-9)
#define type int
#define clr_all(x,c) memset(x,c,sizeof(x))
#define clr(x,c,n) memset(x,c,sizeof(x[0])*(n+1))
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)

template <class T> void _swap(T &x,T &y){T t=x;x=y;y=t;}
template <class T> T _max(T x,T y){return x>y? x:y;}
template <class T> T _min(T x,T y){return x<y? x:y;}
int test,cas;
const int M=100000 +5;

int n,m;
int ap[M],b[M],cur[M];

struct node{
    int id,cur,num;
    node(){}
    node(int _i,int _c,int _n){id=_i,cur=_c,num=_n;}
    bool operator < (const node& t) const{
        return num > t.num;
    }
};
priority_queue<node>rgt,lft;
int covr,covl;

void run(){
    int i,j;
    for(i=1;i<=n;i++){
        scanf("%d",&j);
        ap[j]=i;
    }
    while(!rgt.empty()) rgt.pop();
    while(!lft.empty()) lft.pop();
    clr(cur,0,n+1);
    for(i=1;i<=n;i++){
        scanf("%d",&b[i]);
        if(i>ap[b[i]]) rgt.push(node(b[i],0,i-ap[b[i]]));
        else lft.push(node(b[i],0,ap[b[i]]-i));
    }
    covr=covl=0;
    node r,l;
    for(i=1;;i++){
        while(!rgt.empty()){
            r=rgt.top();
            if(r.cur!=cur[r.id]) rgt.pop();
            else if(r.num+covr==0){
                rgt.pop();
                lft.push(node(r.id,r.cur,-covl));
            }else break;
        }
        while(!lft.empty()){
            l=lft.top();
            if(l.cur!=cur[l.id]) lft.pop();
            else break;
        }
        int ans=inf;
        if(!rgt.empty()) ans=_min(ans,rgt.top().num+covr);
        if(!lft.empty()) ans=_min(ans,lft.top().num+covl);
        printf("%d\n",ans);
        if(i==n) break;
        covr--,covl++;
        cur[b[i]]++;
        if(n>ap[b[i]]) rgt.push(node(b[i],cur[b[i]],n-ap[b[i]]-covr));
        else lft.push(node(b[i],cur[b[i]],-covl));
    }
}

void preSof(){
}

int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    preSof();
    //run();
    while(~scanf("%d",&n)) run();
    //for(scanf("%d",&test),cas=1;cas<=test;cas++) run();
    return 0;
}


#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

#define ll long long
#define ls rt<<1
#define rs ls|1
#define lson l,mid,ls
#define rson mid+1,r,rs
#define middle (l+r)>>1
#define eps (1e-9)
#define type int
#define clr_all(x,c) memset(x,c,sizeof(x))
#define clr(x,c,n) memset(x,c,sizeof(x[0])*(n+1))
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)

template <class T> void _swap(T &x,T &y){T t=x;x=y;y=t;}
template <class T> T _max(T x,T y){return x>y? x:y;}
template <class T> T _min(T x,T y){return x<y? x:y;}
int test,cas;
const int M=100000 +5;

int n,m;
int ap[M],b[M],cur[M];

struct node{
    int id,cur,num;
    node(){}
    node(int _i,int _c,int _n){id=_i,cur=_c,num=_n;}
    bool operator < (const node& t) const{
        return num > t.num;
    }
};
priority_queue<node>rgt,lft;
int covr,covl;

void show(priority_queue<node>q,int c){
    while(!q.empty()){
        node t=q.top();
        printf("%d %d %d\n",t.id,t.cur,t.num+c);
        q.pop();
    }
}

void run(){
    int i,j;
    for(i=1;i<=n;i++){
        scanf("%d",&j);
        ap[j]=i;
    }
    while(!rgt.empty()) rgt.pop();
    while(!lft.empty()) lft.pop();
    clr(cur,0,n+1);
    for(i=1;i<=n;i++){
        scanf("%d",&b[i]);
        if(i>ap[b[i]]) rgt.push(node(b[i],0,i-ap[b[i]]));
        else lft.push(node(b[i],0,ap[b[i]]-i));
    }
    covr=covl=0;
    node r,l;
    for(i=1;;i++){
        while(!rgt.empty()){
            r=rgt.top();
            if(r.cur!=cur[r.id]) rgt.pop();
            else if(r.num+covr==0){
                rgt.pop();
                lft.push(node(r.id,r.cur,-covl));
            }else break;
        }
        while(!lft.empty()){
            l=lft.top();
            if(l.cur!=cur[l.id]) lft.pop();
            else break;
        }
        //show(rgt,covr);show(lft,covl);
        int ans=inf;
        if(!rgt.empty()) ans=_min(ans,rgt.top().num+covr);
        if(!lft.empty()) ans=_min(ans,lft.top().num+covl);
        printf("%d\n",ans);
        if(i==n) break;
        covr--,covl++;
        cur[b[i]]++;
        if(n>ap[b[i]]) rgt.push(node(b[i],cur[b[i]],n-ap[b[i]]-covr));
        else lft.push(node(b[i],cur[b[i]],-covl));
    }
}

void preSof(){
}

int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    preSof();
    //run();
    while(~scanf("%d",&n)) run();
    //for(scanf("%d",&test),cas=1;cas<=test;cas++) run();
    return 0;
}


#include <stdio.h>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <queue>
#include <math.h>
#define N 100100
using namespace std;
int a[N], pa[N], b[N], pb[N], r[N];
struct E
{
    int i, t, v, j;
    E(int i=0, int t=0, int v=0, int j=0): i(i), t(t), v(v), j(j) {}
};
bool operator <(E a, E b) { return a.v<b.v || (a.v==b.v && a.t<b.t); }
struct T
{
    int i, v;
    T(int i=0, int v=0): i(i), v(v) {}
};
bool operator <(T a, T b) { return a.v>b.v || (a.v==b.v && a.i<b.i); }
E e[4*N];
set <T> s;
int n;
void sol()
{
    int i, j, k;
    for(i=0; i<n; pa[a[i]]=i, pb[b[i]]=i, i++);
    for(k=0, i=0; i<n; i++)
    {
        j=(pb[i]-pa[i]+n)%n;
        e[k++]=E(i, 0, j);
        e[k++]=E(0, 1, i);
        e[k++]=E(0, 1, i+n);
        if(j<=pb[i]) e[k++]=E(i, 2, pb[i], j);
        else e[k++]=E(i, 2, pb[i]+n, j);
    }
    sort(e, e+k);
    s.clear();
    for(i=0; i<k; i++)
        if(e[i].t==0) s.insert(T(e[i].i, e[i].v));
        else if(e[i].t==2) s.erase(T(e[i].i, e[i].j));
        else if(e[i].t==1 && !s.empty()) r[e[i].v%n]=min(r[e[i].v%n], e[i].v-s.begin()->v);
}
int main()
{
    int i;
    for(scanf("%d", &n), i=0; i<n; scanf("%d", &a[i]), a[i]--, r[i]=n, i++);
    for(i=0; i<n; scanf("%d", &b[i]), b[i]--, i++);
    sol();
    reverse(a, a+n);
    reverse(b, b+n);
    reverse(r+1, r+n);
    sol();
    reverse(r+1, r+n);
    for(i=0; i<n; printf("%d\n", r[i]), i++);
    return 0;
}


#include <stdio.h>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <queue>
#include <math.h>
#define N 100100
using namespace std;
int a[N], pa[N], b[N], pb[N], u[N];
int main()
{
    int i, j, k, n;
    for(scanf("%d", &n), i=0; i<n; scanf("%d", &a[i]), a[i]--, pa[a[i]]=i, i++);
    for(i=0; i<n; scanf("%d", &b[i]), b[i]--, pb[b[i]]=i, i++);
    for(i=0; i<n; u[(pb[i]-pa[i]+n)%n]=1, i++);
    for(i=0; i<n && !u[i]; i++);
    for(; i<n; i+=j)
    {
        for(j=1; !u[(i+j)%n]; j++);
        for(k=0; k<=j; a[(i+k)%n]=min(k, j-k), k++);
    }
    for(i=0; i<n; printf("%d\n", a[i]), i++);
    return 0;
}


#include <stdio.h>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <queue>
#include <math.h>
#define N 100100
using namespace std;
int a[N], pa[N], b[N], pb[N], u[N], v[N];
int main()
{
    int i, j, k, n;
    for(scanf("%d", &n), i=0; i<n; scanf("%d", &a[i]), a[i]--, pa[a[i]]=i, i++);
    for(i=0; i<n; scanf("%d", &b[i]), b[i]--, pb[b[i]]=i, i++);
    for(i=0; i<n; u[(pb[i]-pa[i]+n)%n]=1, i++);
    for(i=0; i<n; a[i]=n, i++);
    for(i=0; i<n && !u[i]; i++);
    for(; i<n; i+=j)
    {
        for(j=1; !u[(i+j)%n]; j++);
        for(k=0; k<=i && k<=j; a[(i+k)%n]=min(a[(i+k)%n], k), k++);
        for(k=j; k>=0 && j-k<=n-1-i; a[(i+k)%n]=min(a[(i+k)%n], j-k), k--);
    }
    for(i=0; i<n; printf("%d\n", a[i]), i++);
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <cassert>
#include <cstdio>
#include <map>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <memory.h>
#include <queue>
#include <stack>
using namespace std;
int cnt[256];
typedef long long li;
typedef pair<int,int> pi;
void solve();
int main(){
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    int t = 1;
    //cin>>t;

    while(t--)
        solve();
    return 0;
}

int a[101010];
int b[101010];
int wa[101010], wb[101010];
void solve(){

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

    for(int i =0; i < n ;++i){
        cin>>b[i];
        --b[i];
        wb[b[i]]=i;
    }
    map<int, int> r;
    for(int i = 0; i < n ;++i){
        r[wb[i] - wa[i]]++;
    }

    for(int i = 0; i < n; ++i){
        map<int,int>::iterator it = r.lower_bound(i);

        /*for(multiset<int>::iterator j = r.begin(); j!= r.end(); ++j){
            cout<<*j + i<<' ';
        }
        cout<<endl;*/
        int best = 1000000000;
        if(it!=r.end())
            best = min(best, abs((it->first) - i));
        if(it!=r.begin()){
            --it;
            best = min(best, abs((it->first) - i));
        }
        assert(best != 1000000000);
        cout<<best<<'\n';
        int perehod = b[i];
        r[wb[perehod] - wa[perehod]] --;
        if(r[wb[perehod] - wa[perehod]]==0){
            r.erase(wb[perehod] - wa[perehod]);
        }
        r[wb[perehod] - wa[perehod] + n] ++;
    }
}


#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <cassert>
#include <cstdio>
#include <map>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <memory.h>
#include <queue>
#include <stack>
using namespace std;
int cnt[256];
typedef long long li;
typedef pair<int,int> pi;
void solve();
int main(){
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    int t = 1;
    //cin>>t;

    while(t--)
        solve();
    return 0;
}

int a[101010];
int b[101010];
int wa[101010], wb[101010];
void solve(){

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

    for(int i =0; i < n ;++i){
        cin>>b[i];
        --b[i];
        wb[b[i]]=i;
    }
    map<int, int> r;
    for(int i = 0; i < n ;++i){
        r[wb[i] - wa[i]]++;
    }

    for(int i = 0; i < n; ++i){
        map<int,int>::iterator it = r.lower_bound(-i);

        /*for(multiset<int>::iterator j = r.begin(); j!= r.end(); ++j){
            cout<<*j + i<<' ';
        }
        cout<<endl;*/
        int best = 1000000000;
        if(it!=r.end())
            best = min(best, abs((it->first) + i));
        if(it!=r.begin()){
            --it;
            best = min(best, abs((it->first) + i));
        }
        assert(best != 1000000000);
        cout<<best<<'\n';
        int perehod = b[n - 1 - i];
        r[wb[perehod] - wa[perehod]] --;
        if(r[wb[perehod] - wa[perehod]]==0){
            r.erase(wb[perehod] - wa[perehod]);
        }
        r[wb[perehod] - wa[perehod] - n] ++;
    }
}


#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <cassert>
#include <cstdio>
#include <map>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <memory.h>
#include <queue>
#include <stack>
using namespace std;
int cnt[256];
typedef long long li;
typedef pair<int,int> pi;
void solve();
int main(){
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    int t = 1;
    //cin>>t;

    while(t--)
        solve();
    return 0;
}

int a[101010];
int b[101010];
int wa[101010], wb[101010];
void solve(){

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

    for(int i =0; i < n ;++i){
        cin>>b[i];
        --b[i];
        wb[b[i]]=i;
    }
    multiset<int> r;
    for(int i = 0; i < n ;++i){
        r.insert(wb[i] - wa[i]);
    }

    for(int i = 0; i < n; ++i){
        assert(r.size() == n);
        multiset<int>::iterator it = r.lower_bound(-i);

        /*for(multiset<int>::iterator j = r.begin(); j!= r.end(); ++j){
            cout<<*j + i<<' ';
        }
        cout<<endl;*/
        int best = 1000000000;
        if(it!=r.end())
            best = min(best, abs((*it) + i));
        if(it!=r.begin()){
            --it;
            best = min(best, abs((*it) + i));
        }
        assert(best != 1000000000);
        cout<<best<<'\n';
        int perehod = b[n - 1 - i];
        r.erase(r.find(wb[perehod] - wa[perehod]));
        r.insert(wb[perehod] - wa[perehod] - n);
    }
}


#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <cassert>
#include <cstdio>
#include <map>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <memory.h>
#include <queue>
#include <stack>
using namespace std;
int cnt[256];
typedef long long li;
typedef pair<int,int> pi;
void solve();
int main(){
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    int t = 1;
    //cin>>t;

    while(t--)
        solve();
    return 0;
}

int a[101010];
int b[101010];
int wa[101010], wb[101010];
void solve(){

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

    for(int i =0; i < n ;++i){
        cin>>b[i];
        --b[i];
        wb[b[i]]=i;
    }
    map<int, int> r;
    for(int i = 0; i < n ;++i){
        r[wb[i] - wa[i]]++;
    }

    for(int i = 0; i < n; ++i){
        map<int,int>::iterator it = r.lower_bound(-i);

        /*for(multiset<int>::iterator j = r.begin(); j!= r.end(); ++j){
            cout<<*j + i<<' ';
        }
        cout<<endl;*/
        int best = 1000000000;
        if(it!=r.end())
            best = min(best, abs((it->first) + i));
        if(it!=r.begin()){
            --it;
            best = min(best, abs((it->first) + i));
        }
        assert(best != 1000000000);
        cout<<best<<'\n';
        int perehod = b[i];
        r[wb[perehod] - wa[perehod]] --;
        if(r[wb[perehod] - wa[perehod]]==0){
            r.erase(wb[perehod] - wa[perehod]);
        }
        r[wb[perehod] - wa[perehod] + n] ++;
    }
}


#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iostream>
#include <iterator>
#include <sstream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <numeric>
#include <memory.h>
#include <iomanip>

using namespace std;

#define FOR(i,a,b) for (int _n(b), i(a); i < _n; i++)
#define REP(i,n) FOR(i,0,n)
#define pb push_back
#define sz size()

#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define UNIQUE(c) SORT((c)), (c).erase(unique(ALL((c))), (c).end())
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define MP(a,b)  make_pair((a), (b))
#define SQR(a) ((a)*(a))
#define X first
#define Y second

typedef pair<int,int> ii;
typedef vector<int > vi;
typedef vector<vi > vvi;
typedef vector<ii  > vii;
typedef vector<vii  > vvii;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;

ll gcd(ll a, ll b){return a == 0 ? b : gcd(b%a, a);}

const string filename = "test";
const string str_fin = filename + ".in";
const string str_fout = filename + ".out";

const ii kEmpty(1000000, -1);
vii v_up, v_down;

void insertDown(int index, const ii& down) {
  if (index >= v_down.size())
    return;
  if (down.second <= 0)
    return;
  if (v_down[index] == kEmpty) {
    v_down[index] = down;
  } else {
    const ii cdown = v_down[index];
    if (cdown.first <= down.first) {
      if (cdown.second >= down.second) {
      } else {
        insertDown(index + cdown.second, ii(down.first - cdown.second, down.second - cdown.second));
      }
    } else {
      if (down.second >= cdown.second) {
        v_down[index] = down;
      } else {
        insertDown(index + down.second, ii(cdown.first - down.second, cdown.second - down.second));
        v_down[index] = down;
      }
    }
  }
}

void insertUp(int index, const ii& up) {
  if (index >= v_up.size())
    return;
  if (up.second <= 0)
    return;
  if (v_up[index] == kEmpty) {
    v_up[index] = up;
  } else {
    const ii cup = v_up[index];
    if (cup.first <= up.first) {
      if (cup.second >= up.second) {
      } else {
        insertUp(index + cup.second, ii(up.first + cup.second, up.second - cup.second));
      }
    } else {
      if (up.second >= cup.second) {
        v_up[index] = up;
      } else {
        insertUp(index + up.second, ii(cup.first + up.second, cup.second - up.second));
        v_up[index] = up;
      }
    }
  }
}

int main() {
  //freopen(str_fin.c_str(), "r", stdin);
  //freopen(str_fout.c_str(), "w", stdout);

  int n, x;
  scanf("%d", &n);
  vi va, vb;
  va.reserve(n);
  vb.reserve(n);
  v_up = vii(n, kEmpty);
  v_down = vii(n, kEmpty);

  vi vpos_a(n + 1, -1), vpos_b(n + 1, -1);

  REP(i, n) {
    scanf("%d", &x);
    va.pb(x);
    vpos_a[x] = i;
  }
  REP(i, n) {
    scanf("%d", &x);
    vb.pb(x);
    vpos_b[x] = i;
  }

  FOR(i, 1, n + 1) {
    int pa = vpos_a[i], pb = vpos_b[i];
    if (pb == pa ) {
      insertUp(0, ii(0, n - pa));
      insertDown(n - pa, ii(pa, pa));
    } else if (pb > pa) {
      insertUp(0, ii(pb - pa, n - pb));
      insertDown(n - pb, ii(pa, pa));
      insertUp(n - pb + pa, ii(0, pb - pa));
    } else {
      insertDown(0, ii(pa - pb, pa - pb));
      insertUp(pa - pb, ii(0, n - pa));
      insertDown(n - pb, ii(pa, pb));
    }
  }

  vi res;
  REP(i, n) {
    res.pb(MIN(v_up[i].first, v_down[i].first));
    if (v_up[i] != kEmpty) {
      insertUp(i + 1, ii(v_up[i].first + 1, v_up[i].second - 1));
    }
    if (v_down[i] != kEmpty) {
      insertDown(i + 1, ii(v_down[i].first - 1, v_down[i].second - 1));
    }
  }
  reverse(res.begin() + 1, res.end());
  REP(i, res.sz) {
    printf("%d\n", res[i]);
  }

  return 0;
}


#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iostream>
#include <iterator>
#include <sstream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <numeric>
#include <memory.h>
#include <iomanip>

using namespace std;

#define FOR(i,a,b) for (int _n(b), i(a); i < _n; i++)
#define REP(i,n) FOR(i,0,n)
#define pb push_back
#define sz size()

#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define UNIQUE(c) SORT((c)), (c).erase(unique(ALL((c))), (c).end())
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define MP(a,b)	 make_pair((a), (b))
#define SQR(a) ((a)*(a))
#define X first
#define Y second

typedef pair<int,int> ii;
typedef vector<int > vi;
typedef vector<vi > vvi;
typedef vector<ii  > vii;
typedef vector<vii  > vvii;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;

ll gcd(ll a, ll b){return a == 0 ? b : gcd(b%a, a);}

const string filename = "test";
const string str_fin = filename + ".in";
const string str_fout = filename + ".out";

vvii v_up, v_down;

//if (v_down[index] == kEmpty) {
//    v_down[index] = down;
//  } else {
//    const ii cdown = v_down[index];
//    if (cdown.first <= down.first) {
//      if (cdown.second >= down.second) {
//      } else {
//        insertDown(index + cdown.second, ii(down.first - cdown.second, down.second - cdown.second));
//      }
//    } else {
//      if (down.second >= cdown.second) {
//        v_down[index] = down;
//      } else {
//        insertDown(index + down.second, ii(cdown.first - down.second, cdown.second - down.second));
//        v_down[index] = down;
//      }
//    }
//  }

//if (v_up[index] == kEmpty) {
//    v_up[index] = up;
//  } else {
//    const ii cup = v_up[index];
//    if (cup.first <= up.first) {
//      if (cup.second >= up.second) {
//      } else {
//        insertUp(index + cup.second, ii(up.first + cup.second, up.second - cup.second));
//      }
//    } else {
//      if (up.second >= cup.second) {
//        v_up[index] = up;
//      } else {
//        insertUp(index + up.second, ii(cup.first + up.second, cup.second - up.second));
//        v_up[index] = up;
//      }
//    }
//  }

void insertDown(int index, const ii& down) {
  if (index >= v_down.size())
    return;
  if (down.second <= 0)
    return;
  v_down[index].pb(down);
}

void insertUp(int index, const ii& up) {
  if (index >= v_up.size())
    return;
  if (up.second <= 0)
    return;
  v_up[index].pb(up);
}

bool comp(const ii& l, const ii& r) {
  if (l.first < r.first)
    return true;
  if (l.second > r.second)
    return false;
  return l.second > r.second;
}

void processV(vii& v) {
  if (v.empty()) return;
  sort(ALL(v), comp);
  int wr = 1;
  int cmp = 0;
  FOR(rd, 1, v.sz) {
    if (v[rd].first == v[cmp].first || v[rd].second <= v[cmp].second) {
    } else {
      if (wr != rd) {
        v[wr] = v[rd];
      }
      cmp = wr;
      wr++;
    }
  }
  v.resize(wr);
}

int process(int index) {
  vii& vu = v_up[index];
  vii& vd = v_down[index];

  processV(vu);
  processV(vd);

  int res = vu.empty() ? 1000000 : vu[0].first;
  if (!vd.empty()) {
    res = MIN(res, vd[0].first);
  }

  if (index + 1 < v_up.sz) {
    vii& n_vu = v_up[index + 1];
    vii& n_vd = v_down[index + 1];
    REP(i, vu.sz) {
      if (vu[i].second > 1) {
        n_vu.push_back(ii(vu[i].first + 1, vu[i].second - 1));
      }
    }
    REP(i, vd.sz) {
      if (vd[i].second > 1) {
        n_vd.push_back(ii(vd[i].first - 1, vd[i].second - 1));
      }
    }
  }

  return res;
}

int main() {
  //freopen(str_fin.c_str(), "r", stdin);
  //freopen(str_fout.c_str(), "w", stdout);

  int n, x;
  scanf("%d", &n);
  vi va, vb;
  va.reserve(n);
  vb.reserve(n);
  v_up = vvii(n);
  v_down = vvii(n);

  vi vpos_a(n + 1, -1), vpos_b(n + 1, -1);

  REP(i, n) {
    scanf("%d", &x);
    va.pb(x);
    vpos_a[x] = i;
  }
  REP(i, n) {
    scanf("%d", &x);
    vb.pb(x);
    vpos_b[x] = i;
  }

  FOR(i, 1, n + 1) {
    int pa = vpos_a[i], pb = vpos_b[i];
    if (pb == pa ) {
      insertUp(0, ii(0, n - pa));
      insertDown(n - pa, ii(pa, pa));
    } else if (pb > pa) {
      insertUp(0, ii(pb - pa, n - pb));
      insertDown(n - pb, ii(pa, pa));
      insertUp(n - pb + pa, ii(0, pb - pa));
    } else {
      insertDown(0, ii(pa - pb, pa - pb));
      insertUp(pa - pb, ii(0, n - pa));
      insertDown(n - pb, ii(pa, pb));
    }
  }

  vi res;
  REP(i, n) {
    res.pb(process(i));
  }

  reverse(res.begin() + 1, res.end());
  REP(i, res.sz) {
    printf("%d\n", res[i]);
  }

  return 0;
}


#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iostream>
#include <iterator>
#include <sstream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <numeric>
#include <memory.h>
#include <iomanip>

using namespace std;

#define FOR(i,a,b) for (int _n(b), i(a); i < _n; i++)
#define REP(i,n) FOR(i,0,n)
#define pb push_back
#define sz size()

#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define UNIQUE(c) SORT((c)), (c).erase(unique(ALL((c))), (c).end())
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define MP(a,b)  make_pair((a), (b))
#define SQR(a) ((a)*(a))
#define X first
#define Y second

typedef pair<int,int> ii;
typedef vector<int > vi;
typedef vector<vi > vvi;
typedef vector<ii  > vii;
typedef vector<vii  > vvii;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;

ll gcd(ll a, ll b){return a == 0 ? b : gcd(b%a, a);}

const string filename = "test";
const string str_fin = filename + ".in";
const string str_fout = filename + ".out";

const ii kEmpty(1000000, -1);
vii v_up, v_down;

void insertDown(int index, const ii& down) {
  if (index >= v_down.size())
    return;
  if (down.second <= 0)
    return;
  if (v_down[index] == kEmpty) {
    v_down[index] = down;
  } else {
    const ii cdown = v_down[index];
    if (cdown.first <= down.first) {
      if (cdown.second >= down.second) {
      } else {
        insertDown(index + cdown.second, ii(down.first - cdown.second, down.second - cdown.second));
      }
    } else {
      if (down.second >= cdown.second) {
        v_down[index] = down;
      } else {
        insertDown(index + down.second, ii(cdown.first - down.second, cdown.second - down.second));
        v_down[index] = down;
      }
    }
  }
}

void insertUp(int index, const ii& up) {
  if (index >= v_up.size())
    return;
  if (up.second <= 0)
    return;
  if (v_up[index] == kEmpty) {
    v_up[index] = up;
  } else {
    const ii cup = v_up[index];
    if (cup.first <= up.first) {
      if (cup.second >= up.second) {
      } else {
        insertUp(index + cup.second, ii(up.first + cup.second, up.second - cup.second));
      }
    } else {
      if (up.second >= cup.second) {
        v_up[index] = up;
      } else {
        insertUp(index + up.second, ii(cup.first + up.second, cup.second - up.second));
        v_up[index] = up;
      }
    }
  }
}

int main() {
  //freopen(str_fin.c_str(), "r", stdin);
  //freopen(str_fout.c_str(), "w", stdout);

  int n, x;
  scanf("%d", &n);
  vi va, vb;
  va.reserve(n);
  vb.reserve(n);
  v_up = vii(n, kEmpty);
  v_down = vii(n, kEmpty);

  vi vpos_a(n + 1, -1), vpos_b(n + 1, -1);

  REP(i, n) {
    scanf("%d", &x);
    va.pb(x);
    vpos_a[x] = i;
  }
  REP(i, n) {
    scanf("%d", &x);
    vb.pb(x);
    vpos_b[x] = i;
  }

  FOR(i, 1, n + 1) {
    int pa = vpos_a[i], pb = vpos_b[i];
    if (pb == pa ) {
      insertUp(0, ii(0, n - pa));
      insertDown(n - pa, ii(pa, pa));
    } else if (pb > pa) {
      insertUp(0, ii(pb - pa, n - pb));
      insertDown(n - pb, ii(pa, pa));
      insertUp(n - pb + pa, ii(0, pb - pa));
    } else {
      insertDown(0, ii(pa - pb, pa - pb));
      insertUp(pa - pb, ii(0, n - pa));
      insertDown(n - pb, ii(pa, pb));
    }
  }

  vi res;
  REP(i, n) {
    res.pb(MIN(v_up[i].first, v_down[i].first));
    if (v_up[i] != kEmpty) {
      insertUp(i + 1, ii(v_up[i].first + 1, v_up[i].second - 1));
    }
    if (v_down[i] != kEmpty) {
      insertDown(i + 1, ii(v_down[i].first - 1, v_down[i].second - 1));
    }
  }
  reverse(ALL(res));
  REP(i, res.sz) {
    printf("%d\n", res[i]);
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
struct node{
	int p,t;
};
const int stra=1<<21;
node tree[stra*2];
int a[300100],b[300100];
void upd(int pos){
	tree[pos].t=max(tree[pos*2].t,tree[pos*2+1].t)+tree[pos].p;
	return;
}
void modi(int pos,int l,int r,int v,int tl,int tr){
	if(l==tl && r==tr){
		tree[pos].p+=v;
		tree[pos].t+=v;
		return;
	}
	int md=(tl+tr)>>1;
	if(r<=md){
		modi(pos*2,l,r,v,tl,md);
		upd(pos);
	}
	else if(l>md){
		modi(pos*2+1,l,r,v,md+1,tr);
		upd(pos);
	}
	else{
		modi(pos*2,l,md,v,tl,md);
		modi(pos*2+1,md+1,r,v,md+1,tr);
		upd(pos);
	}
	return;
}
int que(int pos=1,int val=0,int l=1,int r=1001000){
	if(val+tree[pos].t<0){
		return -1;
	}
	if(l==r) return l;
	int md=(l+r)>>1;
	val+=tree[pos].p;
	if(tree[pos*2+1].t+val>0){
		return que(pos*2+1,val,md+1,r);
	}
	return que(pos*2,val,l,md);
}
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		modi(1,1,a[i],1,1,1001000);
	}
	for(int i=1;i<=m;i++){
		scanf("%d",&b[i]);
		modi(1,1,b[i],-1,1,1001000);
	}
	int q;
	scanf("%d",&q);
	while(q--){
		int t,i,x;
		scanf("%d%d%d",&t,&i,&x);
		if(t==1){
			modi(1,1,a[i],-1,1,1001000);
			a[i]=x;
			modi(1,1,a[i],1,1,1001000);
		}
		else{
			modi(1,1,b[i],1,1,1001000);
			b[i]=x;
			modi(1,1,b[i],-1,1,1001000);
		}
		printf("%d\n",que());
	}
}


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

#define fastio ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define LL long long int
#define rep(i,n,z) for(int i=z;i<n;i++)
#define rrep(i,z) for(int i=z;i>=0;i--)
#define nl cout<<endl
#define vi vector<int>
#define vlli vector<long long int>
#define umap unordered_map
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
#define ipair pair <int,int>
#define llipair pair <lli,lli>
#define pq priority_queue
#define displaymatrix(a,m,n) for(int i=0;i<m;i++){for(int j=0;j<n;j++)cout<<a[i][j]<<" ";cout<<endl;}
#define printarray(a,n) for(int i=0;i<n;i++){cout<<a[i]<<" ";}nl;
#define vinput(a,n) vlli a(n);rep(i,n,0)cin>>a[i]
#define ainput(a,n) rep(i,n,0)cin>>a[i]
#define SO(a) sort(a.begin(),a.end())
#define SOP(a,comp) sort(a.begin(),a.end(),comp)
#define inf INT_MAX
#define endl '\n'

#define NN 1000006
struct dataz
{
	//Use required attributes
	int mn;

	//Default Values
	dataz() : mn(1e9) {};
};

struct SegTree
{
	int N;
	vector<dataz> st;
	vector<bool> cLazy;
	vector<int> lazy;

	void init(int n)
	{
		N = n;
		st.resize(4 * N + 5);
		cLazy.assign(4 * N + 5, false);
		lazy.assign(4 * N + 5, 0);
	}

	//Write reqd merge functions
	void merge(dataz &cur, dataz &l, dataz &r)
	{
		cur.mn = min(l.mn, r.mn);
	}

	//Handle lazy propagation appriopriately
	void propagate(int node, int L, int R)
	{
		if(L != R)
		{
			cLazy[node*2] = 1;
			cLazy[node*2 + 1] = 1;
			lazy[node*2] += lazy[node];
			lazy[node*2 + 1] += lazy[node];
		}
		st[node].mn += lazy[node];
		cLazy[node] = 0;
		lazy[node] = 0;
	}

	void build(int node, int L, int R)
	{
		if(L==R)
		{
			st[node].mn = 0;
			return;
		}
		int M=(L + R)/2;
		build(node*2, L, M);
		build(node*2 + 1, M + 1, R);
		merge(st[node], st[node*2], st[node*2+1]);
	}

	int pQuery(int node, int L, int R)
	{
		if(cLazy[node])
			propagate(node, L, R);
		if(L == R)
		{
			if(st[node].mn<0)
				return L;
			else
				return -1;
		}
		int M = (L + R)/2;
		if(cLazy[node*2+1])
			propagate(node*2+1, M+1, R);

		if(st[node*2+1].mn <0)
			return pQuery(node*2 + 1, M + 1, R);
		else
			return pQuery(node*2, L, M);
	}

	void Update(int node, int L, int R, int i, int j, int val)
	{
		if(cLazy[node])
			propagate(node, L, R);
		if(j<L || i>R)
			return;
		if(i<=L && R<=j)
		{
			cLazy[node] = 1;
			lazy[node] += val;
			propagate(node, L, R);
			return;
		}
		int M = (L + R)/2;
		Update(node*2, L, M, i, j, val);
		Update(node*2 + 1, M + 1, R, i, j, val);
		merge(st[node], st[node*2], st[node*2 + 1]);
	}

	void pr()
	{
		for(int i=0;i<=10;i++)
			cout<<i<<" "<<st[i].mn<<endl;
		cout<<endl;
	}
	int query(int l, int r)
	{
		return pQuery(1, 1, N);
	}

	void update(int l, int r, int val)
	{
		Update(1, 1, N, l, r, val);
	}
};

int main()
{
    LL i,j,lt,d,z,v,c,r,l,t1,k,h,n,u,m,q,ts,te,x,y;
    fastio;
    cin>>n>>m;
    SegTree tree;
    tree.init(NN);
    tree.build(1,1,NN);
    LL ar[n+1],br[m+1];
    for(i=1;i<=n;i++)
    {
    	cin>>ar[i];
    	tree.update(1,ar[i],-1);
    }
    for(i=1;i<=m;i++)
    {
    	cin>>br[i];
    	tree.update(1,br[i],1);
    }
    cin>>q;

    while(q--){
    	cin>>z>>x>>y;
    	if(z==1){
    		tree.update(1,ar[x],1);
    		ar[x]=y;
    		tree.update(1,ar[x],-1);
    	}
    	else{
    		tree.update(1,br[x],-1);
    		br[x]=y;
    		tree.update(1,br[x],1);
    	}

    	cout<<tree.query(1,NN-1)<<endl;

    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int lim=1000000;

int tree[5000000],lazy[5000000];
int cost[400000],buy[400000];
void update(int node,int st,int en,int l,int r,int v)
{
    if(lazy[node])
    {
        tree[node]+=lazy[node];
        if(st!=en)
        {
            lazy[node*2]+=lazy[node];
            lazy[node*2+1]+=lazy[node];
        }
        lazy[node]=0;
    }
    if(st>r or en<l)
        return ;
    if(st>=l and en<=r)
    {
        tree[node]+=v;
        if(st!=en)
        {
            lazy[node*2]+=v;
            lazy[node*2+1]+=v;
        }
        return ;
    }
    int mid=(st+en)>>1;
    update(node*2,st,mid,l,r,v);
    update(node*2+1,mid+1,en,l,r,v);
    tree[node]=max(tree[node*2],tree[node*2+1]);
}
int ck;
void query(int node,int st,int en)
{
    if(lazy[node])
    {
        tree[node]+=lazy[node];
        if(st!=en)
        {
            lazy[node*2]+=lazy[node];
            lazy[node*2+1]+=lazy[node];
        }
        lazy[node]=0;
    }
    if(tree[node]<=0)
        return ;
    if(st==en)
    {
        ck=st;
        return ;
    }
    int mid=(st+en)>>1;
    query(node*2+1,mid+1,en);
    if(ck!=-1)
        return ;
    query(node*2,st,mid);
}
int main()
{
    int i,j,k,l,m,n;
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&cost[i]);
        update(1,1,lim,1,cost[i],1);
    }
    for(int i=1; i<=m; i++)
    {
        scanf("%d",&buy[i]);
        update(1,1,lim,1,buy[i],-1);
    }
    int q;
    cin>>q;
    while(q--)
    {
        int ty;
        scanf("%d%d%d",&ty,&l,&k);
        if(ty==1)
        {
            update(1,1,lim,1,cost[l],-1);
            cost[l]=k;
            update(1,1,lim,1,cost[l],1);
        }
        else
        {
            update(1,1,lim,1,buy[l],1);
            buy[l]=k;
            update(1,1,lim,1,buy[l],-1);
        }
        ck=-1;
        query(1,1,lim);
        printf("%d\n",ck);
    }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int lim=1000000;

int tree[5000000],lazy[5000000];
int cost[200000],buy[200000];
void update(int node,int st,int en,int l,int r,int v)
{
    if(lazy[node])
    {
        tree[node]+=lazy[node];
        if(st!=en)
        {
            lazy[node*2]+=lazy[node];
            lazy[node*2+1]+=lazy[node];
        }
        lazy[node]=0;
    }
    if(st>r or en<l)
        return ;
    if(st>=l and en<=r)
    {
        tree[node]+=v;
        if(st!=en)
        {
            lazy[node*2]+=v;
            lazy[node*2+1]+=v;
        }
        return ;
    }
    int mid=(st+en)>>1;
    update(node*2,st,mid,l,r,v);
    update(node*2+1,mid+1,en,l,r,v);
    tree[node]=max(tree[node*2],tree[node*2+1]);
}
int ck;
void query(int node,int st,int en)
{
    if(lazy[node])
    {
        tree[node]+=lazy[node];
        if(st!=en)
        {
            lazy[node*2]+=lazy[node];
            lazy[node*2+1]+=lazy[node];
        }
        lazy[node]=0;
    }
    if(tree[node]<=0)
        return ;
    if(st==en)
    {
        ck=st;
        return ;
    }
    int mid=(st+en)>>1;
    query(node*2+1,mid+1,en);
    if(ck!=-1)
        return ;
    query(node*2,st,mid);
}
int main()
{
    int i,j,k,l,m,n;
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&cost[i]);
        update(1,1,lim,1,cost[i],1);
    }
    for(int i=1; i<=m; i++)
    {
        scanf("%d",&buy[i]);
        update(1,1,lim,1,buy[i],-1);
    }
    int q;
    cin>>q;
    while(q--)
    {
        int ty;
        scanf("%d%d%d",&ty,&l,&k);
        if(ty==1)
        {
            update(1,1,lim,1,cost[l],-1);
            cost[l]=k;
            update(1,1,lim,1,cost[l],1);
        }
        else
        {
            update(1,1,lim,1,buy[l],1);
            buy[l]=k;
            update(1,1,lim,1,buy[l],-1);
        }
        ck=-1;
        query(1,1,lim);
        printf("%d\n",ck);
    }

}


#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+10;
int n, m, q, a[N], b[N];
struct SegTr{
	struct Node{
		int l, r, v, lz;
		Node(){}
		Node(int x, int y){l = x; r = y; v = lz = 0;}
	}nd[N<<2];
	void build(int now, int l, int r){
		nd[now] = Node(l, r);
		if (l == r) return;
		int ls = now<<1;
		int rs = ls^1;
		int mid = (l+r)>>1;
		build(ls, l, mid);
		build(rs, mid+1, r);
	}
	void push_up(int now){
		nd[now].v = min(nd[now<<1].v, nd[now<<1^1].v);
	}
	void push_down(int now){
		int ls = now<<1;
		int rs = ls^1;
		if (nd[now].lz){
			nd[ls].v += nd[now].lz;
			nd[ls].lz += nd[now].lz;
			nd[rs].v += nd[now].lz;
			nd[rs].lz += nd[now].lz;
			nd[now].lz = 0;
		}
	}
	void update(int now, int L, int R, int X){
		if (L <= nd[now].l && nd[now].r <= R){
			nd[now].v += X;
			nd[now].lz += X;
			return;
		}
		int ls = now<<1;
		int rs = ls^1;
		push_down(now);
		if (L <= nd[ls].r) update(ls, L, R, X);
		if (nd[rs].l <= R) update(rs, L, R, X);
		push_up(now);
	}
	int solve(int now){
		if (nd[now].l == nd[now].r) return nd[now].l;
		if (nd[now].v >= 0) return -1;
		push_down(now);
		if (nd[now<<1^1].v < 0) return solve(now<<1^1);
		else return solve(now<<1);
	}
}t;

int main()
{
	scanf("%d%d", &n, &m);
	t.build(1, 1, N-1);
	for (int i = 1; i <= n; ++ i){
		scanf("%d", &a[i]);
		t.update(1, 1, a[i], -1);
	}
	for (int i = 1; i <= m; ++ i){
		scanf("%d", &b[i]);
		t.update(1, 1, b[i], 1);
	}
	scanf("%d", &q);
	for (int i = 1; i <= q; ++ i){
		int opt, id, x;
		scanf("%d%d%d", &opt, &id, &x);
		if (opt == 1){
			t.update(1, 1, a[id], 1);
			t.update(1, 1, x, -1);
			a[id] = x;
		}
		else{
			t.update(1, 1, b[id], -1);
			t.update(1, 1, x, 1);
			b[id] = x;
		}
		printf("%d\n", t.solve(1));
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e6+5;
const int mod=1e9+7;
int a[N],b[N],tree[N<<2],tag[N<<2];
void pushdown(int o){
	if(tag[o]){
		tag[o<<1]+=tag[o];
		tag[o<<1|1]+=tag[o];
		tree[o<<1]+=tag[o];
		tree[o<<1|1]+=tag[o];
		tag[o]=0;
	}
}
void update(int L,int R,int v,int l=1,int r=1e6,int o=1){
	if(L<=l&&r<=R){
		tag[o]+=v;
		tree[o]+=v;
		return;
	}
	int mid=l+r>>1;
	pushdown(o);
	if(L<=mid)update(L,R,v,l,mid,o<<1);
	if(R>mid)update(L,R,v,mid+1,r,o<<1|1);
	tree[o]=max(tree[o<<1],tree[o<<1|1]);
}
int query(int l=1,int r=1e6,int o=1){
	if(l==r){
		return l;
	}
	int mid=l+r>>1;
	pushdown(o);
	if(tree[o<<1|1]>0)return query(mid+1,r,o<<1|1);
	return query(l,mid,o<<1);
}
int main(){
	int n,m,q;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		update(1,a[i],1);
	}
	for(int i=1;i<=m;i++){
		scanf("%d",&b[i]);
		update(1,b[i],-1);
	}
	scanf("%d",&q);
	for(int i=1;i<=q;i++){
		int op,id,x;
		scanf("%d%d%d",&op,&id,&x);
		if(op==1){
			update(1,a[id],-1);
			update(1,x,1);
			a[id]=x;
		}
		else{
			update(1,b[id],1);
			update(1,x,-1);
			b[id]=x;
		}

		printf("%d\n",tree[1]<=0?-1:query());
	}
	return 0;
}


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

const int N = 1e6 + 7;

int mn[N << 2], lzy[N << 2], a[N], b[N];

void push(int v, int l, int r) {
	if(lzy[v]) {
		mn[v] += lzy[v];
		if(l != r) {
			lzy[v * 2] += lzy[v];
			lzy[v * 2 + 1] += lzy[v];
		}
		lzy[v] = 0;
	}
}

void update(int v, int l, int r, int b, int e, int x) {
	push(v, l, r);
	if(r < b || l > e) return;
	if(l >= b && r <= e) {
		lzy[v] += x;
		push(v, l, r);
		return;
	}
	int mid = (l + r) >> 1;
	update(v * 2, l, mid, b, e, x);
	update(v * 2 + 1, mid + 1, r, b, e, x);
	mn[v] = min(mn[v * 2], mn[v * 2 + 1]);
}

int query(int v, int l, int r) {
	push(v, l, r);
	if(l == r) {
		return l;
	}
	int mid = (l + r) >> 1;
	push(v * 2, l, mid);
	push(v * 2 + 1, mid + 1, r);
	if(mn[v * 2 + 1] >= 0) return query(v * 2, l, mid);
	else return query(v * 2 + 1, mid + 1, r);
}

/*void dbg(int v, int l, int r) {
	push(v, l, r);
	if(r <= 10) cout << l << " " << r << " " << mn[v] << endl;
	if(l == r) {
		return;
	}
	int mid = (l + r) >> 1;
	dbg(v * 2, l, mid);
	dbg(v * 2 + 1, mid + 1, r);
}*/

int main() {
	ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int n, m; cin >> n >> m;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		update(1, 1, 1e6, 1, a[i], -1);
	}
	for(int i = 1; i <= m; i++) {
		cin >> b[i];
		update(1, 1, 1e6, 1, b[i], +1);
	}
	int q; cin >> q;
	for(int i = 1; i <= q; i++) {
		int typ, idx, x; cin >> typ >> idx >> x;
		if(typ == 1) {
			update(1, 1, 1e6, 1, a[idx], +1);
			a[idx] = x;
			update(1, 1, 1e6, 1, a[idx], -1);
		} else {
			update(1, 1, 1e6, 1, b[idx], -1);
			b[idx] = x;
			update(1, 1, 1e6, 1, b[idx], +1);
		}

		if(mn[1] >= 0) cout << -1 << endl;
		else cout << query(1, 1, 1e6) << endl;
	}
}


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

const int N = 1e6 + 7;

int mn[N << 2], lzy[N << 2], a[N], b[N];

void push(int v, int l, int r) {
	if(lzy[v]) {
		mn[v] += lzy[v];
		if(l != r) {
			lzy[v * 2] += lzy[v];
			lzy[v * 2 + 1] += lzy[v];
		}
		lzy[v] = 0;
	}
}

void update(int v, int l, int r, int b, int e, int x) {
	push(v, l, r);
	if(r < b || l > e) return;
	if(l >= b && r <= e) {
		lzy[v] += x;
		push(v, l, r);
		return;
	}
	int mid = (l + r) >> 1;
	update(v * 2, l, mid, b, e, x);
	update(v * 2 + 1, mid + 1, r, b, e, x);
	mn[v] = min(mn[v * 2], mn[v * 2 + 1]);
}

int query(int v, int l, int r) {
	push(v, l, r);
	if(l == r) {
		return l;
	}
	int mid = (l + r) >> 1;
	push(v * 2, l, mid);
	push(v * 2 + 1, mid + 1, r);
	if(mn[v * 2] < mn[v * 2 + 1]) return query(v * 2, l, mid);
	else return query(v * 2 + 1, mid + 1, r);
}

int main() {
	ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int n, m; cin >> n >> m;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		update(1, 1, 1e6, 1, a[i], -1);
	}
	for(int i = 1; i <= m; i++) {
		cin >> b[i];
		update(1, 1, 1e6, 1, b[i], +1);
	}
	int q; cin >> q;
	for(int i = 1; i <= q; i++) {
		int typ, idx, x; cin >> typ >> idx >> x;
		if(typ == 1) {
			update(1, 1, 1e6, 1, a[idx], +1);
			a[idx] = x;
			update(1, 1, 1e6, 1, a[idx], -1);
		} else {
			update(1, 1, 1e6, 1, b[idx], -1);
			b[idx] = x;
			update(1, 1, 1e6, 1, b[idx], +1);
		}
		if(mn[1] >= 0) cout << -1 << endl;
		else cout << query(1, 1, 1e6) << endl;
	}
}


#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using namespace std;

// #define int long long
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

ll mod = (1000000007LL);

inline ll poww(ll a, ll b)
{
    ll res = 1;
    while (b > 0)
    {
        if(b & 1) res = (res * a)%mod;
        a = (a * a)%mod;
        b >>= 1;
    }
    return res;
}
inline pii operator+(pii A, pii B){
	return pii(A.f + B.f, A.s + B.s);
}
inline pii operator-(pii A, pii B){
	return pii(A.f - B.f, A.s - B.s);
}
inline ll mult(ll a, ll b){
	return (a * b) % mod;
}

ll gcd (ll a, ll b) {
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}

///CUIDADO COM MAXN
#define N 1000050

int n, m, q, k, v[N], ans, dish[N], cara[N],b[N];
pii w[N];
string s;
int cost[N], inf = 2000000000;
struct segt{
    #define lesq (2*nod)
    #define rdir (2*nod + 1)
    #define mid ((a+b)/2)
    int tree[4*N], lazy[4*N];
    void build(int nod, int a, int b){
        if(a == b) tree[nod] = cost[a];
        else{
            build(lesq, a, mid), build(rdir, mid + 1, b);
            tree[nod] = min(tree[lesq], tree[rdir]);
        }
    }
    void prop(int nod, int a, int b){
        tree[nod] += lazy[nod];
        if(a != b){
            lazy[lesq] += lazy[nod];
            lazy[rdir] += lazy[nod];
        }
        lazy[nod] = 0;
    }
    void upd(int nod, int a, int b, int i, int j, int x){
        prop(nod, a, b);
        if(j < a or i > b) return;
        if(i <= a and j >= b){
            lazy[nod] += x;
            prop(nod, a, b);
        }
        else{
            upd(lesq, a, mid, i, j, x), upd(rdir, mid + 1, b, i, j, x);
            tree[nod] = min(tree[lesq], tree[rdir]);
        }
    }
    int query(int nod, int a, int b, int i, int j){
        prop(nod, a, b);
        if(j < a or i > b) return inf;
        if(i <= a and j >= b) return tree[nod];
        return min(query(lesq, a, mid, i, j), query(rdir, mid + 1, b, i, j));
    }
    int right(int nod, int a, int b){
        prop(nod, a, b);
        if(tree[nod] >= 0) return -1;
        if(a == b) return a;
        // prop(lesq, a, mid);
        // prop(rdir,mid + 1, b);
        int go = right(rdir, mid + 1,  b);
        if(go > 0) return go;
        return right(lesq, a, mid);
    }
    void init(){
        memset(tree, 0, sizeof tree);
        memset(lazy, 0, sizeof lazy);
        build(1, 1, N-1);
    }
} T;
int32_t main(){
	ios::sync_with_stdio(false); cin.tie(0);
    cin>>n>>m;
    for(int i = 1; i <= n; i++){
        cin>>b[i];
        dish[b[i]] ++;
    }
    for(int i = 1; i <= m; i++){
        cin>>v[i];
        cara[v[i]] ++;
    }
   for(int i = N - 1; i >= 1; i--){
        dish[i] += dish[i + 1];
        cara[i] += cara[i + 1];
        cost[i] = cara[i]-dish[i];
   }
   T.init();
   cin>>q;
   while(q--){
        int t, i, x;
        cin>>t>>i>>x;
        if(t == 1){
            T.upd(1, 1, N - 1, 1, b[i], 1);
            b[i] = x;
            T.upd(1, 1, N - 1, 1, b[i], -1);
        }
        else{
            T.upd(1, 1, N - 1, 1, v[i], -1);
            v[i] = x;
            T.upd(1, 1, N - 1, 1, v[i], 1);
        }
        cout<<T.right(1,1,N-1)<<"\n";
   }
}
/*
    CUIDADO COM MAXN, MOD, OVERFLOW
    >Colocar (>DEFINE INT LONG LONG<) no inicio do template
    >mod = (1e9 + 7), por padrao (NAO ESQUECER DE ALTERAR)
    >NAO ESQUECER DEFINE INT LONG LONG
    > N = 1 ?
*/


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll Max=1000000;
ll st[8*Max+5],lazy[8*Max+5],a[Max+5],b[Max+5],n,m,q,R[8*Max+5],L[8*Max+5];
void build(ll id,ll l,ll r)
{
    L[id]=l;
    R[id]=r;
    if (l==r)
        return;
    long long m=(l+r)>>1;
    build(id*2,l,m);
    build(id*2+1,m+1,r);
}
void down(long long id)
{
    long long val=lazy[id];
    if (val==0 || (L[id]==R[id])) return;
    lazy[id*2]+=val;
    lazy[id*2+1]+=val;
    st[id*2]+=val;
    st[id*2+1]+=val;
    lazy[id]=0;
}
void update(ll id ,ll l,ll r, ll u,ll v,ll val)
{

    if(l>v || r<u)
        return;
    if(u<=l && r<=v)
    {
        lazy[id]+=val;
        st[id]+=val;
        return;
    }
    long long m=(l+r)>>1;
    down(id);
    update(id*2,l,m,u,v,val);
    update(id*2+1,m+1,r,u,v,val);
    st[id]=max(st[id*2],st[id*2+1]);
}
long long  get(ll id,ll l,ll r)
{
    down(id);
    if(l==r) {return l; cout<<id<<" "<<l<<" "<<r<<endl; }
    long long m=(l+r)>>1;
    if(st[id*2+1]>=1) return get(id*2+1,m+1,r);
        else return get(id*2,l,m);
}

int main()
{
    //freopen("serge.inp","r",stdin);
    cin>>n>>m;
    build(1,1,Max);
    for (int i=1 ;i<=n;i++)
    {
        cin>>a[i];
        update(1,1,Max,1,a[i],1);
    }
    for (int i=1;i<=m;i++)
    {
        cin>>b[i];
        update(1,1,Max,1,b[i],-1);
    }
    cin>>q;
    for (int i=1;i<=q;i++)
    {
        long long type,pos,val;
        cin>>type>>pos>>val;
        if(type==1)
        {
            update(1,1,Max,1,a[pos],-1);
            a[pos]=val;
            update(1,1,Max,1,a[pos],1);
        }
        else
        {
            update (1,1,Max,1,b[pos],1);
            b[pos]=val;
            update(1,1,Max,1,b[pos],-1);
        }
        if(st[1]<=0)
        {
            cout<<-1<<'\n';
            continue;
        }
        else cout<<get(1,1,Max)<<'\n';
        //cout<<L[15]<<" "<<R[15]<<" "<<st[15];
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll Max=1000000;
ll st[4*Max+5],lazy[4*Max+5],a[Max+5],b[Max+5],n,m,q,R[4*Max+5],L[4*Max+5];

void down(long long id,int l,int r)
{
    long long val=lazy[id];
    if (val==0 || (l == r)) return;
    lazy[id << 1]+=val;
    lazy[(id << 1) | 1]+=val;
    st[id << 1]+=val;
    st[(id << 1) | 1]+=val;
    lazy[id]=0;
}
void update(ll id ,ll l,ll r, ll u,ll v,ll val)
{

    if(l>v || r<u)
        return;
    if(u<=l && r<=v)
    {
        lazy[id]+=val;
        st[id]+=val;
        return;
    }
    long long m=(l+r)>>1;
    down(id,l,r);
    update(id << 1,l,m,u,v,val);
    update((id << 1) | 1,m+1,r,u,v,val);
    st[id]=max(st[id << 1],st[(id << 1) | 1]);
}
long long  get(ll id,ll l,ll r)
{
    down(id,l,r);
    if(l==r) {return l; cout<<id<<" "<<l<<" "<<r<<endl; }
    long long m=(l+r)>>1;
    if(st[(id << 1) | 1]>=1) return get((id << 1) | 1,m+1,r);
        else return get(id << 1,l,m);
}

int main()
{
    //freopen("serge.inp","r",stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin>>n>>m;
    for (int i=1 ;i<=n;i++)
    {
        cin>>a[i];
        update(1,1,Max,1,a[i],1);
    }
    for (int i=1;i<=m;i++)
    {
        cin>>b[i];
        update(1,1,Max,1,b[i],-1);
    }
    cin>>q;
    for (int i=1;i<=q;i++)
    {
        long long type,pos,val;
        cin>>type>>pos>>val;
        if(type==1)
        {
            update(1,1,Max,1,a[pos],-1);
            a[pos]=val;
            update(1,1,Max,1,a[pos],1);
        }
        else
        {
            update (1,1,Max,1,b[pos],1);
            b[pos]=val;
            update(1,1,Max,1,b[pos],-1);
        }
        if(st[1]<=0)
        {
            cout<<-1<<'\n';
            continue;
        }
        else cout<<get(1,1,Max)<<'\n';
        //cout<<L[15]<<" "<<R[15]<<" "<<st[15];
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll Max=1000000;
ll st[4*Max+5],lazy[4*Max+5],a[Max+5],b[Max+5],n,m,q,R[4*Max+5],L[4*Max+5];
void build(ll id,ll l,ll r)
{
    L[id]=l;
    R[id]=r;
    if (l==r)
        return;
    long long m=(l+r)>>1;
    build(id*2,l,m);
    build(id*2+1,m+1,r);
}
void down(long long id)
{
    long long val=lazy[id];
    if (val==0 && (L[id]==R[id])) return;
    lazy[id*2]+=val;
    lazy[id*2+1]+=val;
    st[id*2]+=val;
    st[id*2+1]+=val;
    lazy[id]=0;
}
void update(ll id ,ll l,ll r, ll u,ll v,ll val)
{

    if(l>v || r<u)
        return;
    if(u<=l && r<=v)
    {
        lazy[id]+=val;
        st[id]+=val;
        return;
    }
    long long m=(l+r)>>1;
    down(id);
    update(id*2,l,m,u,v,val);
    update(id*2+1,m+1,r,u,v,val);
    st[id]=max(st[id*2],st[id*2+1]);
}
long long  get(ll id,ll l,ll r)
{
    down(id);
    if(l==r) {return l; cout<<id<<" "<<l<<" "<<r<<endl; }
    long long m=(l+r)>>1;
    if(st[id*2+1]>=1) return get(id*2+1,m+1,r);
        else return get(id*2,l,m);
}

int main()
{
    //freopen("serge.inp","r",stdin);
    cin>>n>>m;
    build(1,1,Max);
    for (int i=1 ;i<=n;i++)
    {
        cin>>a[i];
        update(1,1,Max,1,a[i],1);
    }
    for (int i=1;i<=m;i++)
    {
        cin>>b[i];
        update(1,1,Max,1,b[i],-1);
    }
    cin>>q;
    for (int i=1;i<=q;i++)
    {
        long long type,pos,val;
        cin>>type>>pos>>val;
        if(type==1)
        {
            update(1,1,Max,1,a[pos],-1);
            a[pos]=val;
            update(1,1,Max,1,a[pos],1);
        }
        else
        {
            update (1,1,Max,1,b[pos],1);
            b[pos]=val;
            update(1,1,Max,1,b[pos],-1);
        }
        if(st[1]<=0)
        {
            cout<<-1<<'\n';
            continue;
        }
        else cout<<get(1,1,Max)<<'\n';
        //cout<<L[15]<<" "<<R[15]<<" "<<st[15];
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll Max=1000000;
ll st[4*Max+1],lazy[4*Max+1],a[Max],b[Max],n,m,q,R[4*Max+1],L[4*Max+1];
void build(ll id,ll l,ll r)
{
    L[id]=l;
    R[id]=r;
    if (l==r)
        return;
    long long m=(l+r)>>1;
    build(id*2,l,m);
    build(id*2+1,m+1,r);
}
void down(long long id)
{
    long long val=lazy[id];
    if (val==0) return;
    lazy[id*2]+=val;
    lazy[id*2+1]+=val;
    st[id*2]+=val;
    st[id*2+1]+=val;
    lazy[id]=0;
}
void update(ll id ,ll l,ll r, ll u,ll v,ll val)
{

    if(l>v || r<u)
        return;
    if(u<=l && r<=v)
    {
        lazy[id]+=val;
        st[id]+=val;
        return;
    }
    long long m=(l+r)>>1;
    down(id);
    update(id*2,l,m,u,v,val);
    update(id*2+1,m+1,r,u,v,val);
    st[id]=max(st[id*2],st[id*2+1]);
}
long long  get(ll id,ll l,ll r)
{
    down(id);
    if(l==r) {return l; cout<<id<<" "<<l<<" "<<r<<endl; }
    long long m=(l+r)>>1;
    if(st[id*2+1]>=1) get(id*2+1,m+1,r);
        else get(id*2,l,m);
}

int main()
{
    //freopen("serge.inp","r",stdin);
    cin>>n>>m;
    build(1,1,Max);
    for (int i=1 ;i<=n;i++)
    {
        cin>>a[i];
        update(1,1,Max,1,a[i],1);
    }
    for (int i=1;i<=m;i++)
    {
        cin>>b[i];
        update(1,1,Max,1,b[i],-1);
    }
    cin>>q;
    for (int i=1;i<=q;i++)
    {
        long long type,pos,val;
        cin>>type>>pos>>val;
        if(type==1)
        {
            update(1,1,Max,1,a[pos],-1);
            a[pos]=val;
            update(1,1,Max,1,a[pos],1);
        }
        else
        {
            update (1,1,Max,1,b[pos],1);
            b[pos]=val;
            update(1,1,Max,1,b[pos],-1);
        }
        if(st[1]==0)
        {
            cout<<-1<<'\n';
            continue;
        }
        else cout<<get(1,1,Max)<<'\n';
        //cout<<L[15]<<" "<<R[15]<<" "<<st[15];
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll Max=1000000;
ll st[4*Max+5],lazy[4*Max+5],a[Max+5],b[Max+5],n,m,q,R[4*Max+5],L[4*Max+5];
void build(ll id,ll l,ll r)
{
    L[id]=l;
    R[id]=r;
    if (l==r)
        return;
    long long m=(l+r)>>1;
    build(id*2,l,m);
    build(id*2+1,m+1,r);
}
void down(long long id)
{
    long long val=lazy[id];
    if (val==0 && (L[id]==R[id])) return;
    lazy[id*2]+=val;
    lazy[id*2+1]+=val;
    st[id*2]+=val;
    st[id*2+1]+=val;
    lazy[id]=0;
}
void update(ll id ,ll l,ll r, ll u,ll v,ll val)
{

    if(l>v || r<u)
        return;
    if(u<=l && r<=v)
    {
        lazy[id]+=val;
        st[id]+=val;
        return;
    }
    long long m=(l+r)>>1;
    down(id);
    update(id*2,l,m,u,v,val);
    update(id*2+1,m+1,r,u,v,val);
    st[id]=max(st[id*2],st[id*2+1]);
}
long long  get(ll id,ll l,ll r)
{
    down(id);
    if(l==r) {return l; cout<<id<<" "<<l<<" "<<r<<endl; }
    long long m=(l+r)>>1;
    if(st[id*2+1]>=1) get(id*2+1,m+1,r);
        else get(id*2,l,m);
}

int main()
{
    //freopen("serge.inp","r",stdin);
    cin>>n>>m;
    build(1,1,Max);
    for (int i=1 ;i<=n;i++)
    {
        cin>>a[i];
        update(1,1,Max,1,a[i],1);
    }
    for (int i=1;i<=m;i++)
    {
        cin>>b[i];
        update(1,1,Max,1,b[i],-1);
    }
    cin>>q;
    for (int i=1;i<=q;i++)
    {
        long long type,pos,val;
        cin>>type>>pos>>val;
        if(type==1)
        {
            update(1,1,Max,1,a[pos],-1);
            a[pos]=val;
            update(1,1,Max,1,a[pos],1);
        }
        else
        {
            update (1,1,Max,1,b[pos],1);
            b[pos]=val;
            update(1,1,Max,1,b[pos],-1);
        }
        if(st[1]<=0)
        {
            cout<<-1<<'\n';
            continue;
        }
        else cout<<get(1,1,Max)<<'\n';
        //cout<<L[15]<<" "<<R[15]<<" "<<st[15];
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll Max=1000000;
ll st[4*Max+5],lazy[4*Max+5],a[Max+5],b[Max+5],n,m,q,R[4*Max+5],L[4*Max+5];
void build(ll id,ll l,ll r)
{
    L[id]=l;
    R[id]=r;
    if (l==r)
        return;
    long long m=(l+r)>>1;
    build(id*2,l,m);
    build(id*2+1,m+1,r);
}
void down(long long id)
{
    long long val=lazy[id];
    if (val==0 && (L[id]==R[id])) return;
    lazy[id*2]+=val;
    lazy[id*2+1]+=val;
    st[id*2]+=val;
    st[id*2+1]+=val;
    lazy[id]=0;
}
void update(ll id ,ll l,ll r, ll u,ll v,ll val)
{

    if(l>v || r<u)
        return;
    if(u<=l && r<=v)
    {
        lazy[id]+=val;
        st[id]+=val;
        return;
    }
    long long m=(l+r)>>1;
    down(id);
    update(id*2,l,m,u,v,val);
    update(id*2+1,m+1,r,u,v,val);
    st[id]=max(st[id*2],st[id*2+1]);
}
long long  get(ll id,ll l,ll r)
{
    down(id);
    if(l==r) {return l; cout<<id<<" "<<l<<" "<<r<<endl; }
    long long m=(l+r)>>1;
    if(st[id*2+1]>=1) get(id*2+1,m+1,r);
        else get(id*2,l,m);
}

int main()
{
    //freopen("serge.inp","r",stdin);
    cin>>n>>m;
    build(1,1,Max);
    for (int i=1 ;i<=n;i++)
    {
        cin>>a[i];
        update(1,1,Max,1,a[i],1);
    }
    for (int i=1;i<=m;i++)
    {
        cin>>b[i];
        update(1,1,Max,1,b[i],-1);
    }
    cin>>q;
    for (int i=1;i<=q;i++)
    {
        long long type,pos,val;
        cin>>type>>pos>>val;
        if(type==1)
        {
            update(1,1,Max,1,a[pos],-1);
            a[pos]=val;
            update(1,1,Max,1,a[pos],1);
        }
        else
        {
            update (1,1,Max,1,b[pos],1);
            b[pos]=val;
            update(1,1,Max,1,b[pos],-1);
        }
        if(st[1]==0)
        {
            cout<<-1<<'\n';
            continue;
        }
        else cout<<get(1,1,Max)<<'\n';
        //cout<<L[15]<<" "<<R[15]<<" "<<st[15];
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll Max=1000000;
ll st[4*Max+5],lazy[4*Max+5],a[Max+5],b[Max+5],n,m,q,R[4*Max+5],L[4*Max+5];
void build(ll id,ll l,ll r)
{
    L[id]=l;
    R[id]=r;
    if (l==r)
        return;
    long long m=(l+r)>>1;
    build(id*2,l,m);
    build(id*2+1,m+1,r);
}
void down(long long id)
{
    long long val=lazy[id];
    if (val==0 || (L[id]==R[id])) return;
    lazy[id*2]+=val;
    lazy[id*2+1]+=val;
    st[id*2]+=val;
    st[id*2+1]+=val;
    lazy[id]=0;
}
void update(ll id ,ll l,ll r, ll u,ll v,ll val)
{

    if(l>v || r<u)
        return;
    if(u<=l && r<=v)
    {
        lazy[id]+=val;
        st[id]+=val;
        return;
    }
    long long m=(l+r)>>1;
    down(id);
    update(id*2,l,m,u,v,val);
    update(id*2+1,m+1,r,u,v,val);
    st[id]=max(st[id*2],st[id*2+1]);
}
long long  get(ll id,ll l,ll r)
{
    down(id);
    if(l==r) {return l; cout<<id<<" "<<l<<" "<<r<<endl; }
    long long m=(l+r)>>1;
    if(st[id*2+1]>=1) return get(id*2+1,m+1,r);
        else return get(id*2,l,m);
}

int main()
{
    //freopen("serge.inp","r",stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin>>n>>m;
    build(1,1,Max);
    for (int i=1 ;i<=n;i++)
    {
        cin>>a[i];
        update(1,1,Max,1,a[i],1);
    }
    for (int i=1;i<=m;i++)
    {
        cin>>b[i];
        update(1,1,Max,1,b[i],-1);
    }
    cin>>q;
    for (int i=1;i<=q;i++)
    {
        long long type,pos,val;
        cin>>type>>pos>>val;
        if(type==1)
        {
            update(1,1,Max,1,a[pos],-1);
            a[pos]=val;
            update(1,1,Max,1,a[pos],1);
        }
        else
        {
            update (1,1,Max,1,b[pos],1);
            b[pos]=val;
            update(1,1,Max,1,b[pos],-1);
        }
        if(st[1]<=0)
        {
            cout<<-1<<'\n';
            continue;
        }
        else cout<<get(1,1,Max)<<'\n';
        //cout<<L[15]<<" "<<R[15]<<" "<<st[15];
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll Max=1000000;
ll st[4*Max+5],lazy[4*Max+5],a[Max+5],b[Max+5],n,m,q,R[4*Max+5],L[4*Max+5];
void build(ll id,ll l,ll r)
{
    L[id]=l;
    R[id]=r;
    if (l==r)
        return;
    long long m=(l+r)>>1;
    build(id*2,l,m);
    build(id*2+1,m+1,r);
}
void down(long long id)
{
    long long val=lazy[id];
    if (val==0 || (L[id]==R[id])) return;
    lazy[id*2]+=val;
    lazy[id*2+1]+=val;
    st[id*2]+=val;
    st[id*2+1]+=val;
    lazy[id]=0;
}
void update(ll id ,ll l,ll r, ll u,ll v,ll val)
{

    if(l>v || r<u)
        return;
    if(u<=l && r<=v)
    {
        lazy[id]+=val;
        st[id]+=val;
        return;
    }
    long long m=(l+r)>>1;
    down(id);
    update(id*2,l,m,u,v,val);
    update(id*2+1,m+1,r,u,v,val);
    st[id]=max(st[id*2],st[id*2+1]);
}
long long  get(ll id,ll l,ll r)
{
    down(id);
    if(l==r) {return l; cout<<id<<" "<<l<<" "<<r<<endl; }
    long long m=(l+r)>>1;
    if(st[id*2+1]>=1) return get(id*2+1,m+1,r);
        else return get(id*2,l,m);
}

int main()
{
    //freopen("serge.inp","r",stdin);
    cin>>n>>m;
    build(1,1,Max);
    for (int i=1 ;i<=n;i++)
    {
        cin>>a[i];
        update(1,1,Max,1,a[i],1);
    }
    for (int i=1;i<=m;i++)
    {
        cin>>b[i];
        update(1,1,Max,1,b[i],-1);
    }
    cin>>q;
    for (int i=1;i<=q;i++)
    {
        long long type,pos,val;
        cin>>type>>pos>>val;
        if(type==1)
        {
            update(1,1,Max,1,a[pos],-1);
            a[pos]=val;
            update(1,1,Max,1,a[pos],1);
        }
        else
        {
            update (1,1,Max,1,b[pos],1);
            b[pos]=val;
            update(1,1,Max,1,b[pos],-1);
        }
        if(st[1]<=0)
        {
            cout<<-1<<'\n';
            continue;
        }
        else cout<<get(1,1,Max)<<'\n';
        //cout<<L[15]<<" "<<R[15]<<" "<<st[15];
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fs first
#define sc second
#define mk make_pair
typedef long long ll;
typedef long double ld;
const int INF = 1e9 + 1;
const int MOD = 1e9 + 7;
const int MAKS = 300100;
const int K = 1e6 +5;
int n, m, a[MAKS], b[MAKS], tree[4*K], lazy[4*K] ,x, i, p, q;
void propagate(int idx, int l, int r){
	if (lazy[idx]!=0){
		tree[idx] += lazy[idx];
		if (l!=r){
			lazy[idx*2] += lazy[idx];
			lazy[idx*2+1] += lazy[idx];
		}
		lazy[idx] = 0;
	}
	return ;
}
void update(int idx, int a, int b, int l, int r, int val){
//	printf("%d %d %d\n", idx, a,b );
	propagate(idx, a, b);
	if (b < l || a > r) return;
	if (l <= a && b <= r){
		lazy[idx] += val;
		propagate(idx, a, b);
		return ;
	}
	int mid = (a+b)/2;
	update(idx*2, a, mid, l, r, val);
	update(idx*2+1, mid+1, b, l, r, val);

	tree[idx] = max(tree[idx*2],tree[idx*2+1]);
//	printf("%d %d %d %d\n",idx, a, b, tree[idx] );
	return ;
}
int query(int idx, int l, int r){
	propagate(idx, l, r);
//	printf("lskdfj %d %d %d %d\n", idx, l, r, tree[idx]);
	if (tree[idx]<= 0) return -1;
	if (l==r) return l;
	int mid = (l+r)/2;
	int a = query(idx*2+1, mid+1, r);
	int b = query(idx*2, l, mid);
//	printf("%d %d %d %d\n", l, r, a, b);
	if (a > 0) return a;
	if (b > 0) return b;
}
int main(){
	scanf("%d%d", &n ,&m);
	for (int i = 0; i < n; i++){
		scanf("%d", &a[i]);
		update(1, 1, K, 1, a[i], 1);
	}
	for (int i= 0; i < m; i++){
		cin >> b[i];
		update(1, 1, K, 1, b[i], -1);
	}
	cin >> q;
	while(q--){
		cin >> x >> i >> p;
		if (x==1){
			update(1, 1, K, 1, a[i-1], -1);
			update(1, 1, K, 1, p, 1);
			a[i] = p;
			printf("%d\n", query(1, 1, K));
		}else{
			update(1, 1, K, 1, b[i-1], 1);
			update(1, 1, K, 1, p, -1);
			b[i] = p;
			printf("%d\n", query(1, 1, K));
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fs first
#define sc second
#define mk make_pair
typedef long long ll;
typedef long double ld;
const int INF = 1e9 + 1;
const int MOD = 1e9 + 7;
const int MAKS = 300100;
const int K = 1e6 +5;
int n, m, a[MAKS], b[MAKS], tree[4*K], lazy[4*K] ,x, i, p, q;
void propagate(int idx, int l, int r){
	if (lazy[idx]!=0){
		tree[idx] += lazy[idx];
		if (l!=r){
			lazy[idx*2] += lazy[idx];
			lazy[idx*2+1] += lazy[idx];
		}
		lazy[idx] = 0;
	}
	return ;
}
void update(int idx, int a, int b, int l, int r, int val){
//	printf("%d %d %d\n", idx, a,b );
	propagate(idx, a, b);
	if (b < l || a > r) return;
	if (l <= a && b <= r){
		lazy[idx] += val;
		propagate(idx, a, b);
		return ;
	}
	int mid = (a+b)/2;
	update(idx*2, a, mid, l, r, val);
	update(idx*2+1, mid+1, b, l, r, val);

	tree[idx] = max(tree[idx*2],tree[idx*2+1]);
//	printf("%d %d %d %d\n",idx, a, b, tree[idx] );
	return ;
}
int query(int idx, int l, int r){
	propagate(idx, l, r);
//	printf("lskdfj %d %d %d %d\n", idx, l, r, tree[idx]);
	if (tree[idx]<= 0) return -1;
	if (l==r) return l;
	int mid = (l+r)/2;
	int a = query(idx*2+1, mid+1, r);
	int b = query(idx*2, l, mid);
//	printf("%d %d %d %d\n", l, r, a, b);
	if (a != -1) return a;
	return b;
}
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	scanf("%d%d", &n ,&m);
	for (int i = 1; i <= n; i++){
		cin >> a[i];
		update(1, 1, K-1, 1, a[i], 1);
	}
	for (int i= 1; i <= m; i++){
		cin >> b[i];
		update(1, 1, K-1, 1, b[i], -1);
	}
	cin >> q;
	while(q--){
		cin >> x >> i >> p;
		if (x==1){
			update(1, 1, K-1, 1, a[i], -1);
			update(1, 1, K-1, 1, p, 1);
			a[i] = p;
		}else{
			update(1, 1, K-1, 1, b[i], 1);
			update(1, 1, K-1, 1, p, -1);
			b[i] = p;
		}
		cout << query(1, 1, K-1) << '\n';
	}
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fs first
#define sc second
#define mk make_pair
typedef long long ll;
typedef long double ld;
const int INF = 1e9 + 1;
const int MOD = 1e9 + 7;
const int MAKS = 300100;
const int K = 1e6 +5;
int n, m, a[MAKS], b[MAKS], tree[4*K], lazy[4*K] ,x, i, p, q;
void propagate(int idx, int l, int r){
	if (lazy[idx]!=0){
		tree[idx] += lazy[idx];
		if (l!=r){
			lazy[idx*2] += lazy[idx];
			lazy[idx*2+1] += lazy[idx];
		}
		lazy[idx] = 0;
	}
	return ;
}
void update(int idx, int a, int b, int l, int r, int val){
//	printf("%d %d %d\n", idx, a,b );
	propagate(idx, a, b);
	if (b < l || a > r) return;
	if (l <= a && b <= r){
		lazy[idx] += val;
		propagate(idx, a, b);
		return ;
	}
	int mid = (a+b)/2;
	update(idx*2, a, mid, l, r, val);
	update(idx*2+1, mid+1, b, l, r, val);

	tree[idx] = max(tree[idx*2],tree[idx*2+1]);
//	printf("%d %d %d %d\n",idx, a, b, tree[idx] );
	return ;
}
int query(int idx, int l, int r){
	propagate(idx, l, r);
//	printf("lskdfj %d %d %d %d\n", idx, l, r, tree[idx]);
	if (tree[idx]<= 0) return -1;
	if (l==r) return l;
	int mid = (l+r)/2;
	int a = query(idx*2+1, mid+1, r);

//	printf("%d %d %d %d\n", l, r, a, b);
	if (a != -1) return a;
	return query(idx*2, l, mid);
}
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= n; i++){
		cin >> a[i];
		update(1, 1, K-1, 1, a[i], 1);
	}
	for (int i= 1; i <= m; i++){
		cin >> b[i];
		update(1, 1, K-1, 1, b[i], -1);
	}
	cin >> q;
	while(q--){
		cin >> x >> i >> p;
		if (x==1){
			update(1, 1, K-1, 1, a[i], -1);
			update(1, 1, K-1, 1, p, 1);
			a[i] = p;
		}else{
			update(1, 1, K-1, 1, b[i], 1);
			update(1, 1, K-1, 1, p, -1);
			b[i] = p;
		}
		cout << query(1, 1, K-1) << '\n';
	}
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fs first
#define sc second
#define mk make_pair
typedef long long ll;
typedef long double ld;
const int INF = 1e9 + 1;
const int MOD = 1e9 + 7;
const int MAKS = 300100;
const int K = 1e6 +5;
int n, m, a[MAKS], b[MAKS], tree[4*K], lazy[4*K] ,x, i, p, q;
void propagate(int idx, int l, int r){
	if (lazy[idx]!=0){
		tree[idx] += lazy[idx];
		if (l!=r){
			lazy[idx*2] += lazy[idx];
			lazy[idx*2+1] += lazy[idx];
		}
		lazy[idx] = 0;
	}
	return ;
}
void update(int idx, int a, int b, int l, int r, int val){
//	printf("%d %d %d\n", idx, a,b );
	propagate(idx, a, b);
	if (b < l || a > r) return;
	if (l <= a && b <= r){
		lazy[idx] += val;
		propagate(idx, a, b);
		return ;
	}
	int mid = (a+b)/2;
	update(idx*2, a, mid, l, r, val);
	update(idx*2+1, mid+1, b, l, r, val);

	tree[idx] = max(tree[idx*2],tree[idx*2+1]);
//	printf("%d %d %d %d\n",idx, a, b, tree[idx] );
	return ;
}
int query(int idx, int l, int r){
	propagate(idx, l, r);
//	printf("lskdfj %d %d %d %d\n", idx, l, r, tree[idx]);
	if (tree[idx]<= 0) return -1;
	if (l==r) return l;
	int mid = (l+r)/2;
	int a = query(idx*2+1, mid+1, r);
	int b = query(idx*2, l, mid);
//	printf("%d %d %d %d\n", l, r, a, b);
	if (a != -1) return a;
	return b;
}
int main(){
	scanf("%d%d", &n ,&m);
	for (int i = 0; i < n; i++){
		scanf("%d", &a[i]);
		update(1, 1, K, 1, a[i], 1);
	}
	for (int i= 0; i < m; i++){
		cin >> b[i];
		update(1, 1, K, 1, b[i], -1);
	}
	cin >> q;
	while(q--){
		cin >> x >> i >> p;
		if (x==1){
			update(1, 1, K, 1, a[i-1], -1);
			update(1, 1, K, 1, p, 1);
			a[i-1] = p;
			printf("%d\n", query(1, 1, K));
		}else{
			update(1, 1, K, 1, b[i-1], 1);
			update(1, 1, K, 1, p, -1);
			b[i-1] = p;
			printf("%d\n", query(1, 1, K));
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fs first
#define sc second
#define mk make_pair
typedef long long ll;
typedef long double ld;
const int INF = 1e9 + 1;
const int MOD = 1e9 + 7;
const int MAKS = 300100;
const int K = 1e6 +5;
int n, m, a[MAKS], b[MAKS], tree[4*K], lazy[4*K] ,x, i, p, q;
void propagate(int idx, int l, int r){
	if (lazy[idx]!=0){
		tree[idx] += lazy[idx];
		if (l!=r){
			lazy[idx*2] += lazy[idx];
			lazy[idx*2+1] += lazy[idx];
		}
		lazy[idx] = 0;
	}
	return ;
}
void update(int idx, int a, int b, int l, int r, int val){
//	printf("%d %d %d\n", idx, a,b );
	propagate(idx, a, b);
	if (b < l || a > r) return;
	if (l <= a && b <= r){
		lazy[idx] += val;
		propagate(idx, a, b);
		return ;
	}
	int mid = (a+b)/2;
	update(idx*2, a, mid, l, r, val);
	update(idx*2+1, mid+1, b, l, r, val);

	tree[idx] = max(tree[idx*2],tree[idx*2+1]);
//	printf("%d %d %d %d\n",idx, a, b, tree[idx] );
	return ;
}
int query(int idx, int l, int r){
	propagate(idx, l, r);
//	printf("lskdfj %d %d %d %d\n", idx, l, r, tree[idx]);
	if (tree[idx]<= 0) return -1;
	if (l==r) return l;
	int mid = (l+r)/2;
	int a = query(idx*2+1, mid+1, r);
	int b = query(idx*2, l, mid);
//	printf("%d %d %d %d\n", l, r, a, b);
	if (a != -1) return a;
	return b;
}
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= n; i++){
		cin >> a[i];
		update(1, 1, K-1, 1, a[i], 1);
	}
	for (int i= 1; i <= m; i++){
		cin >> b[i];
		update(1, 1, K-1, 1, b[i], -1);
	}
	cin >> q;
	while(q--){
		cin >> x >> i >> p;
		if (x==1){
			update(1, 1, K-1, 1, a[i], -1);
			update(1, 1, K-1, 1, p, 1);
			a[i] = p;
		}else{
			update(1, 1, K-1, 1, b[i], 1);
			update(1, 1, K-1, 1, p, -1);
			b[i] = p;
		}
		cout << query(1, 1, K-1) << '\n';
	}
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fs first
#define sc second
#define mk make_pair
typedef long long ll;
typedef long double ld;
const int INF = 1e9 + 1;
const int MOD = 1e9 + 7;
const int MAKS = 300100;
const int K = 1e6 +5;
int n, m, a[MAKS], b[MAKS], tree[4*K], lazy[4*K] ,x, i, p, q;
void propagate(int idx, int l, int r){
	if (lazy[idx]!=0){
		tree[idx] += lazy[idx];
		if (l!=r){
			lazy[idx*2] += lazy[idx];
			lazy[idx*2+1] += lazy[idx];
		}
		lazy[idx] = 0;
	}
	return ;
}
void update(int idx, int a, int b, int l, int r, int val){
//	printf("%d %d %d\n", idx, a,b );
	propagate(idx, a, b);
	if (b < l || a > r) return;
	if (l <= a && b <= r){
		lazy[idx] += val;
		propagate(idx, a, b);
		return ;
	}
	int mid = (a+b)/2;
	update(idx*2, a, mid, l, r, val);
	update(idx*2+1, mid+1, b, l, r, val);

	tree[idx] = max(tree[idx*2],tree[idx*2+1]);
//	printf("%d %d %d %d\n",idx, a, b, tree[idx] );
	return ;
}
int query(int idx, int l, int r){
	propagate(idx, l, r);
//	printf("lskdfj %d %d %d %d\n", idx, l, r, tree[idx]);
	if (tree[idx]<= 0) return -1;
	if (l==r) return l;
	int mid = (l+r)/2;
	int a = query(idx*2+1, mid+1, r);

//	printf("%d %d %d %d\n", l, r, a, b);
	if (a != -1) return a;
	return query(idx*2+1, l, mid);
}
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= n; i++){
		cin >> a[i];
		update(1, 1, K-1, 1, a[i], 1);
	}
	for (int i= 1; i <= m; i++){
		cin >> b[i];
		update(1, 1, K-1, 1, b[i], -1);
	}
	cin >> q;
	while(q--){
		cin >> x >> i >> p;
		if (x==1){
			update(1, 1, K-1, 1, a[i], -1);
			update(1, 1, K-1, 1, p, 1);
			a[i] = p;
		}else{
			update(1, 1, K-1, 1, b[i], 1);
			update(1, 1, K-1, 1, p, -1);
			b[i] = p;
		}
		cout << query(1, 1, K-1) << '\n';
	}
}


#include <stdio.h>
#include <vector>
#include <stack>
#include <queue>
#include <utility>
#include <algorithm>
#include <string.h>
// #include <bits/stdc++.h>
using namespace std;

pair<int, int> A[600001];
int i, n, m, q, left[600001], right[600001], height[600001], buffer1[600001], buffer2[600001], r = -1, a, b, c;

int updatebuffers(pair<int, int> A[], int left[], int right[], int buffer1[], int buffer2[], int r)
{
	if(left[r] == -1)
	{
		if(A[r].second == 1)
		{
			if(buffer2[right[r]] > 0)
			{
				buffer2[r] = buffer2[right[r]] - 1;
				buffer1[r] = buffer1[right[r]];
			}
			else
			{
				buffer2[r] = 0;
				buffer1[r] = buffer1[right[r]] + 1;
			}
		}
		else
		{
			buffer1[r] = buffer1[right[r]];
			buffer2[r] = buffer2[right[r]] + 1;
		}
	}
	else if(right[r] == -1)
	{
		if(A[r].second == 1)
		{
			buffer1[r] = buffer1[left[r]] + 1;
			buffer2[r] = buffer2[left[r]];
		}
		else
		{
			if(buffer1[left[r]] > 0)
			{
				buffer1[r] = buffer1[left[r]] - 1;
				buffer2[r] = buffer2[left[r]];
			}
			else
			{
				buffer1[r] = 0;
				buffer2[r] = buffer2[left[r]] + 1;
			}
		}
	}
	else
	{
		if(A[r].second == 1)
		{
			if(buffer2[right[r]] > 0)
			{
				buffer2[r] = buffer2[right[r]] - 1;
				buffer1[r] = buffer1[right[r]];
			}
			else
			{
				buffer2[r] = 0;
				buffer1[r] = buffer1[right[r]] + 1;
			}
		}
		else
		{
			buffer2[r] = buffer2[right[r]] + 1;
			buffer1[r] = buffer1[right[r]];
		}
		if(buffer2[r] >= buffer1[left[r]])
		{
			buffer2[r] -= buffer1[left[r]];
			buffer2[r] += buffer2[left[r]];
		}
		else
		{
			buffer1[r] += buffer1[left[r]] - buffer2[r];
			buffer2[r] = buffer2[left[r]];
		}
	}
}

int max(int a, int b)
{
	if(a > b)
	{
		return a;
	}
	return b;
}

int heigt(int left[], int right[], int height[], int x)
{
	if(left[x] == -1 && right[x] == -1)
	{
		return 1;
	}
	if(left[x] == -1)
	{
		return height[right[x]] + 1;
	}
	if(right[x] == -1)
	{
		return height[left[x]] + 1;
	}
	return max(height[left[x]], height[right[x]]) + 1;
}

int getbalance(int left[], int right[], int height[], int r)
{
	int balance;
	if(left[r] == -1)
	{
		balance = -height[right[r]];
	}
	else if(right[r] == -1)
	{
		balance = height[left[r]];
	}
	else
	{
		balance = height[left[r]] - height[right[r]];
	}

	return balance;
}

int leftrotate(pair<int, int> A[], int left[], int right[], int height[], int buffer1[], int buffer2[], int x)
{
	int y, T2;
	y = right[x];
	T2 = left[y];
	left[y] = x;
	right[x] = T2;
	height[x] = heigt(left, right, height, x);
	updatebuffers(A, left, right, buffer1, buffer2, x);
	height[y] = heigt(left, right, height, y);
	updatebuffers(A, left, right, buffer1, buffer2, y);
	return y;
}

int rightrotate(pair<int, int> A[], int left[], int right[], int height[], int buffer1[], int buffer2[], int y)
{
	int x, T2;
	x = left[y];
	T2 = right[x];
	right[x] = y;
	left[y] = T2;
	height[y] = heigt(left, right, height, y);
	updatebuffers(A, left, right, buffer1, buffer2, y);
	height[x] = heigt(left, right, height, x);
	updatebuffers(A, left, right, buffer1, buffer2, x);
	return x;
}

bool more(pair<int, int> A[], int index, int r)
{
	if(A[index].first > A[r].first)
	{
		return true;
	}
	else if(A[index].first < A[r].first)
	{
		return false;
	}
	else if(A[index].second > A[r].second)
	{
		return true;
	}
	else if(A[index].second < A[r].second)
	{
		return false;
	}
	else if(index > r)
	{
		return true;
	}
	else
	{
		return false;
	}
}

int insert(pair<int, int> A[], int left[], int right[], int height[], int buffer1[], int buffer2[], int index, int r)
{
	int balance;
	if(r == -1)
	{
		r = index;
		left[r] = -1;
		right[r] = -1;
		height[r] = 1;
		if(A[index].second == 1)
		{
			buffer1[r] = 1;
			buffer2[r] = 0;
		}
		else
		{
			buffer1[r] = 0;
			buffer2[r] = 1;
		}
		return r;
	}
	if(more(A, index, r))
	{
		right[r] = insert(A, left, right, height, buffer1, buffer2, index, right[r]);
	}
	else
	{
		left[r] = insert(A, left, right, height, buffer1, buffer2, index, left[r]);
	}
	if(left[r] == -1)
	{
		height[r] = height[right[r]] + 1;
		balance = -height[right[r]];
	}
	else if(right[r] == -1)
	{
		height[r] = height[left[r]] + 1;
		balance = height[left[r]];
	}
	else
	{
		height[r] = max(height[left[r]], height[right[r]]) + 1;
		balance = height[left[r]] - height[right[r]];
	}

	updatebuffers(A, left, right, buffer1, buffer2, r);

	if(balance > 1 && more(A, left[r], index))
	{
		return rightrotate(A, left, right, height, buffer1, buffer2, r);
	}

	if(balance < -1 && more(A, index, right[r]))
	{
		return leftrotate(A, left, right, height, buffer1, buffer2, r);
	}

	if(balance > 1 && more(A, index, left[r]))
	{
		left[r] = leftrotate(A, left, right, height, buffer1, buffer2, left[r]);
		return rightrotate(A, left, right, height, buffer1, buffer2, r);
	}

	if(balance < -1 && more(A, right[r], index))
	{
		right[r] = rightrotate(A, left, right, height, buffer1, buffer2, right[r]);
		return leftrotate(A, left, right, height, buffer1, buffer2, r);
	}

	return r;
}

int minvalnode(int left[], int r)
{
	int current = r;
	while(left[current] != -1)
	{
		current = left[current];
	}

	return current;
}

int del(pair<int, int> A[], int left[], int right[], int height[], int buffer1[], int buffer2[], int index, int r)
{
	int temp, balance;
	if(r == -1)
	{
		return r;
	}

	if(more(A, index, r))
	{
		right[r] = del(A, left, right, height, buffer1, buffer2, index, right[r]);
	}
	else if(more(A, r, index))
	{
		left[r] = del(A, left, right, height, buffer1, buffer2, index, left[r]);
	}
	else
	{
		if(left[r] == -1 || right[r] == -1)
		{
			if(left[r] == -1)
			{
				temp = right[r];
			}
			else
			{
				temp = left[r];
			}
			r = temp;
		}
		else
		{
			temp = minvalnode(left, right[r]);
			r = temp;
			right[r] = del(A, left, right, height, buffer1, buffer2, temp, right[r]);
		}
	}

	if(r == -1)
	{
		return r;
	}

	if(left[r] == -1)
	{
		height[r] = height[right[r]] + 1;
		balance = -height[right[r]];
	}
	else if(right[r] == -1)
	{
		height[r] = height[left[r]] + 1;
		balance = height[left[r]];
	}
	else
	{
		height[r] = max(height[left[r]], height[right[r]]) + 1;
		balance = height[left[r]] - height[right[r]];
	}

	if(balance > 1 && getbalance(left, right, height, left[r]) >= 0)
	{
		return rightrotate(A, left, right, height, buffer1, buffer2, r);
	}

	if(balance > 1 && getbalance(left, right, height, left[r]) < 0)
	{
		left[r] = leftrotate(A, left, right, height, buffer1, buffer2, left[r]);
		return rightrotate(A, left, right, height, buffer1, buffer2, r);
	}

	if(balance < -1 && getbalance(left, right, height, right[r]) <= 0)
	{
		return leftrotate(A, left, right, height, buffer1, buffer2, r);
	}

	if(balance < -1 && getbalance(left, right, height, left[r]) > 0)
	{
		right[r] = rightrotate(A, left, right, height, buffer1, buffer2, right[r]);
		return leftrotate(A, left, right, height, buffer1, buffer2, r);
	}

	return r;
}

int find(pair<int, int> A[], int left[], int right[], int buffer1[], int buffer2[], int r, int count)
{
	if(r == -1)
	{
		return -1;
	}

	if(buffer1[r] <= count)
	{
		return -1;
	}
	if(right[r] != -1)
	{
		if(buffer1[right[r]] > count)
		{
			return find(A, left, right, buffer1, buffer2, right[r], count);
		}
		else if(left[r] == -1)
		{
			return A[r].first;
		}
		else if(A[r].second == 1)
		{
			if(buffer1[right[r]] == count && buffer2[right[r]] == 0)
			{
				return A[r].first;
			}
			else
			{
				return find(A, left, right, buffer1, buffer2, left[r], count - buffer1[right[r]] + buffer2[right[r]] - 1);
			}
		}
		else
		{
			return find(A, left, right, buffer1, buffer2, left[r], count - buffer1[right[r]] + buffer2[right[r]] + 1);
		}
	}
	if(left[r] == -1)
	{
		return A[r].first;
	}
	else if(A[r].second == 1)
	{
		return find(A, left, right, buffer1, buffer2, left[r], count - 1);
	}
	else
	{
		return find(A, left, right, buffer1, buffer2, left[r], count + 1);
	}
}

int main()
{
	scanf("%d%d", &n, &m);

	for(i = 1; i <= n; i++)
	{
		scanf("%d", &A[i].first);
		A[i].second = 1;
		r = insert(A, left, right, height, buffer1, buffer2, i, r);
	}

	for(i = 1; i <= m; i++)
	{
		scanf("%d", &A[i + n].first);
		A[i + n].second = 2;
		r = insert(A, left, right, height, buffer1, buffer2, i + n, r);
	}

	scanf("%d", &q);

	while(q--)
	{
		scanf("%d%d%d", &a, &b, &c);
		r = del(A, left, right, height, buffer1, buffer2, (a - 1)*n + b, r);
		A[(a-1)*n+b].first = c;
		r = insert(A, left, right, height, buffer1, buffer2, (a - 1)*n + b, r);
		// for(i = 1; i <= n + m; i++)
		// {
		// 	printf("%d, first = %d, second = %d, left = %d, right = %d, buffer = %d, %d\n", i, A[i].first, A[i].second, left[i], right[i], buffer1[i], buffer2[i]);
		// }
		printf("%d\n", find(A, left, right, buffer1, buffer2, r, 0));
	}
	return 0;
}


#include <stdio.h>
#include <vector>
#include <stack>
#include <queue>
#include <utility>
#include <algorithm>
#include <string.h>
// #include <bits/stdc++.h>
using namespace std;

pair<int, int> A[600001];
int i, n, m, q, left[600001], right[600001], height[600001], buffer1[600001], buffer2[600001], r = -1, a, b, c;

int updatebuffers(pair<int, int> A[], int left[], int right[], int buffer1[], int buffer2[], int r)
{
	if(left[r] == -1)
	{
		if(A[r].second == 1)
		{
			if(buffer2[right[r]] > 0)
			{
				buffer2[r] = buffer2[right[r]] - 1;
				buffer1[r] = buffer1[right[r]];
			}
			else
			{
				buffer2[r] = 0;
				buffer1[r] = buffer1[right[r]] + 1;
			}
		}
		else
		{
			buffer1[r] = buffer1[right[r]];
			buffer2[r] = buffer2[right[r]] + 1;
		}
	}
	else if(right[r] == -1)
	{
		if(A[r].second == 1)
		{
			buffer1[r] = buffer1[left[r]] + 1;
			buffer2[r] = buffer2[left[r]];
		}
		else
		{
			if(buffer1[left[r]] > 0)
			{
				buffer1[r] = buffer1[left[r]] - 1;
				buffer2[r] = buffer2[left[r]];
			}
			else
			{
				buffer1[r] = 0;
				buffer2[r] = buffer2[left[r]] + 1;
			}
		}
	}
	else
	{
		if(A[r].second == 1)
		{
			if(buffer2[right[r]] > 0)
			{
				buffer2[r] = buffer2[right[r]] - 1;
				buffer1[r] = buffer1[right[r]];
			}
			else
			{
				buffer2[r] = 0;
				buffer1[r] = buffer1[right[r]] + 1;
			}
		}
		else
		{
			buffer2[r] = buffer2[right[r]] + 1;
			buffer1[r] = buffer1[right[r]];
		}
		if(buffer2[r] >= buffer1[left[r]])
		{
			buffer2[r] -= buffer1[left[r]];
			buffer2[r] += buffer2[left[r]];
		}
		else
		{
			buffer1[r] += buffer1[left[r]] - buffer2[r];
			buffer2[r] = buffer2[left[r]];
		}
	}
}

int max(int a, int b)
{
	if(a > b)
	{
		return a;
	}
	return b;
}

int heigt(int left[], int right[], int height[], int x)
{
	if(left[x] == -1 && right[x] == -1)
	{
		return 1;
	}
	if(left[x] == -1)
	{
		return height[right[x]] + 1;
	}
	if(right[x] == -1)
	{
		return height[left[x]] + 1;
	}
	return max(height[left[x]], height[right[x]]) + 1;
}

int getbalance(int left[], int right[], int height[], int r)
{
	int balance;
	if(left[r] == -1)
	{
		balance = -height[right[r]];
	}
	else if(right[r] == -1)
	{
		balance = height[left[r]];
	}
	else
	{
		balance = height[left[r]] - height[right[r]];
	}

	return balance;
}

int leftrotate(pair<int, int> A[], int left[], int right[], int height[], int buffer1[], int buffer2[], int x)
{
	int y, T2;
	y = right[x];
	T2 = left[y];
	left[y] = x;
	right[x] = T2;
	height[x] = heigt(left, right, height, x);
	updatebuffers(A, left, right, buffer1, buffer2, x);
	height[y] = heigt(left, right, height, y);
	updatebuffers(A, left, right, buffer1, buffer2, y);
	return y;
}

int rightrotate(pair<int, int> A[], int left[], int right[], int height[], int buffer1[], int buffer2[], int y)
{
	int x, T2;
	x = left[y];
	T2 = right[x];
	right[x] = y;
	left[y] = T2;
	height[y] = heigt(left, right, height, y);
	updatebuffers(A, left, right, buffer1, buffer2, y);
	height[x] = heigt(left, right, height, x);
	updatebuffers(A, left, right, buffer1, buffer2, x);
	return x;
}

bool more(pair<int, int> A[], int index, int r)
{
	if(A[index].first > A[r].first)
	{
		return true;
	}
	else if(A[index].first < A[r].first)
	{
		return false;
	}
	else if(A[index].second > A[r].second)
	{
		return true;
	}
	else if(A[index].second < A[r].second)
	{
		return false;
	}
	else if(index > r)
	{
		return true;
	}
	else
	{
		return false;
	}
}

int insert(pair<int, int> A[], int left[], int right[], int height[], int buffer1[], int buffer2[], int index, int r)
{
	int balance;
	if(r == -1)
	{
		r = index;
		left[r] = -1;
		right[r] = -1;
		height[r] = 1;
		if(A[index].second == 1)
		{
			buffer1[r] = 1;
			buffer2[r] = 0;
		}
		else
		{
			buffer1[r] = 0;
			buffer2[r] = 1;
		}
		return r;
	}
	if(more(A, index, r))
	{
		right[r] = insert(A, left, right, height, buffer1, buffer2, index, right[r]);
	}
	else
	{
		left[r] = insert(A, left, right, height, buffer1, buffer2, index, left[r]);
	}
	if(left[r] == -1)
	{
		height[r] = height[right[r]] + 1;
		balance = -height[right[r]];
	}
	else if(right[r] == -1)
	{
		height[r] = height[left[r]] + 1;
		balance = height[left[r]];
	}
	else
	{
		height[r] = max(height[left[r]], height[right[r]]) + 1;
		balance = height[left[r]] - height[right[r]];
	}

	updatebuffers(A, left, right, buffer1, buffer2, r);

	if(balance > 1 && more(A, left[r], index))
	{
		return rightrotate(A, left, right, height, buffer1, buffer2, r);
	}

	if(balance < -1 && more(A, index, right[r]))
	{
		return leftrotate(A, left, right, height, buffer1, buffer2, r);
	}

	if(balance > 1 && more(A, index, left[r]))
	{
		left[r] = leftrotate(A, left, right, height, buffer1, buffer2, left[r]);
		return rightrotate(A, left, right, height, buffer1, buffer2, r);
	}

	if(balance < -1 && more(A, right[r], index))
	{
		right[r] = rightrotate(A, left, right, height, buffer1, buffer2, right[r]);
		return leftrotate(A, left, right, height, buffer1, buffer2, r);
	}

	return r;
}

int minvalnode(int left[], int r)
{
	int current = r;
	while(left[current] != -1)
	{
		current = left[current];
	}

	return current;
}

int del(pair<int, int> A[], int left[], int right[], int height[], int buffer1[], int buffer2[], int index, int r)
{
	// printf("Check 1: %d, %d\n", index, r);
	int temp, balance;
	if(r == -1)
	{
		return r;
	}

	if(more(A, index, r))
	{
		right[r] = del(A, left, right, height, buffer1, buffer2, index, right[r]);
	}
	else if(more(A, r, index))
	{
		left[r] = del(A, left, right, height, buffer1, buffer2, index, left[r]);
	}
	else
	{
		if(left[r] == -1 || right[r] == -1)
		{
			if(left[r] == -1)
			{
				temp = right[r];
			}
			else
			{
				temp = left[r];
			}
			r = temp;
		}
		else
		{
			temp = minvalnode(left, right[r]);
			right[temp] = del(A, left, right, height, buffer1, buffer2, temp, right[r]);
			left[temp] = left[r];
			height[temp] = height[r];
			r = temp;
		}
	}

	if(r == -1)
	{
		return r;
	}

	if(left[r] == -1)
	{
		height[r] = height[right[r]] + 1;
		balance = -height[right[r]];
	}
	else if(right[r] == -1)
	{
		height[r] = height[left[r]] + 1;
		balance = height[left[r]];
	}
	else
	{
		height[r] = max(height[left[r]], height[right[r]]) + 1;
		balance = height[left[r]] - height[right[r]];
	}

	updatebuffers(A, left, right, buffer1, buffer2, r);

	if(balance > 1 && getbalance(left, right, height, left[r]) >= 0)
	{
		return rightrotate(A, left, right, height, buffer1, buffer2, r);
	}

	if(balance > 1 && getbalance(left, right, height, left[r]) < 0)
	{
		left[r] = leftrotate(A, left, right, height, buffer1, buffer2, left[r]);
		return rightrotate(A, left, right, height, buffer1, buffer2, r);
	}

	if(balance < -1 && getbalance(left, right, height, right[r]) <= 0)
	{
		return leftrotate(A, left, right, height, buffer1, buffer2, r);
	}

	if(balance < -1 && getbalance(left, right, height, left[r]) > 0)
	{
		right[r] = rightrotate(A, left, right, height, buffer1, buffer2, right[r]);
		return leftrotate(A, left, right, height, buffer1, buffer2, r);
	}

	return r;
}

int find(pair<int, int> A[], int left[], int right[], int buffer1[], int buffer2[], int r, int count)
{
	if(r == -1)
	{
		return -1;
	}

	if(buffer1[r] <= count)
	{
		return -1;
	}
	if(right[r] != -1)
	{
		if(buffer1[right[r]] > count)
		{
			return find(A, left, right, buffer1, buffer2, right[r], count);
		}
		else if(left[r] == -1)
		{
			return A[r].first;
		}
		else if(A[r].second == 1)
		{
			if(buffer1[right[r]] == count && buffer2[right[r]] == 0)
			{
				return A[r].first;
			}
			else
			{
				return find(A, left, right, buffer1, buffer2, left[r], count - buffer1[right[r]] + buffer2[right[r]] - 1);
			}
		}
		else
		{
			return find(A, left, right, buffer1, buffer2, left[r], count - buffer1[right[r]] + buffer2[right[r]] + 1);
		}
	}
	if(left[r] == -1)
	{
		return A[r].first;
	}
	else if(A[r].second == 1)
	{
		return find(A, left, right, buffer1, buffer2, left[r], count - 1);
	}
	else
	{
		return find(A, left, right, buffer1, buffer2, left[r], count + 1);
	}
}

int main()
{
	scanf("%d%d", &n, &m);

	for(i = 1; i <= n; i++)
	{
		scanf("%d", &A[i].first);
		A[i].second = 1;
		r = insert(A, left, right, height, buffer1, buffer2, i, r);
	}

	for(i = 1; i <= m; i++)
	{
		scanf("%d", &A[i + n].first);
		A[i + n].second = 2;
		r = insert(A, left, right, height, buffer1, buffer2, i + n, r);
	}

	scanf("%d", &q);

	while(q--)
	{
		scanf("%d%d%d", &a, &b, &c);
		r = del(A, left, right, height, buffer1, buffer2, (a - 1)*n + b, r);
		A[(a-1)*n+b].first = c;
		r = insert(A, left, right, height, buffer1, buffer2, (a - 1)*n + b, r);
		// for(i = 1; i <= n + m; i++)
		// {
		// 	printf("%d, first = %d, second = %d, left = %d, right = %d, buffer = %d, %d\n", i, A[i].first, A[i].second, left[i], right[i], buffer1[i], buffer2[i]);
		// }
		printf("%d\n", find(A, left, right, buffer1, buffer2, r, 0));
	}
	return 0;
}