#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cstdbool>
#include <cinttypes>

#define REPEAT(token, num) for (token = 0; token < num; token++)
static inline int64_t min(int64_t length1, int64_t length2) { return (length1 < length2) ? length1 : length2; }
static inline int64_t max(int64_t length1, int64_t length2) { return (length1 > length2) ? length1 : length2; }

//#define DEBUG
#ifdef DEBUG
#define PRINTF printf
#else
#define PRINTF(...)
#endif

typedef struct {
    int64_t cost;
    int64_t index;
} item_key;
typedef int64_t item_value;
typedef int64_t num_items;

int64_t compareKeys(item_key key1, item_key key2) {
    if (key1.cost != key2.cost) return key1.cost-key2.cost;
    return key1.index-key2.index;
}

struct treap {
    item_key key;
    item_value value;
    num_items priority;
    num_items size;
    num_items need;
    num_items excess;
    struct treap *left;
    struct treap *right;
};
struct treap_pair {
    struct treap *left;
    struct treap *right;
};

struct treap space[400000];
num_items numSpaceUsed;

num_items getTreapSize(struct treap *treap1) {
    if (treap1 == NULL) return 0;
    return treap1->size;
}

num_items getTreapNeed(struct treap *treap1) {
    if (treap1 == NULL) return 0;
    return treap1->need;
}

num_items getTreapExcess(struct treap *treap1) {
    if (treap1 == NULL) return 0;
    return treap1->excess;
}

void calcTreapProperties(struct treap *treap1) {
    if (treap1 != NULL) {
        treap1->size = 1+getTreapSize(treap1->left)+getTreapSize(treap1->right);

        num_items needLeft = getTreapNeed(treap1->left);
        num_items excessRight = getTreapExcess(treap1->right);
        if (treap1->value > 0) excessRight++;
        else needLeft++;

        num_items nodesMatched = needLeft;
        if (excessRight < nodesMatched) nodesMatched = excessRight;

        treap1->need = needLeft+getTreapNeed(treap1->right)-nodesMatched;
        treap1->excess = getTreapExcess(treap1->left)+excessRight-nodesMatched;
    }
}

struct treap *newTreap(item_key key, item_value value) {
    if (numSpaceUsed > 400000) fprintf(stderr, "Ran out of treap space"), exit(1);
    space[numSpaceUsed].priority = rand();
    space[numSpaceUsed].key = key;
    space[numSpaceUsed].value = value;
    space[numSpaceUsed].left = NULL;
    space[numSpaceUsed].right = NULL;
    calcTreapProperties(space+numSpaceUsed);
    numSpaceUsed++;
    return space+(numSpaceUsed-1);
}

struct treap *mergeTreaps(struct treap *left, struct treap *right) {
    if (left == NULL) return right;
    if (right == NULL) return left;
    if (left->priority > right->priority) {
        left->right = mergeTreaps(left->right, right);
        calcTreapProperties(left);
        return left;
    } else {
        right->left = mergeTreaps(left, right->left);
        calcTreapProperties(right);
        return right;
    }
}

//Split treap into a left-side with all elements less than or equal to key and a right-side with the rest of the elements.
//Note that if exclusive is true, then left-side will have all elements less than key and right-side will have all elements greater than or equal to key.
struct treap_pair splitByKey(struct treap *treap1, item_key key, bool exclusive) {
    struct treap_pair answer;
    if (treap1 == NULL) {
        answer.left = answer.right = NULL;
        return answer;
    }

    int compareKeysResult = compareKeys(key, treap1->key);
    //If the key is less than the key of the root, then all items less than or equal to key are on the left-hand side.
    if (exclusive ? (compareKeysResult <= 0) : (compareKeysResult < 0)) {
        answer = splitByKey(treap1->left, key, exclusive);
        //Since answer.left is the tree rooted at key, make answer.right, which has all elements greater than key but less than the key of the root, the new left node of the treap:
        treap1->left = answer.right;
        //After making this modification, treap1 becomes the right-side, which has all elements greater than key:
        answer.right = treap1;
    }
    //If the key is greater than or equal to the key of the root, then we know that the left-side and the root is composed of all elements less than or equal to key, so we need to search the right-hand side for any other elements which are less than or equal to key.
    else {
        answer = splitByKey(treap1->right, key, exclusive);
        //Now, we need to combine treap1 and its left-side with all of the elements in the right-side which are also less than or equal to key.
        //These elements are now in answer.left, so make answer.left the new right-side of treap:
        treap1->right = answer.left;
        //After making this modification, treap1 becomes the left-side, which has all elements less than or equal to key:
        answer.left = treap1;
    }
    calcTreapProperties(treap1);
    return answer;
}

bool addElementByKey(struct treap **treapPtr, item_key newKey, item_value newValue, bool exclusive) {
    //Split treap up into a left-side with all elements less than newKey:
    struct treap_pair parts = splitByKey(*treapPtr, newKey, false);
    //Make sure newKey is not already in the tree:
    if (exclusive) {
        struct treap_pair parts2 = splitByKey(parts.left, newKey, true);
        if (parts2.right != NULL) {
            *treapPtr = mergeTreaps(mergeTreaps(parts2.left, parts2.right), parts.right);
            return false;
        }
        parts.left = mergeTreaps(parts2.left, parts2.right);
    }

    //Create a new node:
    struct treap *storeTreap = newTreap(newKey, newValue);
    //Put the new node to the right of the left-side, and then merge the new left-side with the right-side:
    *treapPtr = mergeTreaps(mergeTreaps(parts.left, storeTreap), parts.right);

    return true;
}

bool removeElementByKey(struct treap **treapPtr, item_key oldKey) {
    //Split treap up into a left-side which has all elements up to oldKey:
    struct treap_pair parts = splitByKey(*treapPtr, oldKey, false);
    //Now, split the left-side up into a left-side which has all elements before oldKey and a right-side which has all elements equal to oldKey:
    struct treap_pair parts2 = splitByKey(parts.left, oldKey, true);
    //Record whether there were actually any elements with oldKey:
    bool actuallyRemoved = parts2.right != NULL;
    //Merge treaps back together without the elements equal to oldKey:
    *treapPtr = mergeTreaps(parts2.left, parts.right);

    return actuallyRemoved;
}

item_key getHighestNeed(struct treap *treapPtr) {
    num_items index = getTreapNeed(treapPtr)-1;

    while (true) {
        num_items needLeft = getTreapNeed(treapPtr->left);
        num_items excessRight = getTreapExcess(treapPtr->right);
        if (treapPtr->value > 0) excessRight++;
        else if (excessRight <= needLeft) needLeft++;
        if (excessRight < needLeft) needLeft -= excessRight;
        else needLeft = 0;

        //printf("%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 "\n", needLeft, excessRight, index, treapPtr->key.cost);

        if ((treapPtr->value == 0) && (excessRight == 0) && (needLeft == index+1)) return treapPtr->key;
        if (index >= needLeft) index -= needLeft, treapPtr = treapPtr->right;
        else treapPtr = treapPtr->left;
    }
}

typedef int64_t num_dishes;
typedef int64_t num_pupils;
typedef int64_t cost;
typedef int64_t num_queries;

num_dishes numDishes;
num_pupils numPupils;
num_queries numQueries;
cost dishes[300010], pupils[300010];
struct treap *dishesAndPupils;

num_dishes i;
num_pupils j, queryIndex;
item_key storeKey;
cost queryCost;
num_queries queryType;

