rt,根号重构
using namespace std;
#include<bits/stdc++.h>
//#include<cstdio>
//#include<cstring>
//#include<string>
//#include<iostream>
//#include<utility>
//#include<algorithm>
#define MAX(x,y) (x<y?y:x)
#define MIN(x,y) (x<y?x:y)
#define ABS(x) (x<0?-x:x)
#define lb(x) ((x)&(-(x)))
#define LL long long
#define ULL unsigned LL
#define LD long double
#define int LL
//#define double LD
#define P 1000000007
#ifdef _WIN32
#define Rand() ((rand()<<16)|rand())
#define getchar _getchar_nolock
#define putchar _putchar_nolock
#elif _WINDOWS_
#define Rand() ((rand()<<16)|rand())
#define getchar _getchar_nolock
#define putchar _putchar_nolock
#else
#define Rand() (rand())
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
constexpr double EPS=(1e-6);
#ifdef int
constexpr int INF=9211081101201201140;
#else
constexpr int INF=1145141919;
#endif
template<typename T>T Max(T x,T y){return MAX(x,y);}
template<typename T>T Min(T x,T y){return MIN(x,y);}
template<typename T>T Abs(T x){return ABS(x);}
template<typename T>void Swap(T&x,T&y){x^=y^=x^=y;}
template<typename T>T Gcd(T x,T y){while(y^=x^=y^=x%=y);return x;}
template<typename T>
T qp(T a,T b){T sum=1;
while(b){if(b&1)sum=(sum*a)%P;a=(a*a)%P;b>>=1;}return sum;}
template<typename T>
void read(T&x){
x=0;char c=getchar();/*T fl=1;*/
while(c<'0'||c>'9'){/*if(c == '-')fl=-1;*/c=getchar();}
while('/'<c&&c<':'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}/*x*=fl;*/}
template<typename T>
void write(T x){if(x<0){x=-x;putchar('-');}
if(x>9){write(x/10);}putchar(x%10|48);}
int n;
struct myhs{
unsigned int operator()(int x) const {
x+=0x9e3779b97f4a7c15;
x=(x^(x>>30))*0xbf58476d1ce4e5b9;
x=(x^(x>>27))*0x94d049bb133111eb;
return x^(x>>31);
}
};
unordered_map<int,int,myhs>mp,del,vis;
#undef int
int main(){
#define int LL
read(n);
int T=sqrt((long long)n)+1;
for(int i=1;i<=n;i++){
char c=getchar();
while(c!='?'&&c!='+'&&c!='-')c=getchar();
int x;
read(x);
if(c=='+')vis[x]++;
else if(c=='-'){
del[x]++,vis[x]--;
if(!vis[x])vis.erase(x);
}
else{
int ans=INF;
for(auto u:del){
if(vis.find(u.first)==vis.end()&&u.first%x==0)ans=Min(ans,u.first);
}
for(int i=mp[x]+1;;i++){
if(i*x>2000000000000000000ll)break;
if(vis.find(i*x)!=vis.end())mp[x]=i;
else{
write(Min(ans,i*x)),puts("");
break;
}
}
}
if((int)del.size()>T){
for(auto&u:mp){
int x=u.first;
for(auto v:del)
if(vis.find(v.first)==vis.end()&&v.first%x==0)
u.second=Min(u.second,Max(v.first/x-5,(int)0));
del.clear();
}
}
}
return 0;
#undef int
}