/*
AizuOnline A2152
Title
*/
#include <stdio.h>
#include <string.h>

struct bd //block descriptor
{
        int start;
        int size;
        struct bd * nextp;
} bds[10000];
struct bd * emptyblock;
struct bd * free_block;  // start ptr
struct fd //file descriptor
{
        int name;
        struct bd * b;
} fds[10000];
struct fd * last_fdp;

int N;
char read_buf[30];
struct mcb
{
        int name;
        int start;
        int end;
} mcbs[10000];
int mcbcnt;
//
dump_sector_memory()
{
        struct fd *fdp;
        struct bd *bdp;
        int cnt;

        cnt=0;
        bdp=free_block;
        do
        {     
                mcbs[cnt].name = -1;
                mcbs[cnt].start=bdp->start;
                mcbs[cnt].end  =bdp->start+bdp->size-1;
                cnt++;
                bdp=bdp->nextp;
        }
        while(bdp);

        for(fdp=&fds[0];fdp<last_fdp;fdp++)
        {
                bdp=fdp->b;
                
                while(bdp)
                {
                        mcbs[cnt].name = fdp->name;
                        mcbs[cnt].start=bdp->start;
                        mcbs[cnt].end  =bdp->start+bdp->size-1;
                        cnt++;
                        bdp=bdp->nextp;
                }
        }
        mcbcnt = cnt;
}

dump_sector()
{
        struct fd *fdp;
        struct bd *bdp;
     

        printf("------------\n");
        bdp=free_block;
        do
        {       printf("Free block:");
                printf("%d ~ %d\n",bdp->start,bdp->start+bdp->size-1);
                bdp=bdp->nextp;
        }
        while(bdp);

        for(fdp=&fds[0];fdp<last_fdp;fdp++)
        {
                bdp=fdp->b;
                
                //if(bdp)
                        while(bdp)
                {
                        printf("File:%d:",fdp->name);
                        printf("%d ~ %d\n",bdp->start,bdp->start+bdp->size-1);
                        bdp=bdp->nextp;
                }
                
                
        }
}
copy_block(struct bd *src,struct bd *dst)
{
        memcpy(dst,src,sizeof(struct bd));
}

void write_command(int fname,int fsize)
{
        struct bd *bp,**bpp,*nextbp;
        struct fd *filep;
        int oldsize;


        
        filep=last_fdp++;
        filep->name=fname;

        filep->b=free_block;
                        
        while(1)
        {
                *bpp=free_block;
                bp = free_block;
                //printf("FSZ %d\n",fsize);
                        
                if(fsize <= bp->size)   //
                {
                        nextbp=bp->nextp;
                        bp->nextp = NULL;
                        oldsize   = bp->size;
                        bp->size  = fsize;

                        if(fsize < oldsize)   //split
                        {
                                copy_block(bp,emptyblock);
                                free_block=emptyblock++;
                                free_block->start = bp ->start +fsize;
                                free_block->size  = oldsize    -fsize;
                                free_block->nextp = nextbp;
                                return;
                        }
                        else //just size
                        {
                                free_block=nextbp;
                                break;
                        }
                }
                else 
                {
                        free_block=bp->nextp;     
                        fsize -= bp->size;
                        if(fsize > 0)
                        {
                                bpp=&(bp->nextp);
                                continue;
                        }
                        return;
                }
        }
}
int compare_mcb(const void *x, const void *y)
{
        return ((struct mcb *)x)->start - ((struct mcb *)y)->start;
}

int read_command(int sector)
{
        int siz,center,mini,maxi;

        dump_sector_memory();
        qsort(mcbs,mcbcnt,sizeof(struct mcb),compare_mcb);
        mini=0;maxi=mcbcnt;
        while(maxi-mini>1)
        {
                center=(maxi+mini)/2;
#ifdef DEBUG
                printf("R:%d C:%d %d\n",sector,
                       mcbs[center].start,mcbs[center].end);
#endif
                if(mcbs[center].start > sector)
                {
                        maxi=(maxi+mini)/2;
                }
                else if(mcbs[center].end < sector)
                {
                        mini=(maxi+mini)/2;
                }
                else  //equal
                {
                        return(mcbs[center].name);
                }
        }
        if(maxi-mini==1)
        {
                if(mcbs[maxi].start < sector)
                        return(mcbs[maxi].name);
                else
                        return(mcbs[mini].name);
        }
        return(mcbs[center].name);
}
void release_block(struct bd * r_bdp)
{
        struct bd *bdp,*next_bdp,*prev_bdp;

        bdp=free_block;
LOOP:
        if(bdp->start > r_bdp->start)
        {
                if(bdp==free_block)
                {
                        next_bdp  = free_block;
                        free_block = r_bdp;
                        r_bdp->nextp = next_bdp;
                }
                else  // 2nd block iko
                {
                        next_bdp = bdp;
                        prev_bdp->nextp = r_bdp;
                        r_bdp->nextp = next_bdp;
                }
                return;
        }

        prev_bdp=bdp;
        if(bdp=bdp->nextp)
                goto LOOP;
        else  // last block
        {
                prev_bdp->nextp=r_bdp;
                r_bdp->nextp=NULL;
        }
}
void delete_command(int fname)
{
        struct fd *fdp;
        struct bd *bdp,*nextbdp;

        for(fdp=fds;fdp<last_fdp;fdp++)
                if(fdp->name==fname)
                        goto NEXT;
        return;
NEXT:

        bdp=fdp->b;

        while(bdp)
        {
                nextbdp=bdp->nextp;
                release_block(bdp);
                bdp = nextbdp;
        }
        fdp->b=NULL;
}
void dispatch(char * buf)
{
        char *p;
        int  op1,op2,ret;

        p=strtok(buf," ");
        if(p[0]=='W')
        {
                p=strtok(NULL," ");
                sscanf(p,"%d",&op1);
                p=strtok(NULL," ");
                sscanf(p,"%d",&op2);
                write_command(op1,op2);
                return;
        }
        if(p[0]=='D')
        {
                p=strtok(NULL," ");
                sscanf(p,"%d",&op1);
                delete_command(op1);
                return;
        }
        if(p[0]=='R')
        {
                p=strtok(NULL," ");
                sscanf(p,"%d",&op1);
                ret=read_command(op1);
#ifdef DEBUG
                printf("ANS=");
#endif
                printf("%d\n",ret);
                return;
        }
}
init()
{
        bds[0].start=0;
        bds[0].size=1000000000;
        bds[0].nextp=NULL;
        free_block = &bds[0];

        last_fdp = &fds[0];
        emptyblock=&bds[1];
}
main()
{
        int i;

        init();

        while(EOF!=scanf("%d ",&N) && N)
        {
                for(i=0;i<N;i++)
                {
                        memset(read_buf,30,0);
                        fgets(read_buf,30,stdin);
                        dispatch(read_buf);
#ifdef DEBUG
                        if(read_buf[0] != 'R')
                                dump_sector();
#endif
                }
        
        }
        return(0);
}