int main() {
    scanf("%" PRId64 " %" PRId64, &numDishes, &numPupils);
    REPEAT(i, numDishes) {
        scanf("%" PRId64, dishes+i);

        storeKey.cost = dishes[i];
        storeKey.index = i;
        addElementByKey(&dishesAndPupils, storeKey, 0, false);
    }
    REPEAT(i, numPupils) {
        scanf("%" PRId64, pupils+i);

        storeKey.cost = pupils[i];
        storeKey.index = i+numDishes;
        addElementByKey(&dishesAndPupils, storeKey, 1, false);
    }

    scanf("%" PRId64, &numQueries);
    while (numQueries--) {
        scanf("%" PRId64 " %" PRId64 " %" PRId64, &queryType, &queryIndex, &queryCost);
        queryIndex--;
        storeKey.cost = (queryType == 1) ? dishes[queryIndex] : pupils[queryIndex];
        storeKey.index = (queryType == 1) ? queryIndex : (queryIndex+numDishes);
        bool success = removeElementByKey(&dishesAndPupils, storeKey);
        if (!success) puts("NOES"), exit(1);

        storeKey.cost = queryCost;
        if (queryType == 1) dishes[queryIndex] = queryCost;
        else pupils[queryIndex] = queryCost;
        addElementByKey(&dishesAndPupils, storeKey, (queryType == 2), false);
        //printf("%" PRId64 "\n", getTreapNeed(dishesAndPupils));

        if (getTreapNeed(dishesAndPupils) > 0) printf("%" PRId64 "\n", getHighestNeed(dishesAndPupils).cost);
        else puts("-1");
    }

    exit(0);
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cstdbool>
#include <cinttypes>

#define REPEAT(token, num) for (token = 0; token < num; token++)
static inline int64_t min(int64_t length1, int64_t length2) { return (length1 < length2) ? length1 : length2; }
static inline int64_t max(int64_t length1, int64_t length2) { return (length1 > length2) ? length1 : length2; }

//#define DEBUG
#ifdef DEBUG
#define PRINTF printf
#else
#define PRINTF(...)
#endif

typedef struct {
    int64_t cost;
    int64_t index;
} item_key;
typedef int64_t item_value;
typedef int64_t num_items;

int64_t compareKeys(item_key key1, item_key key2) {
    if (key1.cost != key2.cost) return key1.cost-key2.cost;
    return key1.index-key2.index;
}

struct treap {
    item_key key;
    item_value value;
    num_items priority;
    num_items size;
    num_items need;
    num_items excess;
    struct treap *left;
    struct treap *right;
};
struct treap_pair {
    struct treap *left;
    struct treap *right;
};

struct treap space[10000000];
num_items numSpaceUsed;

num_items getTreapSize(struct treap *treap1) {
    if (treap1 == NULL) return 0;
    return treap1->size;
}

num_items getTreapNeed(struct treap *treap1) {
    if (treap1 == NULL) return 0;
    return treap1->need;
}

num_items getTreapExcess(struct treap *treap1) {
    if (treap1 == NULL) return 0;
    return treap1->excess;
}

void calcTreapProperties(struct treap *treap1) {
    if (treap1 != NULL) {
        treap1->size = 1+getTreapSize(treap1->left)+getTreapSize(treap1->right);

        num_items needLeft = getTreapNeed(treap1->left);
        num_items excessRight = getTreapExcess(treap1->right);
        if (treap1->value > 0) excessRight++;
        else needLeft++;

        num_items nodesMatched = needLeft;
        if (excessRight < nodesMatched) nodesMatched = excessRight;

        treap1->need = needLeft+getTreapNeed(treap1->right)-nodesMatched;
        treap1->excess = getTreapExcess(treap1->left)+excessRight-nodesMatched;
    }
}

struct treap *newTreap(item_key key, item_value value) {
    if (numSpaceUsed >= 10000000) fprintf(stderr, "Ran out of treap space"), exit(1);
    space[numSpaceUsed].priority = rand();
    space[numSpaceUsed].key = key;
    space[numSpaceUsed].value = value;
    space[numSpaceUsed].left = NULL;
    space[numSpaceUsed].right = NULL;
    calcTreapProperties(space+numSpaceUsed);
    numSpaceUsed++;
    return space+(numSpaceUsed-1);
}

struct treap *mergeTreaps(struct treap *left, struct treap *right) {
    if (left == NULL) return right;
    if (right == NULL) return left;
    if (left->priority > right->priority) {
        left->right = mergeTreaps(left->right, right);
        calcTreapProperties(left);
        return left;
    } else {
        right->left = mergeTreaps(left, right->left);
        calcTreapProperties(right);
        return right;
    }
}

//Split treap into a left-side with all elements less than or equal to key and a right-side with the rest of the elements.
//Note that if exclusive is true, then left-side will have all elements less than key and right-side will have all elements greater than or equal to key.
struct treap_pair splitByKey(struct treap *treap1, item_key key, bool exclusive) {
    struct treap_pair answer;
    if (treap1 == NULL) {
        answer.left = answer.right = NULL;
        return answer;
    }

    int compareKeysResult = compareKeys(key, treap1->key);
    //If the key is less than the key of the root, then all items less than or equal to key are on the left-hand side.
    if (exclusive ? (compareKeysResult <= 0) : (compareKeysResult < 0)) {
        answer = splitByKey(treap1->left, key, exclusive);
        //Since answer.left is the tree rooted at key, make answer.right, which has all elements greater than key but less than the key of the root, the new left node of the treap:
        treap1->left = answer.right;
        //After making this modification, treap1 becomes the right-side, which has all elements greater than key:
        answer.right = treap1;
    }
    //If the key is greater than or equal to the key of the root, then we know that the left-side and the root is composed of all elements less than or equal to key, so we need to search the right-hand side for any other elements which are less than or equal to key.
    else {
        answer = splitByKey(treap1->right, key, exclusive);
        //Now, we need to combine treap1 and its left-side with all of the elements in the right-side which are also less than or equal to key.
        //These elements are now in answer.left, so make answer.left the new right-side of treap:
        treap1->right = answer.left;
        //After making this modification, treap1 becomes the left-side, which has all elements less than or equal to key:
        answer.left = treap1;
    }
    calcTreapProperties(treap1);
    return answer;
}

bool addElementByKey(struct treap **treapPtr, item_key newKey, item_value newValue, bool exclusive) {
    //Split treap up into a left-side with all elements less than newKey:
    struct treap_pair parts = splitByKey(*treapPtr, newKey, false);
    //Make sure newKey is not already in the tree:
    if (exclusive) {
        struct treap_pair parts2 = splitByKey(parts.left, newKey, true);
        if (parts2.right != NULL) {
            *treapPtr = mergeTreaps(mergeTreaps(parts2.left, parts2.right), parts.right);
            return false;
        }
        parts.left = mergeTreaps(parts2.left, parts2.right);
    }

    //Create a new node:
    struct treap *storeTreap = newTreap(newKey, newValue);
    //Put the new node to the right of the left-side, and then merge the new left-side with the right-side:
    *treapPtr = mergeTreaps(mergeTreaps(parts.left, storeTreap), parts.right);

    return true;
}

bool removeElementByKey(struct treap **treapPtr, item_key oldKey) {
    //Split treap up into a left-side which has all elements up to oldKey:
    struct treap_pair parts = splitByKey(*treapPtr, oldKey, false);
    //Now, split the left-side up into a left-side which has all elements before oldKey and a right-side which has all elements equal to oldKey:
    struct treap_pair parts2 = splitByKey(parts.left, oldKey, true);
    //Record whether there were actually any elements with oldKey:
    bool actuallyRemoved = parts2.right != NULL;
    //Merge treaps back together without the elements equal to oldKey:
    *treapPtr = mergeTreaps(parts2.left, parts.right);

    return actuallyRemoved;
}

item_key getHighestNeed(struct treap *treapPtr) {
    num_items index = getTreapNeed(treapPtr)-1;

    while (true) {
        num_items needLeft = getTreapNeed(treapPtr->left);
        num_items excessRight = getTreapExcess(treapPtr->right);
        if (treapPtr->value > 0) excessRight++;
        else if (excessRight <= needLeft) needLeft++;
        if (excessRight < needLeft) needLeft -= excessRight;
        else needLeft = 0;

        //printf("%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 "\n", needLeft, excessRight, index, treapPtr->key.cost);

        if ((treapPtr->value == 0) && (excessRight == 0) && (needLeft == index+1)) return treapPtr->key;
        if (index >= needLeft) index -= needLeft, treapPtr = treapPtr->right;
        else treapPtr = treapPtr->left;
    }
}

typedef int64_t num_dishes;
typedef int64_t num_pupils;
typedef int64_t cost;
typedef int64_t num_queries;

num_dishes numDishes;
num_pupils numPupils;
num_queries numQueries;
cost dishes[300010], pupils[300010];
struct treap *dishesAndPupils;

num_dishes i;
num_pupils j, queryIndex;
item_key storeKey;
cost queryCost;
num_queries queryType;

int main() {
    scanf("%" PRId64 " %" PRId64, &numDishes, &numPupils);
    REPEAT(i, numDishes) {
        scanf("%" PRId64, dishes+i);

        storeKey.cost = dishes[i];
        storeKey.index = i;
        addElementByKey(&dishesAndPupils, storeKey, 0, false);
    }
    REPEAT(i, numPupils) {
        scanf("%" PRId64, pupils+i);

        storeKey.cost = pupils[i];
        storeKey.index = i+numDishes;
        addElementByKey(&dishesAndPupils, storeKey, 1, false);
    }

    scanf("%" PRId64, &numQueries);
    while (numQueries--) {
        scanf("%" PRId64 " %" PRId64 " %" PRId64, &queryType, &queryIndex, &queryCost);
        queryIndex--;
        storeKey.cost = (queryType == 1) ? dishes[queryIndex] : pupils[queryIndex];
        storeKey.index = (queryType == 1) ? queryIndex : (queryIndex+numDishes);
        bool success = removeElementByKey(&dishesAndPupils, storeKey);
        if (!success) puts("NOES"), exit(1);

        storeKey.cost = queryCost;
        if (queryType == 1) dishes[queryIndex] = queryCost;
        else pupils[queryIndex] = queryCost;
        addElementByKey(&dishesAndPupils, storeKey, (queryType == 2), false);
        //printf("%" PRId64 "\n", getTreapNeed(dishesAndPupils));

        if (getTreapNeed(dishesAndPupils) > 0) printf("%" PRId64 "\n", getHighestNeed(dishesAndPupils).cost);
        else puts("-1");
    }

    exit(0);
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
#define ll long long
const int N=3e5+5;
int a[N],b[N],t[N],v[3*N],p[N],x[N];
int ad[N*4],tree[N*4];
int n,m,q,sum;
ll rd()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
void init()
{
	n=rd();m=rd();sum=0;
	rep(i,1,n) a[i]=rd(),v[++sum]=a[i];
	rep(i,1,m) b[i]=rd(),v[++sum]=b[i];
	q=rd();
	rep(i,1,q)
	{
		t[i]=rd();
		if(t[i]<=2)
		{
			p[i]=rd(),x[i]=rd(),v[++sum]=x[i];
		}
	}
	sort(v+1,v+1+sum);
	sum = unique(v+1, v + sum+1) - v-1;
	rep(i,1,n) a[i] = lower_bound(v+1,v + sum+1,a[i]) -v;
	rep(i,1,m) b[i] = lower_bound(v+1,v + sum+1,b[i]) -v;
    rep(i,1,q)
		if(t[i]<=2)
			x[i] = lower_bound(v+1,v + sum+1,x[i]) -v;
	//rep(i,1,sum) printf(" %d\n",v[i]);
}
void down(int rt)
{
	ad[rt*2]+=ad[rt];
	ad[rt*2+1]+=ad[rt];
	tree[rt*2]+=ad[rt];
	tree[rt*2+1]+=ad[rt];
	ad[rt]=0;
}
void upd(int rt,int L,int R,int l,int r,int x)
{
	if(l>R || r<L) return;
	if(l<=L&&R<=r)
	{
		tree[rt]+=x;
		ad[rt]+=x;
		return;
	}
	int mid=(L+R)/2;
	down(rt);
	upd(rt*2,L,mid,l,r,x);
	upd(rt*2+1,mid+1,R,l,r,x);
	tree[rt]=max(tree[rt*2],tree[rt*2+1]);
}
void add(int x)
{
	upd(1,1,sum,0,x,1);
}
void del(int x)
{
	upd(1,1,sum,0,x,-1);
}
int getit(int rt,int l,int r)
{
	if(l==r) return l;
	int m=(l+r)/2;
	down(rt);
	if(tree[2*rt+1]>0) return getit(2*rt+1,m+1,r);
	else return getit(2*rt,l,m);
}
int get()
{
	if(tree[1]>=1) return v[getit(1,1,sum)];
	else return -1;
}
void work()
{
	rep(i,1,n) add(a[i]);
	rep(i,1,m) del(b[i]);
	rep(i,1,q)
	{
		if(t[i]==1)
		{
			del(a[p[i]]);
			a[p[i]]=x[i];
			add(x[i]);
		}
		if(t[i]==2)
		{
			add(b[p[i]]);
			b[p[i]]=x[i];
			del(x[i]);
		}
		printf("%d\n",get());
	}
}
int main()
{
	init();
	work();
	//outit();
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
#define ll long long
const int N=3e5+5;
int a[N],b[N],t[N],v[3*N],p[N],x[N];
int ad[N*4],tree[N*4];
int n,m,q,sum;
ll rd()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
void init()
{
	n=rd();m=rd();sum=0;
	rep(i,1,n) a[i]=rd(),v[++sum]=a[i];
	rep(i,1,m) b[i]=rd(),v[++sum]=b[i];
	q=rd();
	rep(i,1,q)
	{
		t[i]=rd();
		if(t[i]<=2)
		{
			p[i]=rd(),x[i]=rd(),v[++sum]=x[i];
		}
	}
	sort(v+1,v+1+sum);
	sum = unique(v+1, v + sum+1) - v-1;
	rep(i,1,n) a[i] = lower_bound(v+1,v + sum+1,a[i]) -v;
	rep(i,1,m) b[i] = lower_bound(v+1,v + sum+1,b[i]) -v;
    rep(i,1,q)
		if(t[i]<=2)
			x[i] = lower_bound(v+1,v + sum+1,x[i]) -v;
	//rep(i,1,sum) printf(" %d\n",v[i]);
}
void down(int rt)
{
	ad[rt*2]+=ad[rt];
	ad[rt*2+1]+=ad[rt];
	tree[rt*2]+=ad[rt];
	tree[rt*2+1]+=ad[rt];
	ad[rt]=0;
}
void upd(int rt,int L,int R,int l,int r,int x)
{
	if(l>R || r<L) return;
	if(l<=L&&R<=r)
	{
		tree[rt]+=x;
		ad[rt]+=x;
		return;
	}
	int mid=(L+R)/2;
	down(rt);
	upd(rt*2,L,mid,l,r,x);
	upd(rt*2+1,mid+1,R,l,r,x);
	tree[rt]=max(tree[rt*2],tree[rt*2+1]);
}
void add(int x)
{
	upd(1,0,sum,0,x,1);
}
void del(int x)
{
	upd(1,0,sum,0,x,-1);
}
int getit(int rt,int l,int r)
{
	if(l==r) return l;
	int m=(l+r)/2;
	down(rt);
	if(tree[2*rt+1]>0) return getit(2*rt+1,m+1,r);
	else return getit(2*rt,l,m);
}
int get()
{
	if(tree[1]>=1) return v[getit(1,1,sum)];
	else return -1;
}
void work()
{
	rep(i,1,n) add(a[i]);
	rep(i,1,m) del(b[i]);
	rep(i,1,q)
	{
		if(t[i]==1)
		{
			del(a[p[i]]);
			a[p[i]]=x[i];
			add(x[i]);
		}
		if(t[i]==2)
		{
			add(b[p[i]]);
			b[p[i]]=x[i];
			del(x[i]);
		}
		printf("%d\n",get());
	}
}
int main()
{
	init();
	work();
	//outit();
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
#define ll long long
const int N=3e5+5;
int a[N],b[N],t[N],v[3*N],p[N],x[N];
int ad[N*4],tree[N*4];
int n,m,q,sum;
ll rd()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
void init()
{
	n=rd();m=rd();sum=0;
	rep(i,1,n) a[i]=rd(),v[++sum]=a[i];
	rep(i,1,m) b[i]=rd(),v[++sum]=b[i];
	q=rd();
	rep(i,1,q)
	{
		t[i]=rd();
		if(t[i]<=2)
		{
			p[i]=rd(),x[i]=rd(),v[++sum]=x[i];
		}
	}
	sort(v+1,v+1+sum);
	sum = unique(v+1, v + sum+1) - v-1;
	rep(i,1,n) a[i] = lower_bound(v+1,v + sum+1,a[i]) -v;
	rep(i,1,m) b[i] = lower_bound(v+1,v + sum+1,b[i]) -v;
    rep(i,1,q)
		if(t[i]<=2)
			x[i] = lower_bound(v+1,v + sum+1,x[i]) -v;
	//rep(i,1,sum) printf(" %d\n",v[i]);
}
void down(int rt)
{
	ad[rt*2]+=ad[rt];
	ad[rt*2+1]+=ad[rt];
	tree[rt*2]+=ad[rt];
	tree[rt*2+1]+=ad[rt];
	ad[rt]=0;
}
void upd(int rt,int L,int R,int l,int r,int x)
{
	if(l>R || r<L) return;
	if(l<=L&&R<=r)
	{
		tree[rt]+=x;
		ad[rt]+=x;
		return;
	}
	int mid=(L+R)/2;
	down(rt);
	upd(rt*2,L,mid,l,r,x);
	upd(rt*2+1,mid+1,R,l,r,x);
	tree[rt]=max(tree[rt*2],tree[rt*2+1]);
}
void add(int x)
{
	upd(1,0,sum,0,x,1);
}
void del(int x)
{
	upd(1,0,sum,0,x,-1);
}
int getit(int rt,int l,int r)
{
	if(l==r) return l;
	int m=(l+r)/2;
	down(rt);
	if(tree[2*rt+1]>0) return getit(2*rt+1,m+1,r);
	else return getit(2*rt,l,m);
}
int get()
{
	if(tree[1]>=1) return getit(1,1,sum);
	else return -1;
}
void work()
{
	rep(i,1,n) add(a[i]);
	rep(i,1,m) del(b[i]);
	rep(i,1,q)
	{
		if(t[i]==1)
		{
			del(a[p[i]]);
			a[p[i]]=x[i];
			add(x[i]);
		}
		if(t[i]==2)
		{
			add(b[p[i]]);
			b[p[i]]=x[i];
			del(x[i]);
		}
		printf("%d\n",get());
	}
}
int main()
{
	init();
	work();
	//outit();
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
#define ll long long
const int N=3e5+5;
int a[N],b[N],t[N],v[3*N],p[N],x[N];
int ad[N*10],tree[N*10];
int n,m,q,sum;
ll rd()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
void init()
{
	n=rd();m=rd();sum=0;
	rep(i,1,n) a[i]=rd(),v[++sum]=a[i];
	rep(i,1,m) b[i]=rd(),v[++sum]=b[i];
	q=rd();
	rep(i,1,q)
	{
		t[i]=rd();
		if(t[i]<=2)
		{
			p[i]=rd(),x[i]=rd(),v[++sum]=x[i];
		}
	}
	sort(v+1,v+1+sum);
	sum = unique(v+1, v + sum+1) - v-1;
	rep(i,1,n) a[i] = lower_bound(v+1,v + sum+1,a[i]) -v;
	rep(i,1,m) b[i] = lower_bound(v+1,v + sum+1,b[i]) -v;
    rep(i,1,q)
		if(t[i]<=2)
			x[i] = lower_bound(v+1,v + sum+1,x[i]) -v;
	//rep(i,1,sum) printf(" %d\n",v[i]);
}
void down(int rt)
{
	ad[rt*2]+=ad[rt];
	ad[rt*2+1]+=ad[rt];
	tree[rt*2]+=ad[rt];
	tree[rt*2+1]+=ad[rt];
	ad[rt]=0;
}
void upd(int rt,int L,int R,int l,int r,int x)
{
	if(l>R || r<L) return;
	if(l<=L&&R<=r)
	{
		tree[rt]+=x;
		ad[rt]+=x;
		return;
	}
	int mid=(L+R)/2;
	down(rt);
	upd(rt*2,L,mid,l,r,x);
	upd(rt*2+1,mid+1,R,l,r,x);
	tree[rt]=max(tree[rt*2],tree[rt*2+1]);
}
void add(int x)
{
	upd(1,1,sum,0,x,1);
}
void del(int x)
{
	upd(1,1,sum,0,x,-1);
}
int getit(int rt,int l,int r)
{
	if(l==r) return l;
	int m=(l+r)/2;
	down(rt);
	if(tree[2*rt+1]>0) return getit(2*rt+1,m+1,r);
	else return getit(2*rt,l,m);
}
int get()
{
	if(tree[1]>=1) return v[getit(1,1,sum)];
	else return -1;
}
void work()
{
	rep(i,1,n) add(a[i]);
	rep(i,1,m) del(b[i]);
	rep(i,1,q)
	{
		if(t[i]==1)
		{
			del(a[p[i]]);
			a[p[i]]=x[i];
			add(x[i]);
		}
		if(t[i]==2)
		{
			add(b[p[i]]);
			b[p[i]]=x[i];
			del(x[i]);
		}
		printf("%d\n",get());
	}
}
int main()
{
	init();
	work();
	//outit();
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define maxn (1000001)
#define lson l,m,o<<1
#define rson m+1,r,o<<1|1
int data[maxn<<2][3], a[maxn], b[maxn], x[maxn], y[maxn];
inline void push_up(int o) {
	data[o][1] = data[o<<1][1] + data[o<<1|1][1];
	data[o][2] = data[o<<1][2] + data[o<<1|1][2];
	data[o][0] = data[o<<1|1][0] + max(0, data[o<<1][0] - (data[o<<1|1][2] - (data[o<<1|1][1] - data[o<<1|1][0])));
}
void build(int l, int r, int o) {
	//printf("build [%d, %d], %d\n", l, r, o);
	if(l == r) {
		data[o][1] = a[r];
		data[o][2] = b[r];
		data[o][0] = max(0, data[o][1] - data[o][2]);
	} else {
		int m = (l + r) >> 1;
		build(lson); build(rson);
		push_up(o);
	}
}
void update(int i, int x, int v, int l, int r, int o) {
	//printf("[%d, %d] node[%d] before update data[0] = %d, data[1] = %d, data[2] = %d\n", l, r, o, data[o][0], data[o][1], data[o][2]);
	if(l == r) {
		data[o][i] += v;
		data[o][0] = max(0, data[o][1] - data[o][2]);
	} else {

		int m = (l + r) >> 1;
		if(x <= m) update(i, x, v, lson);
		else update(i, x, v, rson);
		push_up(o);
	}
	//printf("[%d, %d] node[%d] AFTER update data[0] = %d, data[1] = %d, data[2] = %d\n", l, r, o, data[o][0], data[o][1], data[o][2]);
}
int query(int s, int l, int r, int o) {
	if(s - data[o][0] >= 0) {
		return -1;
	}
	if(l == r) {
		return l;
	} else {
		int m = (l + r) >> 1;
		int rem = data[o<<1|1][2] - (data[o<<1|1][1] - data[o<<1|1][0]);
		if(s >= data[o<<1|1][0]) return query(s - data[o<<1|1][0] + rem, lson);
		else return query(s, rson);
	}
}

int main()
{
	int n, m; cin >> n >> m;
	for(int i = 1; i <= n; i++) {
		scanf("%d", &x[i]);
		a[x[i]]++;
	}
	for(int i = 1; i <= m; i++) {
		scanf("%d", &y[i]);
		b[y[i]]++;
	}
	build(1, 1e6, 1);
	int q; cin >> q;
	for(int i = 0, op, idx, v; i < q; i++) {
		scanf("%d%d%d", &op, &idx, &v);
		// printf("in: %d %d %d\n", op, idx, v);

		if(op == 1) {
			// printf("x[%d] = %d\n", idx, x[idx]);
			// printf("update(%d, %d, %d, rt)\n", op, x[idx], -1);
			update(op, x[idx], -1, 1, 1e6, 1);
			x[idx] = v;
			// printf("x[%d] = %d\n", idx, x[idx]);
			// printf("update(%d, %d, %d, rt)\n", op, x[idx], 1);
			update(op, x[idx], 1, 1, 1e6, 1);
		} else {
			// printf("y[%d] = %d\n", idx, y[idx]);
			// printf("update(%d, %d, %d, rt)\n", op, y[idx], -1);
			update(op, y[idx], -1, 1, 1e6, 1);
			y[idx] = v;
			// printf("y[%d] = %d\n", idx, y[idx]);
			// printf("update(%d, %d, %d, rt)\n", op, y[idx], 1);
			update(op, y[idx], 1, 1, 1e6, 1);
		}
		// printf("update(%d, %d, %d, rt)\n", op, ((op > 1)? b[x]: a[x]), -1);
		// if(op > 1) printf("b[%d] = %d\n", x, b[x]);
		// else printf("a[%d] = %d\n", x, a[x]);
		// update(op, ((op > 1)? b[x]: a[x]), -1, 1, 1e6, 1);

		// printf("update(%d, %d, %d, rt)\n", op, v, 1);
		// update(op, v, 1, 1, 1e6, 1);
		printf("%d\n", query(0, 1, 1e6, 1));
	}

	return 0;
}


/*
 * 	The way if it's all predetermined
 * 		And the way i should go all my life
 * 			I swear to go wherever will be
 * 				'Cause there'll be something to see and to find
 */
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2,tune=native")
#include <bits/stdc++.h>
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
#ifdef rd
#define trace(...) cout<<"Line:"<<__LINE__<<" "; __f(#__VA_ARGS__, __VA_ARGS__)
template<typename Arg1>
void __f(const char* name, Arg1&& arg1) {
	cout<<name<<" : "<<arg1<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
	const char* comma=strchr(names+1,',');
	cout.write(names,comma-names)<<" : "<<arg1<<" | ";
	__f(comma+1,args...);
}
#else
#define trace(...)
#define endl '\n'
#endif
#define pb push_back
#define fi first
#define se second
#define int long long
typedef long long ll;
typedef double f80;
#define double long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define sz(x) ((long long)x.size())
#define fr(a,b,c) for(int a=b; a<=c; a++)
#define rep(a,b,c) for(int a=b; a<c; a++)
#define trav(a,x) for(auto &a:x)
#define all(con) con.begin(),con.end()
const ll infl=4e18;
const int infi=1e9;
//const int mod=998244353;
const int mod=1000003;
typedef vector<int> vi;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
auto clk=clock();
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
	uniform_int_distribution<int> uid(0,lim-1);
	return uid(rang);
}
int powm(ll a, ll b) {
	int res=1;
	while(b) {
		if(b&1)
			res=(res*a)%mod;
		a=(a*a)%mod;
		b>>=1;
	}
	return res;
}

const int N=1e6+5;
const int N2=1e6;
int tr[4*N],lazy[4*N];
int a[N],b[N];
void pushdown(int seg) {
	if(lazy[seg]) {
		tr[seg<<1]+=lazy[seg];
		tr[seg<<1|1]+=lazy[seg];
		lazy[seg<<1]+=lazy[seg];
		lazy[seg<<1|1]+=lazy[seg];
		lazy[seg]=0;
	}
}
void update(int seg, int ll, int rr, int l, int r, int val) {
	if(rr<l||ll>r) {
		return;
	}
	if(rr<=r&&l<=ll) {
		lazy[seg]+=val;
		tr[seg]+=val;
		return;
	}
	pushdown(seg);
	update(seg<<1,ll,(ll+rr)/2,l,r,val);
	update(seg<<1|1,1+(ll+rr)/2,rr,l,r,val);
	tr[seg]=min(tr[seg<<1],tr[seg<<1|1]);
}
int query(int seg, int ll, int rr) {
	if(tr[seg]>=0)
		return -1;
	if(ll==rr)
		return ll;
	pushdown(seg);
	int qu=query(seg<<1|1,(ll+rr)/2+1,rr);
	if(qu==-1) {
		return query(seg<<1,ll,(ll+rr)/2);
	}
	return qu;
}
void solve() {
	int n,m;
	cin>>n>>m;
	fr(i,1,n) {
		cin>>a[i];
		update(1,1,N2,1,a[i],-1);
	}
	fr(i,1,m) {
		cin>>b[i];
		update(1,1,N2,1,b[i],1);
	}
	int q;
	cin>>q;
	while(q--) {
		int ty;
		cin>>ty;
		if(ty==1) {
			int i,x;
			cin>>i>>x;
			update(1,1,N2,1,a[i],1);
			a[i]=x;
			update(1,1,N2,1,a[i],-1);
		} else {
			int i,x;
			cin>>i>>x;
			update(1,1,N2,1,b[i],-1);
			b[i]=x;
			update(1,1,N2,1,b[i],1);
		}
//		cout<<tr[1]<<endl;
		cout<<query(1,1,N2)<<endl;
	}

}

signed main() {
	ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	srand(chrono::high_resolution_clock::now().time_since_epoch().count());
	cout<<fixed<<setprecision(10);
	int t=1;
//	cin>>t;
	fr(i,1,t)
		solve();
#ifdef rd
	fflush(stdout);
	cerr<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define mem(i,j) memset(i,j,sizeof(i))
#define inc(i,j,k) for(int i=j;i<=k;i++)
#define dec(i,j,k) for(int i=j;i>=k;i--)
const int N=1e6+5;
const int UP=1e6;

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define root 1,UP,1
int MAX[N<<2], LA[N<<2];
void pushUp(int rt) {
    MAX[rt]=max(MAX[rt<<1],MAX[rt<<1|1]);
}
void pushDown(int rt) {
    if(LA[rt]) {
        LA[rt<<1]+=LA[rt];
        MAX[rt<<1]+=LA[rt];
        LA[rt<<1|1]+=LA[rt];
        MAX[rt<<1|1]+=LA[rt];
        LA[rt]=0;
    }
}
void update(int L,int R,int x,int l,int r,int rt) {
    if(L<=l && r<=R) {
        LA[rt]+=x; MAX[rt]+=x;
        return ;
    }
    pushDown(rt);
    int m=(l+r)>>1;
    if(L<=m) update(L,R,x,lson);
    if(m<R) update(L,R,x,rson);
    pushUp(rt);
}
int query(int L,int R,int l,int r,int rt) {
    if(MAX[rt]<=0) return -1;
    if(l>=r) return l;
    pushDown(rt);
    int m=(l+r)>>1;

    if(MAX[rt<<1|1]>0) return query(L,R,rson);
    else return query(L,R,lson);
    // 改变查询方法
    // 查询记录大于0 且 位置最右（也就是最大）

}

int n,a[N],m,b[N];

int main()
{
    while(~scanf("%d%d",&n,&m)) {
        inc(i,1,n) scanf("%d",&a[i]) ;
        inc(i,1,m) scanf("%d",&b[i]) ;
        mem(MAX,0); mem(LA,0);
        inc(i,1,n) update(1,a[i],1,root);
        inc(i,1,m) update(1,b[i],-1,root);

        int q; scanf("%d",&q);
        while(q--) {
            int op,id,x; scanf("%d%d%d",&op,&id,&x);
            if(op==1) {
                update(1,a[id],-1,root);
                update(1,a[id]=x,1,root);
            } else {
                update(1,b[id],1,root);
                update(1,b[id]=x,-1,root);
            }
            printf("%d\n",query(1,UP,root));
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define mem(i,j) memset(i,j,sizeof(i))
#define inc(i,j,k) for(int i=j;i<=k;i++)
#define dec(i,j,k) for(int i=j;i>=k;i--)
const int N=1e6+5;
const int UP=1e6;

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define root 1,UP,1
int MAX[N<<2], LA[N<<2];
void pushUp(int rt) {
    MAX[rt]=max(MAX[rt<<1],MAX[rt<<1|1]);
}
void pushDown(int rt) {
    if(LA[rt]) {
        LA[rt<<1]+=LA[rt];
        MAX[rt<<1]+=LA[rt];
        LA[rt<<1|1]+=LA[rt];
        MAX[rt<<1|1]+=LA[rt];
        LA[rt]=0;
    }
}
void update(int L,int R,int x,int l,int r,int rt) {
    if(L<=l && r<=R) {
        LA[rt]+=x; MAX[rt]+=x;
        return ;
    }
    pushDown(rt);
    int m=(l+r)>>1;
    if(L<=m) update(L,R,x,lson);
    if(m<R) update(L,R,x,rson);
    pushUp(rt);
}
int query(int L,int R,int l,int r,int rt) {
    if(MAX[rt]<=0) return -1;
    if(l>=r) return l;
    pushDown(rt);
    int m=(l+r)>>1;

    if(MAX[rt<<1|1]) return query(L,R,rson);
    else return query(L,R,lson);
    // 改变查询方法
    // 查询记录大于0 且 位置最右（也就是最大）

}

int n,a[N],m,b[N];

int main()
{
    while(~scanf("%d%d",&n,&m)) {
        inc(i,1,n) scanf("%d",&a[i]) ;
        inc(i,1,m) scanf("%d",&b[i]) ;
        mem(MAX,0); mem(LA,0);
        inc(i,1,n) update(1,a[i],1,root);
        inc(i,1,m) update(1,b[i],-1,root);

        int q; scanf("%d",&q);
        while(q--) {
            int op,id,x; scanf("%d%d%d",&op,&id,&x);
            if(op==1) {
                update(1,a[id],-1,root);
                update(1,a[id]=x,1,root);
            } else {
                update(1,b[id],1,root);
                update(1,b[id]=x,-1,root);
            }
            printf("%d\n",query(1,UP,root));
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FOR(i, n) for(int i=0; i<int(n); i++)
#define RNG(i, f, t) for(int i=int(f); i<=int(t); i++)
bool RELAXED = false;
template<int,typename T> struct MINMAX{ T val; MINMAX(T val) : val(val) {} };
template<typename T> MINMAX<1,T> MAX(T val) { return MINMAX<1,T>(val); };
template<typename T> MINMAX<2,T> MIN(T val) { return MINMAX<2,T>(val); };
template<typename T, typename U> inline T& operator|=(T &lhs, MINMAX<1,U> rhs) { return lhs = (rhs.val > lhs ? (RELAXED=true,rhs.val) : (RELAXED=false,lhs)); }
template<typename T, typename U> inline T& operator|=(T &lhs, MINMAX<2,U> rhs) { return lhs = (rhs.val < lhs ? (RELAXED=true,rhs.val) : (RELAXED=false,lhs)); }
template<typename T> inline vector<T> readVector(int n) { vector<T> vec(n); FOR(i,n) cin>>vec[i]; return vec; }
template<typename T> void EXIT(T answer) { cout<<answer<<endl; exit(0); }
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef signed long long int ll;

struct SegTree {
    static const int INF = 1<<30;
    static const int H = 20;
    static const int N = 1<<H;
    int t[2*N]={0};
    int lazy[N]={0};

    void update(int ID) {
        t[ID] = min(t[ID*2], t[ID*2+1]) + lazy[ID];
    }

    void propagate(int ID) {
        if(ID>=N || lazy[ID]==0) return;
        if(ID*2 >= N) {
            t[ID*2] += lazy[ID];
            t[ID*2+1] += lazy[ID];
        } else
        {
            lazy[ID*2] += lazy[ID];
            lazy[ID*2+1] += lazy[ID];
            update(ID*2);
            update(ID*2+1);
        }
        lazy[ID] = 0;
    }

    void incrementRange(int from, int to, int val, int ID=1, int L=0, int R=N-1) {
        if(L==R) {
            t[ID]+=val;
            return;
        }
        if(from<=L && to>=R) {
            lazy[ID] += val;
            t[ID] += val;
            return;
        }
        propagate(ID);
        int mid = L+(R-L)/2;
        if(from<=mid)
            incrementRange(from, to, val, ID*2, L, mid);
        if(to>mid)
            incrementRange(from, to, val, ID*2+1, mid+1, R);
        update(ID);
    }

    int queryRMN(int from, int to, int ID=1, int L=0, int R= N - 1) {
        if(L==R)
            return t[ID] < 0 ? L : INF;
        propagate(ID);
        int mid = L+(R-L)/2;
        if(to>=mid && t[ID*2+1]<0)
            return queryRMN(from, to, ID * 2 + 1, mid + 1, R);
        else if(from<=mid)
            return queryRMN(from, to, ID * 2, L, mid);
        return INF;
    }
};

SegTree st;

int main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr);

    int n,m; cin>>n>>m;
    vi dishes = readVector<int>(n);
    vi pupils = readVector<int>(m);

    for(auto e : dishes) {
        st.incrementRange(0,e,-1);
    }
    for(auto e : pupils) {
        st.incrementRange(0,e,1);
    }

    int q; cin>>q;
    FOR(i,q) {
        int type, pos, x; cin>>type>>pos>>x;
        pos--;
        if(type==1) {
            st.incrementRange(0, dishes[pos], 1);
            st.incrementRange(0, x, -1);
            dishes[pos] = x;
        }
        else {
            st.incrementRange(0, pupils[pos], -1);
            st.incrementRange(0, x, 1);
            pupils[pos] = x;
        }
        int res = st.queryRMN(0, 1e6);
        cout<<(res==SegTree::INF ? -1 : res)<<endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FOR(i, n) for(int i=0; i<int(n); i++)
#define RNG(i, f, t) for(int i=int(f); i<=int(t); i++)
bool RELAXED = false;
template<int,typename T> struct MINMAX{ T val; MINMAX(T val) : val(val) {} };
template<typename T> MINMAX<1,T> MAX(T val) { return MINMAX<1,T>(val); };
template<typename T> MINMAX<2,T> MIN(T val) { return MINMAX<2,T>(val); };
template<typename T, typename U> inline T& operator|=(T &lhs, MINMAX<1,U> rhs) { return lhs = (rhs.val > lhs ? (RELAXED=true,rhs.val) : (RELAXED=false,lhs)); }
template<typename T, typename U> inline T& operator|=(T &lhs, MINMAX<2,U> rhs) { return lhs = (rhs.val < lhs ? (RELAXED=true,rhs.val) : (RELAXED=false,lhs)); }
template<typename T> inline vector<T> readVector(int n) { vector<T> vec(n); FOR(i,n) cin>>vec[i]; return vec; }
template<typename T> void EXIT(T answer) { cout<<answer<<endl; exit(0); }
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef signed long long int ll;

struct SegTree {
#define IS_LEAF(id) (id>=N)
#define LC (ID<<1)
#define RC (ID<<1|1)
    static const int INF = 1<<30;
    static const int H = 20;
    static const int N = 1<<H;
    int t[2*N]={0};
    int lazy[N]={0};

    void update(int ID) {
        t[ID] = min(t[LC], t[RC]) + lazy[ID];
    }

    void propagate(int ID) {
        if(IS_LEAF(ID) || lazy[ID]==0) return;
        if(!IS_LEAF(LC)) {
            lazy[LC] += lazy[ID];
            lazy[RC] += lazy[ID];
        }
        t[LC] += lazy[ID];
        t[RC] += lazy[ID];
        lazy[ID] = 0;
    }

    void incrementRange(int from, int to, int val, int ID=1, int L=0, int R=N-1) {
        if(from<=L && to>=R) {
            if(!IS_LEAF(ID)) lazy[ID] += val;
            t[ID] += val;
            return;
        }
        propagate(ID);
        int mid = (L+R)>>1;
        if(from<=mid) incrementRange(from, to, val, LC, L, mid);
        if(to>mid) incrementRange(from, to, val, RC, mid+1, R);
        update(ID);
    }

    int queryRMN(int from, int to, int ID=1, int L=0, int R= N - 1) {
        if(IS_LEAF(ID))
            return t[ID] < 0 ? L : INF;
        propagate(ID);
        int mid = (L+R)>>1;
        if(to>=mid && t[RC]<0)
            return queryRMN(from, to, RC, mid + 1, R);
        else if(from<=mid && t[LC]<0)
            return queryRMN(from, to, LC, L, mid);
        return INF;
    }
};

SegTree st;

int main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

    int n,m; cin>>n>>m;
    vi dishes = readVector<int>(n);
    vi pupils = readVector<int>(m);

    for(auto e : dishes) {
        st.incrementRange(0,e,-1);
    }
    for(auto e : pupils) {
        st.incrementRange(0,e,1);
    }

    int q; cin>>q;
    FOR(i,q) {
        int type, pos, x; cin>>type>>pos>>x;
        pos--;
        if(type==1) {
            st.incrementRange(0, dishes[pos], 1);
            st.incrementRange(0, x, -1);
            dishes[pos] = x;
        }
        else {
            st.incrementRange(0, pupils[pos], -1);
            st.incrementRange(0, x, 1);
            pupils[pos] = x;
        }
        int res = st.queryRMN(0, 1e6);
        cout<<(res==SegTree::INF ? -1 : res)<<'\n';
    }

    return 0;
}


// by Balloons
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define mpr make_pair
#define debug() puts("okkkkkkkk")
#define rep(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)

using namespace std;

typedef long long LL;

const int inf = 1 << 30;
const int maxn=3e5+5,N=1e6,limit=1e9;

struct segm{
	int mn,lazy;
}se[N<<2];

int n,m;
int a[maxn],b[maxn],cnt[1000005];

void build(int l,int r,int num){
//	printf("%d %d\n",l,r);getchar();
	if(l==r){
		se[num].mn=limit;
		se[num].lazy=0;
		return ;
	}
	int mid=l+r>>1;
	build(l,mid,num<<1);
	build(mid+1,r,num<<1|1);
	se[num].mn=limit;se[num].lazy=0;
}

void pushdown(int num){
	if(!se[num].lazy)return ;
	se[num<<1].lazy+=se[num].lazy;
	se[num<<1|1].lazy+=se[num].lazy;
	se[num<<1].mn+=se[num].lazy;
	se[num<<1|1].mn+=se[num].lazy;
	se[num].lazy=0;
}

void upd(int l,int r,int num,int fm,int to,int delt){
	if(fm<=l && r<=to){
		se[num].lazy+=delt;
		se[num].mn+=delt;
		return ;
	}
	pushdown(num);
	int mid=l+r>>1;
	if(fm<=mid)upd(l,mid,num<<1,fm,to,delt);
	if(to>mid)upd(mid+1,r,num<<1|1,fm,to,delt);
	se[num].mn=min(se[num<<1].mn,se[num<<1|1].mn);
}

int query(int l,int r,int num){
	if(se[num].mn>=0)return -1;
	if(l==r)return l;
	int mid=l+r>>1;
	pushdown(num);
	if(se[num<<1|1].mn<0)return query(mid+1,r,num<<1|1);
	return query(l,mid,num<<1);
}

int main(){
	scanf("%d%d",&n,&m);
	build(1,N,1);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		++ cnt[a[i]];
		if(cnt[a[i]]==1)upd(1,N,1,a[i],a[i],-limit);
		upd(1,N,1,1,a[i],-1);
	}
	for(int i=1;i<=m;i++){
		scanf("%d",&b[i]);
		upd(1,N,1,1,b[i],1);
	}

	int q;scanf("%d",&q);
	while(q--){
		int op,li,x;scanf("%d%d%d",&op,&li,&x);
		if(op==1){
			upd(1,N,1,1,a[li],1);
			-- cnt[a[li]];
			if(cnt[a[li]]==0)upd(1,N,1,a[li],a[li],limit);
			a[li]=x;
			++ cnt[a[li]];
			if(cnt[a[li]]==1)upd(1,N,1,a[li],a[li],-limit);
			upd(1,N,1,1,a[li],-1);
		}else{
			upd(1,N,1,1,b[li],-1);
			b[li]=x;
			upd(1,N,1,1,b[li],1);
		}
		printf("%d\n",query(1,N,1));
	}

	return 0;
}


#include<bits/stdc++.h>
#define endl '\n'
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define lg2(x) __lg(x)
#define rem_dupl(x) sort(all(x)); x.erase(unique(all(x)), x.end())
using namespace std;

typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
#define x first
#define y second

const db eps = 1e-9;
const int maxn = (int)1e6 + 5;
const ll mod = 1000000007;

//#include<ext/pb_ds/assoc_container.hpp> // Common file
//#include<ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
//using namespace __gnu_pbds;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// // find_by_order
// // order_of_key

//mt19937 rand32(chrono::steady_clock::now().time_since_epoch().count());
//mt19937_64 rand64(chrono::steady_clock::now().time_since_epoch().count());

int st[maxn<<2], lazy[maxn<<2], pos;

void prop(int u, int l, int r, int x){
    st[u] += x;
    if(l != r){
        lazy[u<<1] += x;
        lazy[(u<<1)|1] += x;
    }
    lazy[u] = 0;
}

void upd(int u, int l, int r, int L, int R,int val){
    if(lazy[u]) prop(u,l,r,lazy[u]);
    if(l >= L and r <= R){
        prop(u,l,r,val);
        return;
    }
    if(l > R or r < L) return;
    int mid = (l+r)>>1;
    upd(u<<1,l,mid,L,R,val);
    upd((u<<1)|1,mid+1,r,L,R,val);
    st[u] = max(st[u<<1], st[(u<<1)|1]);
}

void query(int u, int l, int r){
    if(lazy[u]) prop(u,l,r,lazy[u]);
    if(st[u] <= 0) return;
    if(l == r){
        pos = max(pos, l);
        return;
    }
    int mid = (l+r)>>1;
    query(2*u+1, mid+1, r);
    if(pos == -1) query(2*u, l, mid);
}

const int val[] = {1,-1};

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cout.setf(ios::fixed); cout.precision(0);

    int n, m;
    cin >> n >> m;
    vector<int>a[2];
    a[0] = vector<int>(n);
    a[1] = vector<int>(m);
    for(int j = 0; j < 2; j++)
        for(auto &i:a[j]){
            cin >> i;
            upd(1,1,maxn-1,1,i,val[j]);
        }

    int q;
    cin >> q;
    while(q--){
        int t, i, x;
        cin >> t >> i >> x;
        t--, i--;

        upd(1,1,maxn-1,1, a[t][i], -val[t]);

        a[t][i] = x;

        upd(1,1,maxn-1,1, a[t][i], val[t]);

        pos = -1;

        query(1,1,maxn-1);

        cout << pos << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
#define endl '\n'
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define lg2(x) __lg(x)
#define rem_dupl(x) sort(all(x)); x.erase(unique(all(x)), x.end())
using namespace std;

typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
#define x first
#define y second

const db eps = 1e-9;
const int maxn = (int)1e6 + 5;
const ll mod = 1000000007;

//#include<ext/pb_ds/assoc_container.hpp> // Common file
//#include<ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
//using namespace __gnu_pbds;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// // find_by_order
// // order_of_key

//mt19937 rand32(chrono::steady_clock::now().time_since_epoch().count());
//mt19937_64 rand64(chrono::steady_clock::now().time_since_epoch().count());

int st[maxn<<2], lazy[maxn<<2], pos;

void prop(int u, int l, int r, int x){
    st[u] += x;
    if(l != r){
        lazy[u<<1] += x;
        lazy[(u<<1)|1] += x;
    }
    lazy[u] = 0;
}

void upd(int u, int l, int r, int L, int R,int val){
    if(lazy[u]) prop(u,l,r,lazy[u]);
    if(l >= L and r <= R){
        prop(u,l,r,val);
        return;
    }
    if(l > R or r < L) return;
    int mid = (l+r)>>1;
    upd(u<<1,l,mid,L,R,val);
    upd((u<<1)|1,mid+1,r,L,R,val);
    st[u] = max(st[u<<1], st[(u<<1)|1]);
}

void query(int u, int l, int r){
    if(lazy[u])
        prop(u,l,r,lazy[u]);
    if(st[u] <= 0)
        return;

    if(l == r){
        pos = max(pos, l);
        return;
    }
    int mid = (l+r)>>1;
    query(2*u+1, mid+1, r);

    if(pos == -1)
    query(2*u, l, mid);
}

const int val[] = {1,-1};
int n, m;

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cout.setf(ios::fixed); cout.precision(0);

    cin >> n >> m;
    vector<int>a[2];
    a[0] = vector<int>(n);
    a[1] = vector<int>(m);
    for(int j = 0; j < 2; j++)
        for(auto &i:a[j]){
            cin >> i;
            upd(1,1,maxn-1,1,i,val[j]);
        }

    int q;
    cin >> q;
    while(q--){
        int t, i, x;
        cin >> t >> i >> x;
        t--, i--;

        upd(1,1,maxn-1,1, a[t][i], -val[t]);

        a[t][i] = x;

        upd(1,1,maxn-1,1, a[t][i], val[t]);

        pos = -1;

        query(1,1,maxn-1);

        cout << pos << endl;
    }

    return 0;
}


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

typedef long long LL;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e6 + 10;

struct node {
    int l, r;
    int sum, mx;
}tr[N << 2];
int a[N], b[N];

void build(int u, int l, int r) {
    if(l == r) {
        tr[u] = {l, r, 0};
        return;
    }
    int mid = l + r >> 1;
    tr[u] = {l, r, 0};
    build(u << 1, l, mid);
    build(u << 1 | 1, mid + 1, r);
}

void push_up(int u) {
    tr[u].sum = tr[u << 1].sum + tr[u << 1 | 1].sum;
    tr[u].mx = max(tr[u << 1 | 1].mx, tr[u << 1].mx + tr[u << 1 | 1].sum);
}

void update(int u, int x, int num) {
    if(tr[u].l == tr[u].r) {
        tr[u].sum += num;
        tr[u].mx = tr[u].sum;
        return;
    }
    int mid = tr[u].l + tr[u].r >> 1;
    if(x <= mid) update(u << 1, x, num);
    else update(u << 1 | 1, x, num);
    push_up(u);
}

int query(int u, int sum) {
    if(tr[u].l == tr[u].r) {
        return tr[u].l;
    }
    int mid = tr[u].l + tr[u].r >> 1;
    if(sum + tr[u << 1 | 1].mx > 0) return query(u << 1 | 1, sum);
    else return query(u << 1, sum + tr[u << 1 | 1].sum);
}

void solve() {
    int n, m; cin >> n >> m;
    build(1, 1, 1000000);
    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);
    }
    int q; cin >> q;
    while(q--) {
        int op, i, x;
        scanf("%d%d%d", &op, &i, &x);
        if(op == 1) {
            update(1, a[i], -1);
            a[i] = x;
            update(1, a[i], 1);
        } else {
            update(1, b[i], 1);
            b[i] = x;
            update(1, b[i], -1);
        }
        if(tr[1].mx <= 0) puts("-1");
        else printf("%d\n", query(1, 0));
    }
}

int main() {
    // freopen("in.txt", "r", stdin);
    // int t; cin >> t; while(t--)
    solve();
    return 0;
}


#include <bits/stdc++.h>
#define ls (rt<<1)
#define rs (rt<<1|1)
#define ll long long
using namespace std;
const int maxn=3e5+10,tot=1e6;
int n,m,q;
int a[maxn],b[maxn];
int st[tot<<2],lazy[tot<<2];

void pushDown(int rt,int L,int R)
{
    if(lazy[rt]!=0)
    {
        int mid=(L+R)>>1;
        st[ls]=(mid-L+1)*lazy[rt];
        st[rs]=(R-mid)*lazy[rt];
        lazy[ls]+=lazy[rt];
        lazy[rs]+=lazy[rt];
        lazy[rt]=0;
    }
}

void update(int rt,int l,int r,int L,int R,int val)
{
    if(L==R)
    {
        st[rt]+=(R-L+1)*val;
        lazy[rt]+=val;
        return;
    }
    pushDown(rt,L,R);
    int mid=(L+R)>>1;
    if(l<=mid) update(ls,l,r,L,mid,val);
    if(r>mid) update(rs,l,r,mid+1,R,val);
    st[rt]=max(st[ls],st[rs]);
}
int query(int rt,int L,int R)
{
    if(L==R) return L;
    pushDown(rt,L,R);
    int mid=(L+R)>>1;
    if(st[ls]<=st[rs]) return query(rs,mid+1,R);
    else return query(ls,L,mid);
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; ++i) scanf("%d",&a[i]);
    for(int i=1; i<=m; ++i) scanf("%d",&b[i]);
    for(int i=1; i<=n; ++i) update(1,1,a[i],1,tot,1);
    for(int i=1; i<=m; ++i) update(1,1,b[i],1,tot,-1);
    scanf("%d",&q);
    while(q--)
    {
        int op,x,y;
        scanf("%d%d%d",&op,&x,&y);
        if(op==1)
        {
            update(1,1,a[x],1,tot,-1);
            a[x]=y;
            update(1,1,a[x],1,tot,1);
        }
        else
        {
            update(1,1,b[x],1,tot,1);
            b[x]=y;
            update(1,1,b[x],1,tot,-1);
        }
        if(st[1]>=1) printf("%d\n",query(1,1,tot));
        else puts("-1");
    }
    return 0;
}


#include <bits/stdc++.h>
#define ls (rt<<1)
#define rs (rt<<1|1)
#define ll long long
using namespace std;
const int maxn=3e5+10,tot=1e6;
int n,m,q;
int a[maxn],b[maxn];
int st[tot<<2],lazy[tot<<2];

void pushDown(int rt,int L,int R)
{
    if(lazy[rt]!=0)
    {
        st[ls]+=lazy[rt];
        st[rs]+=lazy[rt];
        lazy[ls]+=lazy[rt];
        lazy[rs]+=lazy[rt];
        lazy[rt]=0;
    }
}

void update(int rt,int l,int r,int L,int R,int val)
{
    if(L==R)
    {
        st[rt]+=val;
        lazy[rt]+=val;
        return;
    }
    pushDown(rt,L,R);
    int mid=(L+R)>>1;
    if(l<=mid) update(ls,l,r,L,mid,val);
    if(r>mid) update(rs,l,r,mid+1,R,val);
    st[rt]=max(st[ls],st[rs]);
}
int query(int rt,int L,int R)
{
    if(L==R) return L;
    pushDown(rt,L,R);
    int mid=(L+R)>>1;
    if(st[rs]>0) return query(rs,mid+1,R);
    else return query(ls,L,mid);
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; ++i) scanf("%d",&a[i]);
    for(int i=1; i<=m; ++i) scanf("%d",&b[i]);
    for(int i=1; i<=n; ++i) update(1,1,a[i],1,tot,1);
    for(int i=1; i<=m; ++i) update(1,1,b[i],1,tot,-1);
    scanf("%d",&q);
    while(q--)
    {
        int op,x,y;
        scanf("%d%d%d",&op,&x,&y);
        if(op==1)
        {
            update(1,1,a[x],1,tot,-1);
            a[x]=y;
            update(1,1,a[x],1,tot,1);
        }
        else
        {
            update(1,1,b[x],1,tot,1);
            b[x]=y;
            update(1,1,b[x],1,tot,-1);
        }
        if(st[1]>=1) printf("%d\n",query(1,1,tot));
        else puts("-1");
    }
    return 0;
}


#include <bits/stdc++.h>
#define ls (rt<<1)
#define rs (rt<<1|1)
#define ll long long
using namespace std;
const int maxn=3e5+10,tot=1e6;
int n,m,q;
int a[maxn],b[maxn];
int st[tot<<2],lazy[tot<<2];

void pushDown(int rt,int L,int R)
{
    if(lazy[rt]!=0)
    {
        st[ls]+=lazy[rt];
        st[rs]+=lazy[rt];
        lazy[ls]+=lazy[rt];
        lazy[rs]+=lazy[rt];
        lazy[rt]=0;
    }
}

void update(int rt,int l,int r,int L,int R,int val)
{
    if(l<=L&&R<=r)
    {
        st[rt]+=val;
        lazy[rt]+=val;
        return;
    }
    pushDown(rt,L,R);
    int mid=(L+R)>>1;
    if(l<=mid) update(ls,l,r,L,mid,val);
    if(r>mid) update(rs,l,r,mid+1,R,val);
    st[rt]=max(st[ls],st[rs]);
}
int query(int rt,int L,int R)
{
    if(L==R) return L;
    pushDown(rt,L,R);
    int mid=(L+R)>>1;
    if(st[rs]>0) return query(rs,mid+1,R);
    else return query(ls,L,mid);
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; ++i) scanf("%d",&a[i]);
    for(int i=1; i<=m; ++i) scanf("%d",&b[i]);
    for(int i=1; i<=n; ++i) update(1,1,a[i],1,tot,1);
    for(int i=1; i<=m; ++i) update(1,1,b[i],1,tot,-1);
    scanf("%d",&q);
    while(q--)
    {
        int op,x,y;
        scanf("%d%d%d",&op,&x,&y);
        if(op==1)
        {
            update(1,1,a[x],1,tot,-1);
            a[x]=y;
            update(1,1,a[x],1,tot,1);
        }
        else
        {
            update(1,1,b[x],1,tot,1);
            b[x]=y;
            update(1,1,b[x],1,tot,-1);
        }
        if(st[1]>=1) printf("%d\n",query(1,1,tot));
        else puts("-1");
    }
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;

#define scan(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0)
char _;
#define complete_unique(a) a.erase(unique(a.begin(),a.end()),a.end())
#define all(a) a.begin(),a.end()
#define println printf("\n");
#define readln(x) getline(cin,x);
#define pb push_back
#define endl "\n"
#define INT_INF 0x3f3f3f3f
#define LL_INF 0x3f3f3f3f3f3f3f3f
#define MOD 1000000007
#define mp make_pair
#define fastio cin.tie(0); cin.sync_with_stdio(0);

#define MAXN 300005
#define MAXV 1000005

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef unordered_map<int,int> umii;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef pair<int,pii> triple;
typedef int8_t byte;

mt19937 g1(time(0));

int randint(int a, int b){return uniform_int_distribution<int>(a, b)(g1);}
ll randlong(ll a,ll b){return uniform_int_distribution<long long>(a, b)(g1);}

ll gcd(ll a, ll b){return b == 0 ? a : gcd(b, a % b);}
ll lcm(ll a, ll b){return a*b/gcd(a,b);}
ll fpow(ll  b, ll exp, ll mod){if(exp == 0) return 1;ll t = fpow(b,exp/2,mod);if(exp&1) return t*t%mod*b%mod;return t*t%mod;}
ll divmod(ll i, ll j, ll mod){i%=mod,j%=mod;return i*fpow(j,mod-2,mod)%mod;}

struct node{
	int l,r,mx,lazy;
};

int num_dishes,num_people,num_q,d[MAXN],p[MAXN],totd[MAXV],totp[MAXV];
vector<pair<int,pii>> queries;
vector<int> dishes;
map<int,int> rev;
node seg[16*MAXN];

inline int numLess(int (&bit)[MAXV], int v){
	int res = 0;
	for(int i=v-1; i>0; i-=i&-i)
		res+=bit[i];
	return res;
}

inline void update(int (&bit)[MAXV], int pos, int v){
	for(int i=pos; i<=1e6; i+=i&-i)
		bit[i]+=v;
}

inline int sz(int rt){
	return seg[rt].r-seg[rt].l+1;
}

inline void push_up(int rt){
	seg[rt].mx = max(seg[rt<<1].mx,seg[rt<<1|1].mx);
}

inline void push_down(int rt){
	seg[rt<<1].mx+=seg[rt].lazy, seg[rt<<1|1].mx+=seg[rt].lazy;
	seg[rt<<1].lazy+=seg[rt].lazy, seg[rt<<1|1].lazy+=seg[rt].lazy;
	seg[rt].lazy = 0;
}

void build(int l, int r, int rt){
	seg[rt].l = l, seg[rt].r = r, seg[rt].mx = seg[rt].lazy = 0;
	if(l == r){
		int v = (num_dishes-numLess(totd,dishes[l-1]))-(num_people-numLess(totp,dishes[l-1]));
		seg[rt].mx = v;
		return;
	}
	int mid = (l+r)/2;
	build(l,mid,rt<<1);
	build(mid+1,r,rt<<1|1);
	push_up(rt);
}

void update(int l, int r, int rt, int v){
	if(seg[rt].l == l && seg[rt].r == r){
		seg[rt].lazy+=v, seg[rt].mx+=v;
		return;
	}
	if(seg[rt].lazy) push_down(rt);
	int mid = (seg[rt].l+seg[rt].r)/2;
	if(r <= mid) update(l,r,rt<<1,v);
	else if(l > mid) update(l,r,rt<<1|1,v);
	else update(l,mid,rt<<1,v), update(mid+1,r,rt<<1|1,v);
	push_up(rt);
}

int query(int rt){
	if(seg[rt].mx <= 0) return -1;
	if(seg[rt].l == seg[rt].r) return dishes[seg[rt].l-1];
	if(seg[rt].lazy) push_down(rt);
	if(seg[rt<<1|1].mx > 0) return query(rt<<1|1);
	return query(rt<<1);
}

int gmx(int pos, int rt){
	if(seg[rt].l == seg[rt].r) return seg[rt].mx;
	if(seg[rt].lazy) push_down(rt);
	int mid = (seg[rt].l+seg[rt].r)/2;
	if(pos <= mid) return gmx(pos,rt<<1);
	return gmx(pos,rt<<1|1);
}

inline void pr(){
	for(int i=1; i<=dishes.size(); i++)
		printf("%d: %d   ",dishes[i-1],gmx(i,1));
	printf("\n");
}

int main(){
	scanf("%d %d",&num_dishes,&num_people);
	for(int i=1; i<=num_dishes; i++){
		scanf(" %d",&d[i]);
		dishes.pb(d[i]);
		update(totd,d[i],1);
	}
	for(int i=1; i<=num_people; i++){
		scanf(" %d",&p[i]);
		dishes.pb(p[i]);
		update(totp,p[i],1);
	}
	scanf(" %d",&num_q);
	for(int i=1; i<=num_q; i++){
		int op,a,b; scanf(" %d %d %d",&op,&a,&b);
		dishes.pb(b);
		queries.pb(mp(op,mp(a,b)));
	}
	sort(all(dishes));
	complete_unique(dishes);
	for(int i=1; i<=dishes.size(); i++)
		rev[dishes[i-1]] = i;
	build(1,(int)dishes.size(),1);
//	pr();
	for(int i=0; i<queries.size(); i++){
		int op = queries[i].first, a = queries[i].second.first, b = queries[i].second.second;
		if(op == 1){
			update(1,rev[d[a]],1,-1);
			update(1,rev[d[a]=b],1,1);
		}else{
			update(1,rev[p[a]],1,1);
			update(1,rev[p[a]=b],1,-1);
		}
//		pr();
		printf("%d\n",query(1));
	}
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const int MAXN = 5e6;
const int MAXM = 1e6;
void buildTree(int nd, int left, int right);
void update(int nd, int l, int r, int target, int vals);
int query(int nd, int l, int r, int value);
void pushUP(int nd);

//对每个值的数量进行维护
//想要找到答案很多时候不需要过多的过程
vector<int> vals(1e6+1, 0);
vector<int> A;
vector<int> B;
int tree_sum[MAXN];
int tree_sf[MAXN];

signed main(){
  int n, m;
  int t;
  scanf("%d %d",&n, &m);
  for(int i = 0; i < n; i++){ scanf("%d", &t); A.emplace_back(t); vals[t]++;}
  for(int i = 0; i < m; i++){ scanf("%d", &t); B.emplace_back(t); vals[t]--;}
  buildTree(1, 1, MAXM);
  int q, tp, id, tar;
 // printf("%d \n ",query(1,1, MAXM, 0));
  scanf("%d", &q);
  for(int i = 0; i < q; i++){
    scanf("%d %d %d", &tp, &id, &tar);
    if(tp == 1){
      int val = A[id-1];
      update(1, 1, MAXM, val, -1);
      update(1, 1, MAXM, tar, 1);
    }
    else{
      int val = B[id-1];
      update(1, 1, MAXM, val, 1);
      update(1, 1, MAXM, tar, -1);
    }
    int ans = query(1, 1, MAXM, 0);
    printf("%d\n", ans);
  }
  return 0;
}

void pushUP(int nd){
    tree_sum[nd] = tree_sum[nd<<1] + tree_sum[nd<<1 | 1];
    tree_sf[nd] = max(tree_sf[nd << 1 |1], tree_sum[nd << 1 | 1] + tree_sf[nd <<1]);
}

void buildTree(int nd, int left, int right){
  if(left == right) {tree_sum[nd] = vals[left]; tree_sf[nd] = vals[left]; return;}
  int mid = (left + right) / 2;
  buildTree(nd<<1, left, mid);
  buildTree( (nd<<1) + 1, mid+1, right);
  pushUP(nd);
  //printf("l, r, vals: %d %d %d ", left, right, tree[nd]);
}

void update(int nd, int l, int r, int target, int vals){
 // printf("l, r, vals: %d %d %d \n", l, r, tree[nd]);
  if(l == r) {tree_sum[nd] += vals; tree_sf[nd] +=vals; return;}
  int mid = (l + r) >> 1;
  if(target > mid){
    update(nd*2+1, mid+1, r, target, vals);
  }
  else{
    update(nd*2, l, mid, target, vals);
  }
  pushUP(nd);
}

// int query(int nd, int l, int r, int sl, int sr){
//   if(l>=sl && r <= sr) return tree[nd];
//   int mid = (l+r) / 2;
//   int ans = 0;
//   if(sr > mid)
//     ans += query(nd*2+1, mid+1, r, sl, sr);
//   else
//     ans += query(nd*2, l, mid, sl, sr);
//   return ans;
// }

int query(int nd, int l, int r, int value){
   // printf("l, r, vals: %d %d %d %d \n", l, r, tree[nd], value);
    if(tree_sf[nd] <= value) return -1;
    if(l == r){
        if(tree_sf[nd] > value)
            return l;
        else
            return -1;
    }
    int mid = (l+r) / 2;
    if(tree_sf[nd*2+1] > value) return query(nd*2+1, mid+1, r, value);
    else{
      return query(nd*2, l, mid, value - tree_sum[nd*2+1]);
    }
}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
 {
	//code
	#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;cin>>t;
	while(t--){
		string s;
		cin>>s;
		int res=0;
		int n=s.length();
		vector<int> A(n+1);
		int mini=INT_MAX,pos=-1;
		A[0]=0;
		for(int i=0;i<n;i++){
			if(s[i]=='+'){
				A[i]=A[i-1]+1;
			}
			else
				A[i]=A[i-1]-1;
			if(mini>A[i]){
				mini=A[i];
				pos=i;
			}
		}
		int ans=n;
		pos++;
		if(mini<0)
		ans+=(pos*(pos+1))/2;
		cout<<ans<<endl;

	}
	return 0;
	}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
 {
	//code
	#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;cin>>t;
	while(t--){
		string s;
		cin>>s;
		int res=0;
		int init=0;
		while(1){
			int cur=init;
			++init;
			bool ok=1;
			for(auto i:s){
				res++;
				if(i=='+')
					cur++;
				else
					cur--;
				if(cur<0||res>1e6){
					ok=0; break;
				}
			}
			if(ok)
				break;
		}
		cout<<res<<endl;

	}
	return 0;
	}


#include <iostream>

typedef long int long_size_t;

int main(){

	size_t n;
	std::cin >> n;

	std::string line;
	long_size_t res;
        long long cnt, bottom;

	long_size_t j;

	for( size_t i = 0; i < n; ++i ){

		std::cin >> line;
		res = cnt = bottom = 0;

		for( j = 0; j < line.size(); ++j ){

			cnt = (line[j] == '-')? cnt-1: cnt+1;

			if( cnt < bottom ){

				res += j+1;
				bottom = cnt;
			}
		}
		std::cout << res+j << std::endl;
	}

	return 0;
}


#include <iostream>

typedef unsigned long long_size_t;

int main(){

	size_t n;
	std::cin >> n;

	std::string line;
	long_size_t res;
        long long cnt, bottom;

	long_size_t j;

	for( size_t i = 0; i < n; ++i ){

		std::cin >> line;
		res = cnt = bottom = 0;

		for( j = 0; j < line.size(); ++j ){

			cnt = (line[j] == '-')? cnt-1: cnt+1;

			if( cnt < bottom ){

				res += j+1;
				bottom = cnt;
			}
		}
		std::cout << res+j << std::endl;
	}

	return 0;
}


#include <iostream>

typedef unsigned long long long_size_t;

int main(){

	size_t n;
	std::cin >> n;

	std::string line;
	long_size_t res;
        int cnt, bottom;

	long_size_t j;

	for( size_t i = 0; i < n; ++i ){

		std::cin >> line;
		res = cnt = bottom = 0;

		for( j = 0; j < line.size(); ++j ){

			cnt = (line[j] == '-')? cnt-1: cnt+1;

			if( cnt < bottom ){

				res += j+1;
				bottom = cnt;
			}
		}
		std::cout << res+j << std::endl;
	}

	return 0;
}


#include <iostream>

typedef unsigned long long_size_t;

int main(){

	size_t n;
	std::cin >> n;

	std::string line;
	long_size_t res;
        int cnt, bottom;

	long_size_t j;

	for( size_t i = 0; i < n; ++i ){

		std::cin >> line;
		res = cnt = bottom = 0;

		for( j = 0; j < line.size(); ++j ){

			cnt = (line[j] == '-')? cnt-1: cnt+1;

			if( cnt < bottom ){

				res += j+1;
				bottom = cnt;
			}
		}
		std::cout << res+j << std::endl;
	}

	return 0;
}


#include <iostream>

typedef long unsigned long_size_t;

int main(){

	size_t n;
	std::cin >> n;

	std::string line;
	long_size_t res;
        long long cnt, bottom;

	long_size_t j;

	for( size_t i = 0; i < n; ++i ){

		std::cin >> line;
		res = cnt = bottom = 0;

		for( j = 0; j < line.size(); ++j ){

			cnt = (line[j] == '-')? cnt-1: cnt+1;

			if( cnt < bottom ){

				res += j+1;
				bottom = cnt;
			}
		}
		std::cout << res+j << std::endl;
	}

	return 0;
}


#include <iostream>

typedef long long long_size_t;

int main(){

	size_t n;
	std::cin >> n;

	std::string line;
	long_size_t res;
        long long cnt, bottom;

	long_size_t j;

	for( size_t i = 0; i < n; ++i ){

		std::cin >> line;
		res = cnt = bottom = 0;

		for( j = 0; j < line.size(); ++j ){

			cnt = (line[j] == '-')? cnt-1: cnt+1;

			if( cnt < bottom ){

				res += j+1;
				bottom = cnt;
			}
		}
		std::cout << res+j << std::endl;
	}

	return 0;
}


#include <iostream>

int main(){

	size_t n;
	std::cin >> n;

	std::string line;
	long long res;
	long long cnt, bottom;

	long long j;

	for( size_t i = 0; i < n; ++i ){

		std::cin >> line;
		res = cnt = bottom = 0;

		for( j = 0; j < line.size(); ++j ){

			cnt = (line[j] == '-')? cnt-1: cnt+1;

			if( cnt < bottom ){

				res += j+1;
				bottom = cnt;
			}
		}
		std::cout << res+j << std::endl;
	}

	return 0;
}


#include <iostream>

int main(){

	size_t n;
	std::cin >> n;

	std::string line;
	int res;
	int cnt, bottom;

	size_t j;

	for( size_t i = 0; i < n; ++i ){

		std::cin >> line;
		res = cnt = bottom = 0;

		for( j = 0; j < line.size(); ++j ){

			cnt = (line[j] == '-')? cnt-1: cnt+1;

			if( cnt < bottom ){

				res += j+1;
				bottom = cnt;
			}
		}
		std::cout << res+j << std::endl;
	}

	return 0;
}


#include <iostream>

typedef unsigned long int long_size_t;

int main(){

	size_t n;
	std::cin >> n;

	std::string line;
	long_size_t res;
        long long cnt, bottom;

	long_size_t j;

	for( size_t i = 0; i < n; ++i ){

		std::cin >> line;
		res = cnt = bottom = 0;

		for( j = 0; j < line.size(); ++j ){

			cnt = (line[j] == '-')? cnt-1: cnt+1;

			if( cnt < bottom ){

				res += j+1;
				bottom = cnt;
			}
		}
		std::cout << res+j << std::endl;
	}

	return 0;
}


#include <iostream>

int main(){

	size_t n;
	std::cin >> n;

	std::string line;
	size_t res;
	int cnt, bottom;

	size_t j;

	for( size_t i = 0; i < n; ++i ){

		std::cin >> line;
		res = cnt = bottom = 0;

		for( j = 0; j < line.size(); ++j ){

			cnt = (line[j] == '-')? cnt-1: cnt+1;

			if( cnt < bottom ){

				res += j+1;
				bottom = cnt;
			}
		}
		std::cout << res+j << std::endl;
	}

	return 0;
}


#include <iostream>

typedef unsigned long long long_size_t;

int main(){

	size_t n;
	std::cin >> n;

	std::string line;
	long_size_t res;
        long long cnt, bottom;

	long_size_t j;

	for( size_t i = 0; i < n; ++i ){

		std::cin >> line;
		res = cnt = bottom = 0;

		for( j = 0; j < line.size(); ++j ){

			cnt = (line[j] == '-')? cnt-1: cnt+1;

			if( cnt < bottom ){

				res += j+1;
				bottom = cnt;
			}
		}
		std::cout << res+j << std::endl;
	}

	return 0;
}


#include <iostream>

typedef long unsigned long_size_t;

int main(){

	size_t n;
	std::cin >> n;

	std::string line;
	long_size_t res;
	int cnt, bottom;

	long_size_t j;

	for( size_t i = 0; i < n; ++i ){

		std::cin >> line;
		res = cnt = bottom = 0;

		for( j = 0; j < line.size(); ++j ){

			cnt = (line[j] == '-')? cnt-1: cnt+1;

			if( cnt < bottom ){

				res += j+1;
				bottom = cnt;
			}
		}
		std::cout << res+j << std::endl;
	}

	return 0;
}


#include <iostream>

int main(){

	size_t n;
	std::cin >> n;

	std::string line;
	size_t elem;
	size_t res;
	int cnt, bottom;

	size_t j;

	for( size_t i = 0; i < n; ++i ){

		std::cin >> line;
		elem = 1;
		res = cnt = bottom = 0;

		for( j = 0; j < line.size(); ++j ){

			cnt = (line[j] == '-')? cnt-1: cnt+1;

			if( cnt < bottom ){

				res += elem;
				++elem;
				bottom = cnt;
			}
		}
		std::cout << res+j << std::endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1000000+10;
char s[maxn];int T,a[maxn],tot[maxn];
int main()
{
	cin>>T;
	while(T--)
	{
		cin>>s+1;
		memset(tot,0,sizeof(tot));
		int res=0,opt=0;
		for(int i=1;i<=strlen(s+1);i++)
		{
			if(s[i]=='+')a[i]=a[i-1]+1;
			else a[i]=a[i-1]-1;
		}
		for(int i=1;i<=strlen(s+1);i++)
		{
			if(a[i]<0&&!tot[-a[i]])
			{
				tot[-a[i]]=i;
				res+=i;
			}
		}
		res+=strlen(s+1);
		cout<<res<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1000000+10;
char s[maxn];int T,a[maxn],tot[maxn];
int main()
{
	cin>>T;
	while(T--)
	{
		cin>>s+1;
		int res=0,opt=0;
		for(int i=1;i<=strlen(s+1);i++)
		{
			if(s[i]=='+')a[i]=a[i-1]+1;
			else a[i]=a[i-1]-1;
			opt=max(opt,-a[i]);
		}
		if(opt==0)
		{
			printf("%d\n",strlen(s+1));
			continue;
		}
		tot[1]=min(opt+1,a[1]+opt+2);res+=tot[1];
		for(int i=2;i<=strlen(s+1);i++)
		{
			if(s[i-1]=='-')
			{
				tot[i]=min(tot[i-1]-1,a[i]+opt+2);
			}
			else
			{
				tot[i]=min(tot[i-1],a[i]+opt+2);
			}
			res+=tot[i];
		}
		cout<<res<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1000000+10;
char s[maxn];int T,a[maxn],tot[maxn];
int main()
{
	cin>>T;
	while(T--)
	{
		cin>>s+1;
		memset(tot,0,sizeof(tot));
		int res=0,opt=0;
        int n=strlen(s+1);
		for(int i=1;i<=n;i++)
		{
			if(s[i]=='+')a[i]=a[i-1]+1;
			else a[i]=a[i-1]-1;
		}
		for(int i=1;i<=n;i++)
		{
			if(a[i]<0&&!tot[-a[i]])
			{
				tot[-a[i]]=i;
				res+=i;
			}
		}
		res+=strlen(s+1);
		cout<<res<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const long long maxn=1000000+10;
char s[maxn];long long T,a[maxn],tot[maxn];
int main()
{
	cin>>T;
	while(T--)
	{
		cin>>s+1;
		memset(tot,0,sizeof(tot));
		long long res=0,opt=0;
        long long n=strlen(s+1);
		for(long long i=1;i<=n;i++)
		{
			if(s[i]=='+')a[i]=a[i-1]+1;
			else a[i]=a[i-1]-1;
		}
		for(long long i=1;i<=n;i++)
		{
			if(a[i]<0&&!tot[-a[i]])
			{
				tot[-a[i]]=i;
				res+=i;
			}
		}
		res+=strlen(s+1);
		cout<<res<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1000000+10;
char s[maxn];int T,a[maxn],tot[maxn];
int main()
{
	cin>>T;
	while(T--)
	{
		cin>>s+1;
		int res=0,opt=0;
		for(int i=1;i<=strlen(s+1);i++)
		{
			if(s[i]=='+')a[i]=a[i-1]+1;
			else a[i]=a[i-1]-1;
			opt=max(opt,-a[i]);
		}
		if(opt==0)
		{
			printf("%d\n",strlen(s+1));
			continue;
		}
		tot[0]=0x3f3f3f3f;
		for(int i=1;i<=strlen(s+1);i++)
		{
			if(s[i-1]=='-')
			{
				tot[i]=min(tot[i-1]-1,a[i]+opt+2);
			}
			else
			{
				tot[i]=min(tot[i-1],a[i]+opt+2);
			}
			res+=tot[i];
		}
		cout<<res<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        int n=s.length();
        long int res=0,curr=0,i=0;
        for(i;i<s.length();i++)
        {
            if(s[i]=='+')
            curr++;
            else
            curr--;

            if(curr<0)
            {
                res+=i+1;
				curr=0;
            }
        }
        res+=n;
        cout<<res<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        int n=s.length();
        long int res=0;
        int curr=0;
        int i=0;
        for(i;i<s.length();i++)
        {
            if(s[i]=='+')
            curr++;
            else
            curr--;

            if(curr<0)
            {
                res+=i+1;
				curr=0;
            }
        }
        res+=n;
        cout<<res<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
   int t;
   cin>>t;
   while(t)
   {
        string s;
    cin>>s;
    int res=0;
    for(int init=0;init<=INT_MAX;init++)
    {
        bool ok=true;
    for(int i=0;i<s.length();i++)
       {
        res++;
        if(s[i]=='+')
        cur++;
        else
        cur--;
        if(cur<0)
        {
            ok=false;
            break;
        }
        }
    if(ok)
    break;
    }

    cout<<res;
       t--;
   }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        long long res,last_res=0;
        bool ok=true;
        int init,last_index=0;

        while(init>=0)
        {
            long long curr=init+last_index-1;
            ok=true;

            for(int i=last_index;i<s.length();i++)
            {
                res+=last_res+1;
                if(s[i]=='+')
                curr++;
                else
                curr--;

                if(curr<0)
                {
                    ok=false;
                    last_index=i;
                    last_res=res;
                    break;
                }
            }
            if(ok)
            break;
        }
        cout<<res<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        long int n=s.length();
        long int res=0,curr=0,i=0;
        for(i;i<s.length();i++)
        {
            if(s[i]=='+')
            curr++;
            else
           {
		    curr--;

            if(curr<0)
            {
                res+=(i+1);
				curr=0;
            }
           }
        }
        res+=n;
        cout<<res<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        int n=s.length();
        long long res=0;
        int curr=0;
        int i=0;

            for(i;i<s.length();i++)
            {
                if(s[i]=='+')
                curr++;
                else
                curr--;

                if(curr<0)
                {
                    res+=i;
					i++;
                }
            }
            res+=n;
        cout<<res<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        int n=s.length();
        long int res=0;
        int curr=0;
        int i=0;
        for(i;i<s.length();i++)
        {
            if(s[i]=='+')
            curr++;
            else
            curr--;

            if(curr<0)
            {
                res+=i+1;
				curr=0;
            }
        }
        res+=n+1-1;
        cout<<res<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{ int t;
cin>>t;
while(t--)
{ string s;
cin>>s;
long long int x=0,a=0;
for(int i=0;i<s.length();i++)
{ if(s[i]=='+')
a++;
else
{a--;
if(a<0)
x+=i+1,a=0;}
}
cout<<x+s.size()<<endl;}}


#include <bits/stdc++.h>

using namespace std;

int main()
{
   int t;
   cin>>t;
   while(t>0)
   {
       int init=0;
       int res=0;
         string s;
        cin>>s;
    while(init>=0)
    {

        int cur=init;

        bool ok=true;
        for(int i=0;i<s.length();i++)
       {
        res++;
        if(s[i]=='+')
        cur++;
        else
        cur--;
        if(cur<0)
        {
            ok=false;
            break;
        }
        }
        init++;
    if(ok)
    break;
    }

    cout<<res<<endl;
       t--;
   }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
   int t;
   cin>>t;
   while(t)
   {
       int init=0;
       int res=0;
    while(init)
    {
        string s;
        cin>>s;
        int cur=init;

        bool ok=true;
        for(int i=0;i<s.length();i++)
       {
        res++;
        if(s[i]=='+')
        cur++;
        else
        cur--;
        if(cur<0)
        {
            ok=false;
            break;
        }
        }
        init++;
    if(ok)
    break;
    }

    cout<<res<<endl;
       t--;
   }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        long int n=s.length();
        long int res=0,curr=0,i=0;
        for(i;i<s.length();i++)
        {
            if(s[i]=='+')
            curr++;
            else
            curr--;

            if(curr<0)
            {
                res+=i+1;
				curr=0;
            }
        }
        res+=n;
        cout<<res<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        int n=s.length();
        long int res=0;
        long int curr=0;
        long int i=0;
        for(i;i<s.length();i++)
        {
            if(s[i]=='+')
            curr++;
            else
            curr--;

            if(curr<0)
            {
                res+=i;
				curr=0;
            }
        }
        res+=n;
        cout<<res<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
   int t;
   cin>>t;
   while(t)
   {
       long long int init=0;
       long long int res=0;
        string s;
        cin>>s;
    while(init>=0)
    {

        long long int cur=init;

        bool ok=true;
        for(int i=0;i<s.length();i++)
       {
            res++;
            if(s[i]=='+')
            cur++;
            else
            cur--;
            if(cur<0)
            {
                ok=false;
                break;
            }
        }

        init++;
        if(ok)
        break;
    }

        cout<<res<<endl;
        t--;
   }
    return 0;
}


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

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        long int n=s.length();
        long int res=0,curr=0,i=0;
        for(i;i<s.length();i++)
        {
            if(s[i]=='+')
            curr++;

            else
		    curr--;

            if(curr<0)
            {
                res+=(i+1);
				curr=0;
            }
        }
        cout<<res+n<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        long int n=s.length();
        long int res=0,curr=0,i=0;
        for(i;i<s.length();i++)
        {
            if(s[i]=='+')
            curr++;
            else
            curr--;

            if(curr<0)
            {
                res+=(i+1);
				curr=0;
            }
        }
        res+=n;
        cout<<res<<endl;
    }
    return 0;
}


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

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        long int n=s.length(),curr=0,i=0;
        long long res=0;
        for(i;i<s.length();i++)
        {
            if(s[i]=='+')
            curr++;

            else
		    curr--;

            if(curr<0)
            {
                res+=(i+1);
				curr=0;
            }
        }
        cout<<res+n<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        long long int n=s.length();
        long int res=0,curr=0,i=0;
        for(i;i<s.length();i++)
        {
            if(s[i]=='+')
            curr++;
            else
            curr--;

            if(curr<0)
            {
                res+=i+1;
	        curr=0;
            }
        }
        res+=n;
        cout<<res<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
   int t;
   cin>>t;
   while(t)
   {
       int init=0;
       int res=0;
        string s;
        cin>>s;
    while(init>=0)
    {

        int cur=init;

        bool ok=true;
        for(int i=0;i<s.length();i++)
       {
            res++;
            if(s[i]=='+')
            cur++;
            else
            cur--;
            if(cur<0)
            {
                ok=false;
                break;
            }
        }

        init++;
        if(ok)
        break;
    }

        cout<<res<<endl;
        t--;
   }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
   int t;
   cin>>t;
   while(t)
   {
    while(1)
    {
        string s;
    cin>>s;
    int res=0;
        bool ok=true;
    for(int i=0;i<s.length();i++)
       {
        res++;
        if(s[i]=='+')
        cur++;
        else
        cur--;
        if(cur<0)
        {
            ok=false;
            break;
        }
        }
    if(ok)
    break;
    }

    cout<<res<<endl;
       t--;
   }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
   int t;
   cin>>t;
   while(t)
   {
    while(1)
    {
        string s;
        cin>>s;
        int res=0;
        bool ok=true;
        for(int i=0;i<s.length();i++)
       {
        res++;
        if(s[i]=='+')
        cur++;
        else
        cur--;
        if(cur<0)
        {
            ok=false;
            break;
        }
        }
    if(ok)
    break;
    }

    cout<<res<<endl;
       t--;
   }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

/*struct FenwickTreeMin {
    vector<int> bit;
    int n;
    const int INF = (int)1e9;

    FenwickTreeMin(int n) {
        this->n = n;
        bit.assign(n, INF);
    }

    FenwickTreeMin(vector<int> a) : FenwickTreeMin(a.size()) {
        for (size_t i = 0; i < a.size(); i++)
            update(i, a[i]);
    }

    int getmin(int r) {
        int ret = INF;
        for (; r >= 0; r = (r & (r + 1)) - 1)
            ret = min(ret, bit[r]);
        return ret;
    }

    void update(int idx, int val) {
        for (; idx < n; idx = idx | (idx + 1))
            bit[idx] = min(bit[idx], val);
    }
};*/

string s;

int ans, n, dp[1000007], ind[1000007];

void solve() {
	cin >> s;
	ans = 0, n = s.size();
	if (s[0] == '-') {
		dp[0] = -1;
	} else {
		dp[0] = 1;
	}
	for (int i = 1; i < n; ++i) {
		dp[i] = dp[i - 1];
		if (s[i] == '-') {
			dp[i]--;
		} else {
			dp[i]++;
		}
	}
	int cnt = 0;
	for (int i = 0; i <= n + 1; ++i) {
		ind[i] = -1;
		if (i != n) {
			dp[i] *= -1;
		}
	}
	for (int i = 0; i < n; ++i) {
		if (dp[i] > cnt) {
			for (int j = cnt; j < dp[i]; ++j) {
				ind[j] = i;
			}
			cnt = dp[i];
		}
	}
	for (int i = 0; i <= n + 1; ++i) {
		if (ind[i] != -1) {
			ans += ind[i] + 1;
		} else {
			ans += n;
			break;
		}
	}
	/*while (1) {
		cnt++;
		flag = 1;
		for (int i = 0; i < n; ++i) {
			ans++;
			if (dp[i] > cnt) {
				flag = 0;
				break;
			}
		}
		//cout << ans << endl;
		if (flag) {
			break;
		}
	}*/
	cout << ans << endl;
	//cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	freopen("input.txt", "r", stdin);
	int t; cin >> t; while (t--) solve();
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

/*struct FenwickTreeMin {
    vector<int> bit;
    int n;
    const int INF = (int)1e9;

    FenwickTreeMin(int n) {
        this->n = n;
        bit.assign(n, INF);
    }

    FenwickTreeMin(vector<int> a) : FenwickTreeMin(a.size()) {
        for (size_t i = 0; i < a.size(); i++)
            update(i, a[i]);
    }

    int getmin(int r) {
        int ret = INF;
        for (; r >= 0; r = (r & (r + 1)) - 1)
            ret = min(ret, bit[r]);
        return ret;
    }

    void update(int idx, int val) {
        for (; idx < n; idx = idx | (idx + 1))
            bit[idx] = min(bit[idx], val);
    }
};*/

string s;

int ans, n, dp[1000007], ind[1000007];

void solve() {
	cin >> s;
	ans = 0, n = s.size();
	if (s[0] == '-') {
		dp[0] = -1;
	} else {
		dp[0] = 1;
	}
	for (int i = 1; i < n; ++i) {
		dp[i] = dp[i - 1];
		if (s[i] == '-') {
			dp[i]--;
		} else {
			dp[i]++;
		}
	}
	int cnt = 0;
	for (int i = 0; i <= 1e6; ++i) {
		ind[i] = -1;
		if (i != n) {
			dp[i] *= -1;
		}
	}
	for (int i = 0; i < n; ++i) {
		if (dp[i] > cnt) {
			for (int j = cnt; j < dp[i]; ++j) {
				ind[j] = i;
			}
			cnt = dp[i];
		}
	}
	for (int i = 0; i <= n + 1; ++i) {
		if (ind[i] != -1) {
			ans += ind[i] + 1;
		} else {
			ans += n;
			break;
		}
	}
	/*while (1) {
		cnt++;
		flag = 1;
		for (int i = 0; i < n; ++i) {
			ans++;
			if (dp[i] > cnt) {
				flag = 0;
				break;
			}
		}
		//cout << ans << endl;
		if (flag) {
			break;
		}
	}*/
	cout << ans << endl;
	//cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	//freopen("input.txt", "r", stdin);
	int t; cin >> t; while (t--) solve();
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

/*struct FenwickTreeMin {
    vector<long long> bit;
    long long n;
    const long long INF = (long long)1e9;

    FenwickTreeMin(long long n) {
        this->n = n;
        bit.assign(n, INF);
    }

    FenwickTreeMin(vector<long long> a) : FenwickTreeMin(a.size()) {
        for (size_t i = 0; i < a.size(); i++)
            update(i, a[i]);
    }

    long long getmin(long long r) {
        long long ret = INF;
        for (; r >= 0; r = (r & (r + 1)) - 1)
            ret = min(ret, bit[r]);
        return ret;
    }

    void update(long long idx, long long val) {
        for (; idx < n; idx = idx | (idx + 1))
            bit[idx] = min(bit[idx], val);
    }
};*/

string s;

long long ans, n, dp[1000007], ind[1000007];

void solve() {
	cin >> s;
	ans = 0, n = s.size();
	if (s[0] == '-') {
		dp[0] = -1;
	} else {
		dp[0] = 1;
	}
	for (long long i = 1; i < n; ++i) {
		dp[i] = dp[i - 1];
		if (s[i] == '-') {
			dp[i]--;
		} else {
			dp[i]++;
		}
	}
	long long cnt = 0;
	for (long long i = 0; i <= 1e6; ++i) {
		ind[i] = -1;
		if (i < n) {
			dp[i] *= -1;
		}
	}
	for (long long i = 0; i < n; ++i) {
		if (dp[i] > cnt) {
			for (long long j = cnt; j < dp[i]; ++j) {
				ind[j] = i;
			}
			cnt = dp[i];
		}
	}
	for (long long i = 0; i <= n + 1; ++i) {
		if (ind[i] != -1) {
			ans += ind[i] + 1;
		} else {
			ans += n;
			break;
		}
	}
	/*while (1) {
		cnt++;
		flag = 1;
		for (long long i = 0; i < n; ++i) {
			ans++;
			if (dp[i] > cnt) {
				flag = 0;
				break;
			}
		}
		//cout << ans << endl;
		if (flag) {
			break;
		}
	}*/
	cout << ans << endl;
	//cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	//freopen("input.txt", "r", stdin);
	long long t; cin >> t; while (t--) solve();
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

/*struct FenwickTreeMin {
    vector<int> bit;
    int n;
    const int INF = (int)1e9;

    FenwickTreeMin(int n) {
        this->n = n;
        bit.assign(n, INF);
    }

    FenwickTreeMin(vector<int> a) : FenwickTreeMin(a.size()) {
        for (size_t i = 0; i < a.size(); i++)
            update(i, a[i]);
    }

    int getmin(int r) {
        int ret = INF;
        for (; r >= 0; r = (r & (r + 1)) - 1)
            ret = min(ret, bit[r]);
        return ret;
    }

    void update(int idx, int val) {
        for (; idx < n; idx = idx | (idx + 1))
            bit[idx] = min(bit[idx], val);
    }
};*/

string s;

int ans, n, dp[1000007], ind[1000007];

void solve() {
	cin >> s;
	ans = 0, n = s.size();
	if (s[0] == '-') {
		dp[0] = -1;
	} else {
		dp[0] = 1;
	}
	for (int i = 1; i < n; ++i) {
		dp[i] = dp[i - 1];
		if (s[i] == '-') {
			dp[i]--;
		} else {
			dp[i]++;
		}
	}
	int cnt = 0;
	for (int i = 0; i <= n + 1; ++i) {
		ind[i] = -1;
		if (i != n) {
			dp[i] *= -1;
		}
	}
	for (int i = 0; i < n; ++i) {
		if (dp[i] > cnt) {
			for (int j = cnt; j < dp[i]; ++j) {
				ind[j] = i;
			}
			cnt = dp[i];
		}
	}
	for (int i = 0; i <= n + 1; ++i) {
		if (ind[i] != -1) {
			ans += ind[i] + 1;
		} else {
			ans += n;
			break;
		}
	}
	/*while (1) {
		cnt++;
		flag = 1;
		for (int i = 0; i < n; ++i) {
			ans++;
			if (dp[i] > cnt) {
				flag = 0;
				break;
			}
		}
		//cout << ans << endl;
		if (flag) {
			break;
		}
	}*/
	cout << ans << endl;
	//cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	//freopen("input.txt", "r", stdin);
	int t; cin >> t; while (t--) solve();
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

/*struct FenwickTreeMin {
    vector<int> bit;
    int n;
    const int INF = (int)1e9;

    FenwickTreeMin(int n) {
        this->n = n;
        bit.assign(n, INF);
    }

    FenwickTreeMin(vector<int> a) : FenwickTreeMin(a.size()) {
        for (size_t i = 0; i < a.size(); i++)
            update(i, a[i]);
    }

    int getmin(int r) {
        int ret = INF;
        for (; r >= 0; r = (r & (r + 1)) - 1)
            ret = min(ret, bit[r]);
        return ret;
    }

    void update(int idx, int val) {
        for (; idx < n; idx = idx | (idx + 1))
            bit[idx] = min(bit[idx], val);
    }
};*/

string s;

int ans, n, dp[1000007], ind[1000007];

void solve() {
	cin >> s;
	ans = 0, n = s.size();
	if (s[0] == '-') {
		dp[0] = -1;
	} else {
		dp[0] = 1;
	}
	for (int i = 1; i < n; ++i) {
		dp[i] = dp[i - 1];
		if (s[i] == '-') {
			dp[i]--;
		} else {
			dp[i]++;
		}
	}
	int cnt = 0;
	for (int i = 0; i <= n; ++i) {
		ind[i] = -1;
		if (i != n) {
			dp[i] *= -1;
		}
	}
	for (int i = 0; i < n; ++i) {
		if (dp[i] > cnt) {
			for (int j = cnt; j < dp[i]; ++j) {
				ind[j] = i;
			}
			cnt = dp[i];
		}
	}
	for (int i = 0; i <= n; ++i) {
		if (ind[i] != -1) {
			ans += ind[i] + 1;
		} else {
			if (i != n) ans += n;
			break;
		}
	}
	/*while (1) {
		cnt++;
		flag = 1;
		for (int i = 0; i < n; ++i) {
			ans++;
			if (dp[i] > cnt) {
				flag = 0;
				break;
			}
		}
		//cout << ans << endl;
		if (flag) {
			break;
		}
	}*/
	cout << ans << endl;
	//cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	//freopen("input.txt", "r", stdin);
	int t; cin >> t; while (t--) solve();
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

/*struct FenwickTreeMin {
    vector<long long> bit;
    long long n;
    const long long INF = (long long)1e9;

    FenwickTreeMin(long long n) {
        this->n = n;
        bit.assign(n, INF);
    }

    FenwickTreeMin(vector<long long> a) : FenwickTreeMin(a.size()) {
        for (size_t i = 0; i < a.size(); i++)
            update(i, a[i]);
    }

    long long getmin(long long r) {
        long long ret = INF;
        for (; r >= 0; r = (r & (r + 1)) - 1)
            ret = min(ret, bit[r]);
        return ret;
    }

    void update(long long idx, long long val) {
        for (; idx < n; idx = idx | (idx + 1))
            bit[idx] = min(bit[idx], val);
    }
};*/

string s;

long long ans, n, dp[1000007], ind[1000007];

void solve() {
	cin >> s;
	ans = 0, n = s.size();
	if (s[0] == '-') {
		dp[0] = -1;
	} else {
		dp[0] = 1;
	}
	for (long long i = 1; i < n; ++i) {
		dp[i] = dp[i - 1];
		if (s[i] == '-') {
			dp[i]--;
		} else {
			dp[i]++;
		}
	}
	long long cnt = 0;
	for (long long i = 0; i <= n; ++i) {
		ind[i] = -1;
		if (i < n) {
			dp[i] *= -1;
		}
	}
	for (long long i = 0; i < n; ++i) {
		if (dp[i] > cnt) {
			for (long long j = cnt; j < dp[i]; ++j) {
				ind[j] = i;
			}
			cnt = dp[i];
		}
	}
	for (long long i = 0; i <= n; ++i) {
		if (ind[i] != -1) {
			ans += ind[i] + 1;
		} else {
			ans += n;
			break;
		}
	}
	/*while (1) {
		cnt++;
		flag = 1;
		for (long long i = 0; i < n; ++i) {
			ans++;
			if (dp[i] > cnt) {
				flag = 0;
				break;
			}
		}
		//cout << ans << endl;
		if (flag) {
			break;
		}
	}*/
	cout << ans << endl;
	//cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	//freopen("input.txt", "r", stdin);
	long long t; cin >> t; while (t--) solve();
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

/*struct FenwickTreeMin {
    vector<int> bit;
    int n;
    const int INF = (int)1e9;

    FenwickTreeMin(int n) {
        this->n = n;
        bit.assign(n, INF);
    }

    FenwickTreeMin(vector<int> a) : FenwickTreeMin(a.size()) {
        for (size_t i = 0; i < a.size(); i++)
            update(i, a[i]);
    }

    int getmin(int r) {
        int ret = INF;
        for (; r >= 0; r = (r & (r + 1)) - 1)
            ret = min(ret, bit[r]);
        return ret;
    }

    void update(int idx, int val) {
        for (; idx < n; idx = idx | (idx + 1))
            bit[idx] = min(bit[idx], val);
    }
};*/

string s;

int ans, n, dp[1000007], ind[1000007];

void solve() {
	cin >> s;
	ans = 0, n = s.size();
	if (s[0] == '-') {
		dp[0] = -1;
	} else {
		dp[0] = 1;
	}
	int mn = 2e9;
	for (int i = 1; i < n; ++i) {
		dp[i] = dp[i - 1];
		if (s[i] == '-') {
			dp[i]--;
		} else {
			dp[i]++;
		}
	}
	int cnt = 0;
	for (int i = 0; i <= n; ++i) {
		ind[i] = -1;
		if (i != n) {
			dp[i] *= -1;
		}
	}
	for (int i = 0; i < n; ++i) {
		if (dp[i] > cnt && ind[cnt] == -1) {
			for (int j = cnt; j < dp[i]; ++j) {
				ind[j] = i;
			}
			cnt = dp[i];
		}
	}
	for (int i = 0; i <= n; ++i) {
		if (ind[i] != -1) {
			ans += ind[i] + 1;
		} else {
			ans += n;
			break;
		}
	}
	/*while (1) {
		cnt++;
		flag = 1;
		for (int i = 0; i < n; ++i) {
			ans++;
			if (dp[i] > cnt) {
				flag = 0;
				break;
			}
		}
		//cout << ans << endl;
		if (flag) {
			break;
		}
	}*/
	cout << ans << endl;
	//cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	//freopen("input.txt", "r", stdin);
	int t; cin >> t; while (t--) solve();
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

/*struct FenwickTreeMin {
    vector<int> bit;
    int n;
    const int INF = (int)1e9;

    FenwickTreeMin(int n) {
        this->n = n;
        bit.assign(n, INF);
    }

    FenwickTreeMin(vector<int> a) : FenwickTreeMin(a.size()) {
        for (size_t i = 0; i < a.size(); i++)
            update(i, a[i]);
    }

    int getmin(int r) {
        int ret = INF;
        for (; r >= 0; r = (r & (r + 1)) - 1)
            ret = min(ret, bit[r]);
        return ret;
    }

    void update(int idx, int val) {
        for (; idx < n; idx = idx | (idx + 1))
            bit[idx] = min(bit[idx], val);
    }
};*/

string s;

int ans, n, dp[1000007], ind[1000007];

void solve() {
	cin >> s;
	ans = 0, n = s.size();
	if (s[0] == '-') {
		dp[0] = -1;
	} else {
		dp[0] = 1;
	}
	for (int i = 1; i < n; ++i) {
		dp[i] = dp[i - 1];
		if (s[i] == '-') {
			dp[i]--;
		} else {
			dp[i]++;
		}
	}
	int cnt = 0;
	for (int i = 0; i <= 1e6 + 6; ++i) {
		ind[i] = -1;
		if (i != n) {
			dp[i] *= -1;
		}
	}
	for (int i = 0; i < n; ++i) {
		if (dp[i] > cnt) {
			for (int j = cnt; j < dp[i]; ++j) {
				ind[j] = i;
			}
			cnt = dp[i];
		}
	}
	for (int i = 0; i <= 1e6 + 6; ++i) {
		if (ind[i] != -1) {
			ans += ind[i] + 1;
		} else {
			ans += n;
			break;
		}
	}
	/*while (1) {
		cnt++;
		flag = 1;
		for (int i = 0; i < n; ++i) {
			ans++;
			if (dp[i] > cnt) {
				flag = 0;
				break;
			}
		}
		//cout << ans << endl;
		if (flag) {
			break;
		}
	}*/
	cout << ans << endl;
	//cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	//freopen("input.txt", "r", stdin);
	int t; cin >> t; while (t--) solve();
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

/*struct FenwickTreeMin {
    vector<int> bit;
    int n;
    const int INF = (int)1e9;

    FenwickTreeMin(int n) {
        this->n = n;
        bit.assign(n, INF);
    }

    FenwickTreeMin(vector<int> a) : FenwickTreeMin(a.size()) {
        for (size_t i = 0; i < a.size(); i++)
            update(i, a[i]);
    }

    int getmin(int r) {
        int ret = INF;
        for (; r >= 0; r = (r & (r + 1)) - 1)
            ret = min(ret, bit[r]);
        return ret;
    }

    void update(int idx, int val) {
        for (; idx < n; idx = idx | (idx + 1))
            bit[idx] = min(bit[idx], val);
    }
};*/

string s;

int ans, n, dp[1000007], ind[1000007];

void solve() {
	cin >> s;
	ans = 0, n = s.size();
	if (s[0] == '-') {
		dp[0] = -1;
	} else {
		dp[0] = 1;
	}
	for (int i = 1; i < n; ++i) {
		dp[i] = dp[i - 1];
		if (s[i] == '-') {
			dp[i]--;
		} else {
			dp[i]++;
		}
	}
	int cnt = 0;
	bool flag = 1;
	for (int i = 0; i <= n; ++i) {
		ind[i] = -1;
		if (i != n) {
			dp[i] *= -1;
		}
	}
	/*for (int i = 0; i < n; ++i) {
		if (dp[i] > cnt) {
			for (int j = cnt; j < dp[i]; ++j) {
				ind[j] = i;
			}
			cnt = dp[i];
		}
	}*/
	/*for (int i = 0; i <= n; ++i) {
		if (ind[i] != -1) {
			ans += ind[i] + 1;
		} else {
			ans += n;
			break;
		}
	}*/
	while (1) {
		flag = 1;
		for (int i = 0; i < n; ++i) {
			ans++;
			if (dp[i] > cnt) {
				flag = 0;
				break;
			}
		}
		//cout << ans << endl;
		if (flag) {
			break;
		}
		cnt++;
	}
	cout << ans << endl;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	//freopen("input.txt", "r", stdin);
	int t; cin >> t; while (t--) solve();
	return 0;
}


#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {
    int first[1000001];
    int N;
    cin >> N;
    while (N--){
        memset(first, 0, 1000001 * sizeof(int));
        string s;
        cin >> s;
        int sum = 0;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == '+') {
                sum++;
            } else {
                sum--;
            }
            if (sum < 0) {
                if (first[-sum] == 0) {
                    first[-sum] = i + 1;
                }
            }
        }
        int count = 0;
        for (int i = 0; i < s.size(); i++) {
            if (first[i+1] == 0) {
                count += s.length();
                break;
            }
            else {
                count += first[i+1];
            }
        }
        cout << count << "[";
        for (int i = 1; i < 100; i++) {
            cout << first[i] << ",";
        }
        cout << "]" << endl;
    }
    return 0;
}