> File Name: 5812.cpp
> Author: jiangyuzhu
> Mail: 834138558@qq.com
> Created Time: 2016/8/18 9:24:07
************************************************************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
const int maxn = 1e6 + 5, maxm = 20 + 5, oo = 0x3f3f3f3f;
int c[maxn][maxm];
int f[maxn];
bool isprime[maxn];
int prime[maxn];
int has[maxn];
int tot;
void getprime()
{
tot = 0;
memset(isprime, true, sizeof(isprime));
isprime[1] = false;
for(int i = 2; i < maxn; i++){
if(isprime[i]){
has[i] = 1;
prime[tot++] = i;
for(int j = 2 * i; j < maxn; j += i){
isprime[j] = false;
int tmp = j;
while(tmp % i == 0){
has[j]++;
tmp /= i;
}
}
}
}
}
int main (void)
{
int kas = 1;
int Q;
char ch;int x;
getprime();
while(~scanf("%d", &Q) && Q){
memset(f, 0, sizeof(f));
memset(c, 0, sizeof(c));
printf("Case #%d:\n", kas++);
for(int i = 0; i < Q; i++){
getchar();
scanf("%c%d", &ch, &x);
if(ch == 'I'){
if(c[x][0]) continue;
for(int i = 1; i * i <= x; i++){
if(x % i == 0){
c[x / i][has[i]]++;
f[x / i] |= 1 << has[i];
if(i * i != x){
c[i][has[x / i]]++;
f[i] |= 1 << has[x / i];
}
}
}
}else if(ch == 'D'){
if(!c[x][0]) continue;
for(int i = 1; i * i <= x; i++){
int cnt = 0;
if(x % i == 0){
if(c[x / i][has[i]]) c[x / i][has[i]]--;
if(!c[x / i][has[i]]) f[x / i] ^= 1 << has[i];
if(i * i != x){
if(c[i][has[x / i]]) c[i][has[x / i]]--;
if(!c[i][has[x / i]]) f[i] ^= 1 << has[x / i];
}
}
}
}else{
int ans = oo;
for(int i = 1; i * i <= x; i++){
if(x % i == 0 && f[x / i]){
ans = min(ans, __builtin_ffs(f[x / i]) - 1 + has[i]);
}
if(x % i == 0 && f[i]){
ans = min(ans, __builtin_ffs(f[i]) - 1 + has[x / i]);
}
}
if(ans == oo) ans = -1;
printf("%d\n", ans);
}
}
}
return 0;
}