#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFSIZE 64
#define ALPHASIZE 'z' - 'a' + 1

struct node {
  struct node *move[ALPHASIZE];
  int value;
} *root;

struct node* create_node(void)
{
  struct node* result = (struct node*) malloc(sizeof(struct node));
  memset(result->move, 0, sizeof(result->move));
  result->value = 0;
  return result;
}

void destroy_node(struct node *root)
{
  int i;
  if(root == NULL)
    return;
  for(i = 0; i < ALPHASIZE; ++i)
    destroy_node(root->move[i]);
  free(root);
}

int insert(const char *buffer)
{
  struct node *cur = root;
  const char *c;
  for(c = buffer; *c; ++c) {
    int index = *c - 'a';
    if(!cur->move[index])
      cur->move[index] = create_node();
    cur = cur->move[index];
  }
  return cur->value++;
}

void process_query(const char *buffer, char *answer)
{
  int result = insert(buffer);
  if(!result)
    sprintf(answer, "OK");
  else
    sprintf(answer, "%s%d", buffer, result);
}

void process_queries(void)
{
  int n, i;
  char buffer[BUFSIZE], answer[BUFSIZE];
  scanf("%d", &n);
  for(i = 0; i < n; ++i) {
    scanf("%s", buffer);
    process_query(buffer, answer);
    printf("%s\n", answer);
  }
}

int main(void)
{
  root = create_node();
  process_queries();
  destroy_node(root);
  return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFSIZE 64
#define ALPHASIZE 'z' - 'a' + 1

struct node {
  struct node *move[ALPHASIZE];
  int value;
} *root;

struct node* create_node(void)
{
  struct node* result = (struct node*) malloc(sizeof(struct node));
  memset(result->move, 0, sizeof(result->move));
  result->value = 0;
  return result;
}

void destroy_node(node *root)
{
  int i;
  if(root == NULL)
    return;
  for(i = 0; i < ALPHASIZE; ++i)
    destroy_node(root->move[i]);
  free(root);
}

int insert(const char *buffer)
{
  struct node *cur = root;
  const char *c;
  for(c = buffer; *c; ++c) {
    int index = *c - 'a';
    if(!cur->move[index])
      cur->move[index] = create_node();
    cur = cur->move[index];
  }
  return cur->value++;
}

void process_query(const char *buffer, char *answer)
{
  int result = insert(buffer);
  if(!result)
    sprintf(answer, "OK");
  else
    sprintf(answer, "%s%d", buffer, result);
}

void process_queries(void)
{
  int n, i;
  char buffer[BUFSIZE], answer[BUFSIZE];
  scanf("%d", &n);
  for(i = 0; i < n; ++i) {
    scanf("%s", buffer);
    process_query(buffer, answer);
    printf("%s\n", answer);
  }
}

int main(void)
{
  root = create_node();
  process_queries();
  destroy_node(root);
  return 0;
}


#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>

#define MAX 10000
#define MAXLEN 100

bool cmplen (char *s, char *c) {
	if (strcmp(s, c) == 0) return true;
	return false;
}

bool exist (char s[MAX][MAXLEN], int i, int *x) {
	int j;
	bool isThere = false;

	for (j = 0; j < i; j++)
		if (cmplen(s[i], s[j])) {
			isThere =  true;
			(*x)++;
		}

	return isThere;
}
int main () {
	int n;
	char names[MAX][MAXLEN];
	int i, k;
	int d;

	scanf ("%d", &n);

	for (i = 0; i < n; i++) {
		d = 0;
		scanf ("%s", names[i]);
		if (!exist(names, i, &d)) printf("OK\n");
		else {
			printf("%s%d\n", names[i], d);
		}
	}

	return 0;

}


#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>

#define MAX 10000
#define MAXLEN 33

bool exist (char s[MAX][MAXLEN], int i, int *x) {
	int j;
	bool isThere = false;

	for (j = 0; j < i; j++)
		if (strcmp(s[i], s[j]) == 0) {
			isThere =  true;
			(*x)++;
		}

	return isThere;
}
int main () {
	int n;
	char names[MAX][MAXLEN];
	int i, k;
	int d;

	scanf ("%d", &n);

	for (i = 0; i < n; i++) {
		d = 0;
		scanf ("%s", names[i]);
		if (!exist(names, i, &d)) printf("OK\n");
		else {
			printf("%s%d\n", names[i], d);
		}
	}

	return 0;

}


#include <stdio.h>
#include <stdbool.h>
#include <string.h>

#define MAX 10000
#define MAXLEN 100

bool exist (char s[MAX][MAXLEN], int i) {
	int j;

	for (j = 0; j < i; j++)
		if (strcmp(s[j], s[i]) == 0) return true;
	return false;
}

void append (char s[MAX][MAXLEN], int i, int len) {
	int j, k;

	for (j = len; j < MAX; j++)
		for (k = 1; k <= 9; k++) {
			s[i][j] = k + '0';
			s[i][j + 1] = '\0';
			if (exist(s, i) && k != 9) continue;
			else if (exist(s, i) && k == 9) s[i][j] = 1 + '0';
			else if (!exist(s, i)) return;
		}
}

int main () {
	int n;
	char names[MAX][MAXLEN];
	int i;

	scanf ("%d", &n);

	for (i = 0; i < n; i++) {
		scanf ("%s", names[i]);
		if (!exist(names, i)) printf("OK\n");
		else {
			append(names, i, strlen(names[i]));
			printf("%s\n", names[i]);
		}
	}

}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define MAX 100000

int main(int argc, char const *argv[])
{
    char s[MAX+1][50];
    int occurence[MAX+1];
    int top, i, j, h, n;
    bool found;

    memset (occurence, 0, sizeof(occurence));

    scanf ("%d", &n);

    for (i = 0, h = 0; h < n; i++, h++){
        scanf ("%s", s[i]);
        found = false;
        for (j = 0; j < i; j++)
            if (strcmp(s[j], s[i]) == 0){
                occurence[j]++;
                printf("%s%d\n", s[j], occurence[j]);
                i--;
                found = true;
                break;
            }
        if (!found)
            printf("OK\n");
    }

    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define MAX 100000

int main(int argc, char const *argv[])
{
    char s[MAX+1][MAX+1];
    int occurence[MAX+1];
    int top, i, j, h, n;
    bool found;

    memset (occurence, 0, sizeof(occurence));

    scanf ("%d", &n);

    for (i = 0, h = 0; h < n; i++, h++){
        scanf ("%s", s[i]);
        found = false;
        for (j = 0; j < i; j++)
            if (strcmp(s[j], s[i]) == 0){
                occurence[j]++;
                printf("%s%d\n", s[j], occurence[j]);
                i--;
                found = true;
                break;
            }
        if (!found)
            printf("OK\n");
    }

    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define MAX 1000

int main(int argc, char const *argv[])
{
    char s[MAX+1][MAX+1];
    int occurence[MAX+1];
    int top, i, j, h, n;
    bool found;

    memset (occurence, 0, sizeof(occurence));

    scanf ("%d", &n);

    for (i = 0, h = 0; h < n; i++, h++){
        scanf ("%s", s[i]);
        found = false;
        for (j = 0; j < i; j++)
            if (strcmp(s[j], s[i]) == 0){
                occurence[j]++;
                printf("%s%d\n", s[j], occurence[j]);
                i--;
                found = true;
                break;
            }
        if (!found)
            printf("OK\n");
    }

    return 0;
}


#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>

#define MAX 10000
#define MAXLEN 100

bool cmplen (char *s, char *c) {
	int len = strlen (s);
	int i = 0;
	if (strlen (c) < len) return false;
	else if (strlen (c) > len) return false;

	for (;i < len; i++) {
		if (c[i] != s[i]) return false;
	}
	return true;
}

bool exist (char s[MAX][MAXLEN], int i) {
	int j;

	for (j = 0; j < i; j++)
		if (cmplen(s[i], s[j])) return true;
	return false;
}

/*void append (char s[MAX][MAXLEN], int i, int len) {
	int j, k;

	for (j = len; j < MAXLEN; j++)
		for (k = 1; k <= 9; k++) {
			s[i][j] = k + '0';
			s[i][j + 1] = '\0';
			if (exist(s, i) && k != 9) continue;
			else if (exist(s, i) && k == 9) s[i][j] = 1 + '0';
			else if (!exist(s, i)) return;
		}
}*/

int main () {
	int n;
	char names[MAX][MAXLEN];
	int i, k;
	int d;

	scanf ("%d", &n);

	for (i = 0; i < n; i++) {
		scanf ("%s", names[i]);
		if (!exist(names, i)) printf("OK\n");
		else {
			d = 0;
			//append(names, i, strlen(names[i]));
			for (k = 0; k < i; k++)
				if (cmplen(names[i], names[k])) d++;
			printf("%s%d\n", names[i], d);
		}
	}

}


#include <stdio.h>
#include <stdbool.h>
#include <string.h>

#define MAX 10000
#define MAXLEN 100

bool exist (char s[MAX][MAXLEN], int i) {
	int j;

	for (j = 0; j < i; j++)
		if (strcmp(s[j], s[i]) == 0) return true;
	return false;
}

void append (char s[MAX][MAXLEN], int i, int len) {
	int j, k;

	for (j = len; j < MAXLEN; j++)
		for (k = 1; k <= 9; k++) {
			s[i][j] = k + '0';
			s[i][j + 1] = '\0';
			if (exist(s, i) && k != 9) continue;
			else if (exist(s, i) && k == 9) s[i][j] = 1 + '0';
			else if (!exist(s, i)) return;
		}
}

int main () {
	int n;
	char names[MAX][MAXLEN];
	int i;

	scanf ("%d", &n);

	for (i = 0; i < n; i++) {
		scanf ("%s", names[i]);
		if (!exist(names, i)) printf("OK\n");
		else {
			append(names, i, strlen(names[i]));
			printf("%s\n", names[i]);
		}
	}

}


#include <stdio.h>
#include <stdbool.h>
#include <string.h>

#define MAX 10000
#define MAXLEN 100

bool cmplen (char *s, char *c) {
	int len = strlen (s);
	int i = 0;

	for (;i < len; i++)
		if (c[i] != s[i]) return false;
	return true;
}

bool exist (char s[MAX][MAXLEN], int i) {
	int j;

	for (j = 0; j < i; j++)
		if (cmplen(s[j], s[i])) return true;
	return false;
}

/*void append (char s[MAX][MAXLEN], int i, int len) {
	int j, k;

	for (j = len; j < MAXLEN; j++)
		for (k = 1; k <= 9; k++) {
			s[i][j] = k + '0';
			s[i][j + 1] = '\0';
			if (exist(s, i) && k != 9) continue;
			else if (exist(s, i) && k == 9) s[i][j] = 1 + '0';
			else if (!exist(s, i)) return;
		}
}*/

int main () {
	int n;
	char names[MAX][MAXLEN];
	int i, k;
	int d;

	scanf ("%d", &n);

	for (i = 0; i < n; i++) {
		scanf ("%s", names[i]);
		if (!exist(names, i)) printf("OK\n");
		else {
			d = 0;
			//append(names, i, strlen(names[i]));
			for (k = 0; k < i; k++)
				if (cmplen(names[i], names[k])) d++;
			printf("%s%d\n", names[i], d);
		}
	}

}


#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>

#define MAX 100000
#define MAXLEN 33

bool exist (char s[MAX][MAXLEN], int i, int *x) {
	int j;
	bool isThere = false;

	for (j = 0; j < i; j++)
		if (strcmp(s[i], s[j]) == 0) {
			isThere =  true;
			(*x)++;
		}

	return isThere;
}
int main () {
	int n;
	char names[MAX][MAXLEN];
	int i, k;
	int d;

	scanf ("%d", &n);

	for (i = 0; i < n; i++) {
		d = 0;
		scanf ("%s", names[i]);
		if (!exist(names, i, &d)) printf("OK\n");
		else
			printf("%s%d\n", names[i], d);
	}

	return 0;

}


#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>

#define MAX 10000
#define MAXLEN 100

bool cmplen (char *s, char *c) {
	if (strcmp(s, c) == 0) return true;
	return false;
}

bool exist (char s[MAX][MAXLEN], int i, int *x) {
	int j;
	bool isThere = false;

	for (j = 0; j < i; j++)
		if (cmplen(s[i], s[j])) {
			isThere =  true;
			(*x)++;
		}

	return isThere;
}
int main () {
	int n;
	char names[MAX][MAXLEN];
	int i, k;
	int d;

	scanf ("%d", &n);

	for (i = 0; i < n; i++) {
		d = 0;
		scanf ("%s", names[i]);
		if (!exist(names, i, &d)) printf("OK\n");
		else {
			printf("%s%d\n", names[i], d);
		}
	}

}


#include<stdio.h>
#include<string.h>
/*void hash_check(char* arr)
{
long long int i,hashv=0;int p=3;
for(i=0;i<strlen(arr);i++)
{
hashv+=(arr[i]-0)*p;
p=p*3;
hashv=hashv%10000001;
p=p%10000001;
}
if(hash[hashv]!=0)
{

printf("%s%d\n",arr,hash[hashv]);
hash[hashv]++;
}
else
{hash[hashv]++;printf("OK\n");}
}*/
char a[100000][33],b[33];
int hash[100000];
int main()
{
int i,n,j,l=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s",b);
for(j=0;j<l;j++)
{
if(strcmp(b,a[j])==0) break;
}
if(j==l)
{
printf("OK\n");
strcpy(a[l],b);
l++;
}
else
{
hash[j]++;
printf("%s%d\n",b,hash[j]);
}
}
return 0;
}


#include<stdio.h>
#include<string.h>
int hash[10000001]={0};
void hash_check(char* arr)
{
long long int i,hashv=0;int p=3;
for(i=0;i<strlen(arr);i++)
{
hashv+=(arr[i]-0)*p;
p=p*3;
hashv=hashv%10000001;
p=p%10000001;
}
if(hash[hashv]!=0)
{

printf("%s%d\n",arr,hash[hashv]);
hash[hashv]++;
}
else
{hash[hashv]++;printf("OK\n");}
}
int main()
{
int i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
{
char arr[50]={};
scanf("%s",arr);
hash_check(arr);
}
return 0;
}


#include<stdio.h>
#include<string.h>
int hash[100001]={0};
void hash_check(char* arr)
{
long long int i,hashv=0;int p=3;
for(i=0;i<strlen(arr);i++)
{
hashv+=(arr[i]-0)*p;
p=p*3;
hashv=hashv%100001;
p=p%100001;
}
if(hash[hashv]!=0)
printf("%s%d\n",arr,hash[hashv]);
else
{hash[hashv]++;printf("OK\n");}
}
int main()
{
int i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
{
char arr[33]={};
scanf("%s",arr);
hash_check(arr);
}
return 0;
}


#include<stdio.h>
#include<string.h>
int hash[100001]={0};
void hash_check(char* arr)
{
long long int i,hashv=0;int p=3;
for(i=0;i<strlen(arr);i++)
{
hashv+=(arr[i]-0)*p;
p=p*3;
hashv=hashv%100001;
p=p%100001;
}
if(hash[hashv]!=0)
{

printf("%s%d\n",arr,hash[hashv]);
hash[hashv]++;
}
else
{hash[hashv]++;printf("OK\n");}
}
int main()
{
int i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
{
char arr[33]={};
scanf("%s",arr);
hash_check(arr);
}
return 0;
}


#include<stdio.h>
#include<string.h>
int hash[10000001]={0};
void hash_check(char* arr)
{
long long int i,hashv=0;int p=31;
for(i=0;i<strlen(arr);i++)
{
hashv+=(arr[i]-0)*p;
p=p*31;
hashv=hashv%10000001;
p=p%10000001;
}
if(hash[hashv]!=0)
{

printf("%s%d\n",arr,hash[hashv]);
hash[hashv]++;
}
else
{hash[hashv]++;printf("OK\n");}
}
int main()
{
int i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
{
char arr[33]={};
scanf("%s",arr);
hash_check(arr);
}
return 0;
}


#include<string.h>
#include<stdio.h>
int main()
{
   int n,i,j,k;char a[100001][33];int vis[100000];
   memset(vis,0,sizeof(vis));
   scanf("%d",&n);
   for(i=0;i<n;)
   {
   scanf("%s",a[i]);
   if(i==0)
   {printf("OK\n");i++;}
   else
   {
   for(j=0;j<i;j++)
   {
   if(strcmp(a[j],a[i])==0)
   {
   n--;
   for(k=0;k<33;k++)
   a[i][k]='\0';
   vis[j]++;
   printf("%s%d\n",a[j],vis[j]);
   break;
   }
   }
   if(j==i)
   {
   printf("OK\n");i++;
   }
   }
   }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct LST{

    char name[33];

    int count;

} LST;

int bul ( LST* lst, char a[], int size ){

    int i;

    for ( i = 0; i < size; i++ ){

        if ( strcmp ( lst[i].name, a ) == 0 )

            return i;

    }

    return -1;

}

int main()
{
    int n, i, size = 1, ind, x;

    char temp[33] ;

    char buff[39] ;

    char** output ;

    LST* lst;

    scanf("%d", &n);

    output = malloc (sizeof ( char* ) * n );

    lst = malloc ( sizeof ( LST ) * size );

    lst[0].count = 0;

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

        scanf("%s", temp);

        if ( i == 0 ){

            strcpy( lst[i].name, temp );

            output[i] = malloc( sizeof ( char )* 3 );

            strcpy( output[i], "OK" );

        }

        else{

            ind = bul ( lst, temp, size );

            if ( ind == -1 ){

                size += 1 ;

                lst = realloc( lst, sizeof( LST ) * size ) ;

                lst[size-1].count = 0 ;

                strcpy( lst[size-1].name, temp ) ;

                output[i] = malloc ( sizeof ( char ) * 3 ) ;

                strcpy( output[i], "OK" ) ;

            }

            else{

                lst[ind].count += 1;

                x = sprintf( buff, "%s%d", temp, lst[ind].count ) ;

                output[i] = malloc ( sizeof ( char ) * ( x - 1 ) );

                strcpy(output[i], buff );

            }

        }

    }

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

        printf("%s\n", output[i]);

    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct LST{

    char name[33];

    int count;

} LST;

int bul ( LST* lst, char a[], int size ){

    int i;

    for ( i = 0; i < size; i++ ){

        if ( strcmp ( lst[i].name, a ) == 0 )

            return i;

    }

    return -1;

}

int main()
{
    int n, i, size = 1, ind, x;

    char temp[33] ;

    char buff[39] ;

    char** output ;

    LST* lst;

    scanf("%d", &n);

    output = malloc (sizeof ( char* ) * n );

    lst = malloc ( sizeof ( LST ) * size );

    lst[0].count = 0;

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

        scanf("%s", temp);

        if ( i == 0 ){

            strcpy( lst[i].name, temp );

            output[i] = malloc( sizeof ( char )* 3 );

            strcpy( output[i], "OK" );

            output[i][3] = '\0' ;

        }

        else{

            ind = bul ( lst, temp, size );

            if ( ind == -1 ){

                size += 1 ;

                lst = realloc( lst, sizeof( LST ) * size ) ;

                lst[size-1].count = 0 ;

                strcpy( lst[size-1].name, temp ) ;

                output[i] = malloc ( sizeof ( char ) * 3 ) ;

                strcpy( output[i], "OK" ) ;

                output[i][3] = '\0' ;

            }

            else{

                lst[ind].count += 1;

                x = sprintf( buff, "%s%d", temp, lst[ind].count ) ;

                output[i] = malloc ( sizeof ( char ) * ( x + 1 ) );

                strcpy(output[i], buff );

            }

        }

    }

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

        printf( "%s\n", output[i] );

    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct LST{

    char name[33];

    int count;

} LST;

int bul ( LST* lst, char a[], int size ){

    int i;

    for ( i = 0; i < size; i++ ){

        if ( strcmp ( lst[i].name, a ) == 0 )

            return i;

    }

    return -1;

}

int main()
{
    int n, i, size = 1, ind;

    char temp[33] ;

    char buff[39] ;

    char** output ;

    LST* lst;

    scanf("%d", &n);

    output = malloc (sizeof ( char* ) * n );

    lst = malloc ( sizeof ( LST ) );

    lst[0].count = 0;

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

        scanf("%s", temp);

        if ( i == 0 ){

            strcpy( lst[i].name, temp );

            strcpy( output[i], "OK" );

            continue ;

        }

        ind = bul ( lst, temp, size );

        if ( ind == -1 ){

            size += 1 ;

            lst = realloc ( lst, sizeof ( LST ) * size ) ;

            lst[size-1].count = 0 ;

            strcpy( lst[size-1].name, temp ) ;

            strcpy( output[i], "OK" ) ;

        }

        else{

            lst[ind].count += 1;

            sprintf( buff, "%s%d", temp, lst[ind].count ) ;

            strcpy(output[i], buff );

        }

    }

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

        printf("%s\n", output[i]);

    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct LST{

    char name[33];

    int count;

} LST;

int bul ( LST* lst, char a[], int size ){

    int i;

    for ( i = 0; i < size; i++ ){

        if ( strcmp ( lst[i].name, a ) == 0 )

            return i;

    }

    return -1;

}

int main()
{
    int n, i, size = 1, ind;

    char temp[33] ;

    char buff[39] ;

    char** output ;

    LST* lst;

    scanf("%d", &n);

    output = malloc (sizeof ( char* ) * n );

    lst = malloc ( sizeof ( LST ) * size );

    lst[0].count = 0;

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

        scanf("%s", temp);

        if ( i == 0 ){

            strcpy( lst[i].name, temp );

            strcpy( output[i], "OK" );

        }

        else{

            ind = bul ( lst, temp, size );

            if ( ind == -1 ){

                size += 1 ;

                lst = realloc( lst, sizeof( LST ) * size ) ;

                lst[size-1].count = 0 ;

                strcpy( lst[size-1].name, temp ) ;

                strcpy( output[i], "OK" ) ;

            }

            else{

                lst[ind].count += 1;

                sprintf( buff, "%s%d", temp, lst[ind].count ) ;

                strcpy(output[i], buff );

            }

        }

    }

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

        printf("%s\n", output[i]);

    return 0;
}


#include "stdio.h"
#include "stdlib.h"

typedef struct node node;

int const ALPH = 26;

struct node {
  int cnt;
  node** next;
};

void clear_node(node * cur_node) {
  int i;
  cur_node->next = malloc(sizeof(node*) * ALPH);
  for (i = 0; i < ALPH; i++) {
    cur_node->next[i] = NULL;
  }
  cur_node->cnt = 0;
}

int add(char * s, node * cur_node) {
  if (*s == 0) {
    return cur_node->cnt++;
  } else {
    if (cur_node->next[(*s) - 'a'] == NULL) {
      cur_node->next[(*s) - 'a'] = malloc(sizeof(node));
      clear_node(cur_node->next[(*s) - 'a']);
    }
    add(s + 1, cur_node->next[(*s) - 'a']);
  }
}

void free_node(node * cur_node) {
  int i;
  for (i = 0; i < ALPH; i++) {
    if (cur_node->next[i] != NULL) {
      free_node(cur_node->next[i]);
    }
  }
  free(cur_node->next);
  free(cur_node);
}

int main() {
  node* root = malloc(sizeof(node));
  clear_node(root);
  int n;
  scanf("%d", &n);
  char s[33];
  for (; n > 0; n--) {
    scanf("%s", &s);
    int value = add(s, root);
    if (value == 0) {
      printf("OK\n");
    } else {
      printf("%s%d\n", s, value);
    }
  }
  free_node(root);
  return 0;
}


#include "stdio.h"
#include "stdlib.h"

typedef struct node node;

int const ALPH = 26;

struct node {
  int cnt;
  node** next;
};

void clear_node(node * cur_node) {
  int i;
  cur_node->next = malloc(sizeof(node*) * ALPH);
  for (i = 0; i < ALPH; i++) {
    cur_node->next[i] = NULL;
  }
  cur_node->cnt = 0;
}

int add(char * s, node * cur_node) {
  if (*s == 0) {
    return cur_node->cnt++;
  } else {
    if (cur_node->next[(*s) - 'a'] == NULL) {
      cur_node->next[(*s) - 'a'] = malloc(sizeof(node));
      clear_node(cur_node->next[(*s) - 'a']);
    }
    add(s + 1, cur_node->next[(*s) - 'a']);
  }
}

void free_node(node * cur_node) {
  int i;
  for (i = 0; i < ALPH; i++) {
    if (cur_node->next[i] != NULL) {
      free_node(cur_node->next[i]);
    }
  }
  free(cur_node->next);
  free(cur_node);
}

void my_read(char * s) {
  char * start = s;
  while (1) {
    *s = getchar();
    if (*s >= 'a' && *s <= 'z') {
      *s++;
    } else {
      if (start != s) {
        *s = 0;
        break;
      }
    }
  }
}

int main() {
  node* root = malloc(sizeof(node));
  clear_node(root);
  int n;
  scanf("%d", &n);
  char s[33];
  for (; n > 0; n--) {
    my_read(s);
    int value = add(s, root);
    if (value == 0) {
      printf("OK\n");
    } else {
      printf("%s%d\n", s, value);
    }
  }
  free_node(root);
  return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define HASH 100001
struct list{
char *s;
int n;
struct list * next;
};
struct list *table[HASH];
char *strdup(char *s)
{
	char *p;
	p = (char *) malloc(strlen(s)+1);
	if (p != NULL)
	strcpy(p, s);
	return p;
}
unsigned int hash(char *s)
{
	unsigned int hashval=0;
	for(;*s!='\0';s++)hashval= (hashval >> 5) +*s;
	return hashval%HASH;
}

struct list *find(char *s)
{
	struct list *li;
	for(li=table[hash(s)];li!=NULL;li=li->next)
	{
		if(strcmp(s,li->s)==0) return li;
	}
		return NULL;
}
void insert(char *s)
{
	struct list *li,*zx;

	li=find(s);
	if(li==NULL)
	{
		unsigned int hashval=hash(s);
		zx=(struct list*)malloc(sizeof(*zx));
		zx->next=table[hashval];
		table[hashval]=zx;
		zx->s=strdup(s);;
		zx->n=0;
		printf("OK\n");
	}
	else
	{
		li->n++;
		printf("%s%d\n",s,li->n);
	}
}
int main()
{
	int n,i;
	char s[33];
	scanf("%d",&n);
	getchar();
	for(i=0;i<n;i++)
	{
		gets(s);
		insert(s);
	}
return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define HASH 100001

struct list{
char *s;
int n;
struct list * next;
};
struct list *table[HASH];
char *stpu(char *s)
{
    char *p;
    p = (char *) malloc(strlen(s)+1);
    if (p != NULL)
    strcpy(p, s);
    return p;
}
unsigned int hash(char *s)
{
    unsigned int hashval=0;
    for(;*s!='\0';s++)hashval= (hashval >> 5) +*s;
    return hashval%HASH;
}

struct list *find(char *s)
{
    struct list *li;
    for(li=table[hash(s)];li!=NULL;li=li->next)
    {
        if(strcmp(s,li->s)==0) return li;
    }
        return NULL;
}
void insert(char *s)
{
    struct list *li,*zx;

    li=find(s);
    if(li==NULL)
    {
        unsigned int hashval=hash(s);
        zx=(struct list*)malloc(sizeof(*zx));
        zx->next=table[hashval];
        table[hashval]=zx;
        zx->s=stpu(s);
        zx->n=0;
        printf("OK\n");return;
    }
    else
    {
        li->n++;
        printf("%s%d\n",s,li->n);return;
    }
}
int main()
{
    int n,i;
    char s[33];
    scanf("%d",&n);
    getchar();
    for(i=0;i<n;i++)
    {
        gets(s);
        insert(s);
    }
return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
    char s[10001][33];
    int c[10001]={0};
    int n,i,j;
    scanf("%d",&n);
    getchar();
    for(i=0;i<n;i++)
    {
        gets(s[i]);
        for(j=0;j<i;j++)
            {
                if(!strcmp(s[i],s[j]))
                {
                    c[j]++;
                    printf("%s%d\n",s[i],c[j]);
                    break;
                }
            }
        if(c[j]==0)printf("OK\n");
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct list_el { struct list_el *next; char data[50]; } list;
list *table[10001];

void add(char *key)
{
	int i,k=0;
	for(i=0;key[i]!=0;++i) k+=key[i];
	k%=10001;

	list *curr=(list*)malloc(sizeof(list));
	strcpy(curr->data,key);
	curr->next=table[k];
	table[k]=curr;
}
int search(char *key)
{
	int i,k=0;
	for(i=0;key[i]!=0;++i) k+=key[i];
	k%=10001;

	list *curr=table[k];
	while(curr!=NULL){
		if(!strcmp(curr->data,key)) return 1;
		curr=curr->next;
	}
	return 0;
}
transform(int x,char *number)
{
	int i=0,j;

	while(x>0){ number[i++]=x%10+'0';x/=10; }
	number[i--]=0;
	for(j=0;j<i;++j,--i){
		char t=number[i];
		number[i]=number[j];
		number[j]=t;
	}
}
int main(void)
{
	int n,i,j;
	char name[50],number[50];
	scanf("%d",&n);
	for(i=0;i<n;++i){
		scanf("%s",name);
		if(!search(name)){ add(name); printf("OK"); }
		else{
			for(j=0;name[j]!=0;++j);
			do{
				int k=1;
				transform(k++,number);
				strcpy(&name[j],number);
			}
			while(search(name));
			add(name);
			printf("%s",name);
		}
		if(i<n-1) printf("\n");
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct list_el { struct list_el *next; char data[50]; } list;
list *table[10001];

void add(char *key)
{
	int i,k=0;
	for(i=0;key[i]!=0;++i) k+=key[i];
	k%=100;

	list *curr=(list*)malloc(sizeof(list));
	strcpy(curr->data,key);
	curr->next=table[k];
	table[k]=curr;
}
int search(char *key)
{
	int i,k=0;
	for(i=0;key[i]!=0;++i) k+=key[i];
	k%=100;

	list *curr=table[k];
	while(curr!=NULL){
		if(!strcmp(curr->data,key)) return 1;
		curr=curr->next;
	}
	return 0;
}
transform(int x,char *number)
{
	int i=0,j;

	while(x>0){ number[i++]=x%10+'0';x/=10; }
	number[i--]=0;
	for(j=0;j<i;++j,--i){
		char t=number[i];
		number[i]=number[j];
		number[j]=t;
	}
}
int main(void)
{
	int n,i,j;
	char name[50],number[50];
	scanf("%d",&n);
	for(i=0;i<n;++i){
		scanf("%s",name);
		if(!search(name)){ add(name); printf("OK"); }
		else{
			for(j=0;name[j]!=0;++j);
			int k=1;
			do{
				transform(k++,number);
				strcpy(&name[j],number);
			}
			while(search(name));
			add(name);
			printf("%s",name);
		}
		if(i<n-1) printf("\n");
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct list_el { struct list_el *next; char data[50]; } list;
list *table[10001];

void add(char *key)
{
	int i,k=0;
	for(i=0;key[i]!=0;++i) k+=key[i];
	k%=10001;

	list *curr=(list*)malloc(sizeof(list));
	strcpy(curr->data,key);
	curr->next=table[k];
	table[k]=curr;
}
int search(char *key)
{
	int i,k=0;
	for(i=0;key[i]!=0;++i) k+=key[i];
	k%=10001;

	list *curr=table[k];
	while(curr!=NULL){
		if(!strcmp(curr->data,key)) return 1;
		curr=curr->next;
	}
	return 0;
}
char* transform(int x)
{
	char number[10];
	int i=0,j;

	while(x>0){ number[i++]=x%10+'0';x/=10; }
	number[i--]=0;
	for(j=0;j<i;++j,--i){
		char t=number[i];
		number[i]=number[j];
		number[j]=t;
	}
	return number;
}
int main(void)
{
	int n,i,j;
	char name[50];
	scanf("%d",&n);
	for(i=0;i<n;++i){
		scanf("%s",name);
		if(!search(name)){ add(name); printf("OK"); }
		else{
			for(j=0;name[j]!=0;++j);
			do{
				int k=1;
				strcpy(&name[j],transform(k++));
			}
			while(search(name));
			add(name);
			printf("%s",name);
		}
		if(i<n-1) printf("\n");
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct list_el { struct list_el *next; int num; char data[50]; } list;
list *table[100007];

void add(char *key)
{
	int i;
	unsigned long long k=7;
	for(i=0;key[i]!=0;++i) k+=k*31+key[i];
	k%=100007;

	list *curr=(list*)malloc(sizeof(list));
	strcpy(curr->data,key);
	curr->num=1;
	curr->next=table[k];
	table[k]=curr;
}
list* search(char *key)
{
	int i;
	unsigned long long k=7;
	for(i=0;key[i]!=0;++i) k+=k*31+key[i];
	k%=100007;

	list *curr=table[k];
	while(curr!=NULL){
		if(!strcmp(curr->data,key)) return curr;
		curr=curr->next;
	}
	return NULL;
}
transform(int x,char *number)
{
	int i=0,j;

	while(x>0){ number[i++]=x%10+'0';x/=10; }
	number[i--]=0;
	for(j=0;j<i;++j,--i){
		char t=number[i];
		number[i]=number[j];
		number[j]=t;
	}
}
int main(void)
{
	int n,i,j;
	char name[50],number[50];
	list* temp;

	scanf("%d",&n);
	for(i=0;i<n;++i){
		scanf("%s",name);
		temp=search(name);
		if(temp==NULL){ add(name); printf("OK"); }
		else{
			for(j=0;name[j]!=0;++j);
			transform(temp->num++,number);
			strcpy(&name[j],number);
			add(name);
			printf("%s",name);
		}
		if(i<n-1) printf("\n");
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct list_el { struct list_el *next; char data[50]; } list;
list *table[4321];

void add(char *key)
{
	int i;
	unsigned long long k=7;
	for(i=0;key[i]!=0;++i) k+=k*31+key[i];
	k%=4321;

	list *curr=(list*)malloc(sizeof(list));
	strcpy(curr->data,key);
	curr->next=table[k];
	table[k]=curr;
}
int search(char *key)
{
	int i;
	unsigned long long k=7;
	for(i=0;key[i]!=0;++i) k+=k*31+key[i];
	k%=4321;

	list *curr=table[k];
	while(curr!=NULL){
		if(!strcmp(curr->data,key)) return 1;
		curr=curr->next;
	}
	return 0;
}
transform(int x,char *number)
{
	int i=0,j;

	while(x>0){ number[i++]=x%10+'0';x/=10; }
	number[i--]=0;
	for(j=0;j<i;++j,--i){
		char t=number[i];
		number[i]=number[j];
		number[j]=t;
	}
}
int main(void)
{
	int n,i,j;
	char name[50],number[50];
	scanf("%d",&n);
	for(i=0;i<n;++i){
		scanf("%s",name);
		if(!search(name)){ add(name); printf("OK"); }
		else{
			for(j=0;name[j]!=0;++j);
			int k=1;
			do{
				transform(k++,number);
				strcpy(&name[j],number);
			}
			while(search(name));
			add(name);
			printf("%s",name);
		}
		if(i<n-1) printf("\n");
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct list_el { struct list_el *next; char data[50]; } list;
list *table[1000];

void add(char *key)
{
	int i,k=7;
	for(i=0;key[i]!=0;++i) k+=k*31+key[i];
	k%=1000;

	list *curr=(list*)malloc(sizeof(list));
	strcpy(curr->data,key);
	curr->next=table[k];
	table[k]=curr;
}
int search(char *key)
{
	int i,k=7;
	for(i=0;key[i]!=0;++i) k+=k*31+key[i];
	k%=1000;

	list *curr=table[k];
	while(curr!=NULL){
		if(!strcmp(curr->data,key)) return 1;
		curr=curr->next;
	}
	return 0;
}
transform(int x,char *number)
{
	int i=0,j;

	while(x>0){ number[i++]=x%10+'0';x/=10; }
	number[i--]=0;
	for(j=0;j<i;++j,--i){
		char t=number[i];
		number[i]=number[j];
		number[j]=t;
	}
}
int main(void)
{
	int n,i,j;
	char name[50],number[50];
	scanf("%d",&n);
	for(i=0;i<n;++i){
		scanf("%s",name);
		if(!search(name)){ add(name); printf("OK"); }
		else{
			for(j=0;name[j]!=0;++j);
			int k=1;
			do{
				transform(k++,number);
				strcpy(&name[j],number);
			}
			while(search(name));
			add(name);
			printf("%s",name);
		}
		if(i<n-1) printf("\n");
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct list_el { struct list_el *next; char data[50]; } list;
list *table[10001];

void add(char *key)
{
	int i,k=0;
	for(i=0;key[i]!=0;++i) k+=key[i];
	k%=10001;

	list *curr=(list*)malloc(sizeof(list));
	strcpy(curr->data,key);
	curr->next=table[k];
	table[k]=curr;
}
int search(char *key)
{
	int i,k=0;
	for(i=0;key[i]!=0;++i) k+=key[i];
	k%=10001;

	list *curr=table[k];
	while(curr!=NULL){
		if(!strcmp(curr->data,key)) return 1;
		curr=curr->next;
	}
	return 0;
}
transform(int x,char *number)
{
	int i=0,j;

	while(x>0){ number[i++]=x%10+'0';x/=10; }
	number[i--]=0;
	for(j=0;j<i;++j,--i){
		char t=number[i];
		number[i]=number[j];
		number[j]=t;
	}
}
int main(void)
{
	int n,i,j;
	char name[50],number[50];
	scanf("%d",&n);
	for(i=0;i<n;++i){
		scanf("%s",name);
		if(!search(name)){ add(name); printf("OK"); }
		else{
			for(j=0;name[j]!=0;++j);
			int k=1;
			do{
				transform(k++,number);
				strcpy(&name[j],number);
			}
			while(search(name));
			add(name);
			printf("%s",name);
		}
		if(i<n-1) printf("\n");
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct list_el { struct list_el *next; char data[50]; } list;
list *table[10001];

void add(char *key)
{
	int i,k=0;
	for(i=0;key[i]!=0;++i) k+=key[i];
	k%=500;

	list *curr=(list*)malloc(sizeof(list));
	strcpy(curr->data,key);
	curr->next=table[k];
	table[k]=curr;
}
int search(char *key)
{
	int i,k=0;
	for(i=0;key[i]!=0;++i) k+=key[i];
	k%=500;

	list *curr=table[k];
	while(curr!=NULL){
		if(!strcmp(curr->data,key)) return 1;
		curr=curr->next;
	}
	return 0;
}
transform(int x,char *number)
{
	int i=0,j;

	while(x>0){ number[i++]=x%10+'0';x/=10; }
	number[i--]=0;
	for(j=0;j<i;++j,--i){
		char t=number[i];
		number[i]=number[j];
		number[j]=t;
	}
}
int main(void)
{
	int n,i,j;
	char name[50],number[50];
	scanf("%d",&n);
	for(i=0;i<n;++i){
		scanf("%s",name);
		if(!search(name)){ add(name); printf("OK"); }
		else{
			for(j=0;name[j]!=0;++j);
			int k=1;
			do{
				transform(k++,number);
				strcpy(&name[j],number);
			}
			while(search(name));
			add(name);
			printf("%s",name);
		}
		if(i<n-1) printf("\n");
	}
	return 0;
}


/*
 * CodeForces
 * Problem 4C
 * GNU/C
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAXN 100000
#define MAXS 33

typedef struct TVector
{
	char *names[MAXN];
	int copies[MAXN];
	long size;
} Vector;

void addName(char*, Vector*, const int);
int isNameExist(const char*, const Vector*);
void registerName(char*, Vector*);

int main() {
	long n;            // Number of names
	Vector database;
	int i;

	database.size = 0;
	memset(database.copies, 0, MAXN);
	scanf("%ld", &n);

	for (i=0; i<n; i++) {
		char name[MAXS];

		scanf("%s", name);

		registerName(name, &database);
	}

	return 0;
}

// Add new name to the database
void addName(char *name, Vector *database, const int indexOfOrigin)
{
	database->names[database->size] = (char*)malloc(MAXS);
	strcpy(database->names[database->size], name);

	if (indexOfOrigin > -1) {
		database->copies[indexOfOrigin]++;
	}

	database->size++;
}

// Rigester new name in the database
void registerName(char *name, Vector *database)
{
	int namePos = isNameExist(name, database);

	if(namePos == -1) {
		addName(name, database, namePos);
		printf("OK\n");
	}
	else {
		// Add modified unique name
		char converted[MAXN];
		int i = database->copies[namePos] + 1;

		sprintf(converted, "%d", i);
		strcat(name, converted);

		addName(name, database, namePos);
		printf("%s\n", database->names[database->size-1]);
	}
}

// Does name is existed in the database
// Resturns index of the name position
// If name doesn't exist - return -1
int isNameExist(const char *name, const Vector *database)
{
	int i;

	for(i=0; i<database->size; i++) {
		if(strcmp(name, database->names[i]) == 0) {
			return i;
		}
	}

	return -1;
}


/*
 * CodeForces
 * Problem 4C
 * GNU/C
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAXN 100000
#define MAXS 50

typedef struct TVector
{
	char *names[MAXN];
	int copies[MAXN];
	long size;
} Vector;

void addName(char*, Vector*, const int);
int isNameExist(const char*, const Vector*);
void registerName(char*, Vector*);

int main() {
	long n;            // Number of names
	Vector database;
	int i;

	database.size = 0;
	memset(database.copies, 0, MAXN);
	scanf("%ld", &n);

	for (i=0; i<n; i++) {
		char name[MAXS];

		scanf("%s", name);

		registerName(name, &database);
	}

	return 0;
}

// Add new name to the database
void addName(char *name, Vector *database, const int indexOfOrigin)
{
	if (indexOfOrigin < 0) {
		database->names[database->size] = (char*)malloc(MAXS);
		strcpy(database->names[database->size], name);
		database->size++;
	}
	else if (indexOfOrigin > -1) {
		database->copies[indexOfOrigin]++;
	}

}

// Rigester new name in the database
void registerName(char *name, Vector *database)
{
	int namePos = isNameExist(name, database);

	addName(name, database, namePos);

	if(namePos == -1) {
		printf("OK\n");
	}
	else {
		printf("%s%d\n", database->names[database->size-1], database->copies[namePos]);
	}
}

// Does name is existed in the database
// Resturns index of the name position
// If name doesn't exist - return -1
int isNameExist(const char *name, const Vector *database)
{
	int i;

	for(i=0; i<database->size; i++) {
		if(strcmp(name, database->names[i]) == 0) {
			return i;
		}
	}

	return -1;
}


/*
 * CodeForces
 * Problem 4C
 * GNU/C
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAXN 100000
#define MAXS 50

typedef struct TVector
{
	char *names[MAXN];
	int copies[MAXN];
	long size;
} Vector;

void addName(char*, Vector*, const int);
int isNameExist(const char*, const Vector*);
void registerName(char*, Vector*);

int main() {
	long n;            // Number of names
	Vector database;
	int i;

	database.size = 0;
	memset(database.copies, 0, MAXN);
	scanf("%ld", &n);

	for (i=0; i<n; i++) {
		char name[MAXS];

		scanf("%s", name);

		registerName(name, &database);
	}

	return 0;
}

// Add new name to the database
void addName(char *name, Vector *database, const int indexOfOrigin)
{
	database->names[database->size] = (char*)malloc(MAXS);
	strcpy(database->names[database->size], name);

	if (indexOfOrigin > -1) {
		database->copies[indexOfOrigin]++;
	}

	database->size++;
}

// Rigester new name in the database
void registerName(char *name, Vector *database)
{
	int namePos = isNameExist(name, database);
	if(namePos == -1) {
		addName(name, database, namePos);
		printf("OK\n");
	}
	else {
		// Add modified unique name
		char converted[MAXN];
		int i = database->copies[namePos] + 1;

		sprintf(converted, "%d", i);
		strcat(name, converted);

		addName(name, database, namePos);
		printf("%s\n", database->names[database->size-1]);
	}
}

// Does name is existed in the database
// Resturns index of the name position
// If name doesn't exist - return -1
int isNameExist(const char *name, const Vector *database)
{
	int i;

	for(i=0; i<database->size; i++) {
		if(strcmp(name, database->names[i]) == 0) {
			return i;
		}
	}

	return -1;
}


/*
 * CodeForces
 * Problem 4C
 * GNU/C
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAXN 100000
#define MAXS 50

typedef struct TVector
{
	char *names[MAXN];
	int copies[MAXN];
	long size;
} Vector;

void addName(char*, Vector*, const int);
int isNameExist(const char*, const Vector*);
void registerName(char*, Vector*);

int main() {
	long n;            // Number of names
	Vector database;
	int i;

	database.size = 0;
	memset(database.copies, 0, MAXN);
	scanf("%ld", &n);

	for (i=0; i<n; i++) {
		char name[MAXS];

		scanf("%s", name);

		registerName(name, &database);
	}

	return 0;
}

// Add new name to the database
void addName(char *name, Vector *database, const int indexOfOrigin)
{
	database->names[database->size] = (char*)malloc(MAXS);
	strcpy(database->names[database->size], name);

	if (indexOfOrigin > -1) {
		database->copies[indexOfOrigin]++;
	}

	database->size++;
}

// Rigester new name in the database
void registerName(char *name, Vector *database)
{
	int namePos = isNameExist(name, database);

	addName(name, database, namePos);

	if(namePos == -1) {
		printf("OK\n");
	}
	else {
		printf("%s%d\n", database->names[database->size-1], database->copies[namePos]);
	}
}

// Does name is existed in the database
// Resturns index of the name position
// If name doesn't exist - return -1
int isNameExist(const char *name, const Vector *database)
{
	int i;

	for(i=0; i<database->size; i++) {
		if(strcmp(name, database->names[i]) == 0) {
			return i;
		}
	}

	return -1;
}


/*
 * CodeForces
 * Problem 4C
 * GNU/C
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAXN 100000
#define MAXS 50

typedef struct TVector
{
	char *names[MAXN];
	int copies[MAXN];
	long size;
} Vector;

void addName(char*, Vector*, const int);
int isNameExist(const char*, const Vector*);
void registerName(char*, Vector*);

int main() {
	long n;            // Number of names
	Vector database;
	int i;

	database.size = 0;
	memset(database.copies, 0, MAXN);
	scanf("%ld", &n);

	for (i=0; i<n; i++) {
		char name[MAXS];

		scanf("%s", name);

		registerName(name, &database);
	}

	return 0;
}

// Add new name to the database
void addName(char *name, Vector *database, const int indexOfOrigin)
{
	if (indexOfOrigin < 0) {
		database->names[database->size] = (char*)malloc(MAXS);
		strcpy(database->names[database->size], name);
		database->size++;
	}
	else if (indexOfOrigin > -1) {
		database->copies[indexOfOrigin]++;
	}

}

// Rigester new name in the database
void registerName(char *name, Vector *database)
{
	int namePos = isNameExist(name, database);

	addName(name, database, namePos);

	if(namePos == -1) {
		printf("OK\n");
	}
	else {
		printf("%s%d\n", database->names[namePos], database->copies[namePos]);
	}
}

// Does name is existed in the database
// Resturns index of the name position
// If name doesn't exist - return -1
int isNameExist(const char *name, const Vector *database)
{
	int i;

	for(i=0; i<database->size; i++) {
		if(strcmp(name, database->names[i]) == 0) {
			return i;
		}
	}

	return -1;
}


#include<stdio.h>
#include<string.h>
void read_word(char word[]){
	int i;
	char c;
	for(i = 0; (c = getchar()) != EOF && c != '\n'; i++)
		word[i] = c;
	word[i] = '\0';
}

int search(char names[][40], char name[], int n){
	int i;
	for(i = 0; i < n; i++)
		if(strcmp(names[i], name) == 0)
			return 1;
	return 0;
}

void rewrite(char name[], int index){
	char *tmp = name + strlen(name);
	//��������, ��� ���������� ���� �� ������ 9
	*tmp = index + '0';
	*(tmp+1) = '\0';
}

int main(void){
	char names[100100][40];
	int n;
	scanf("%d\n", &n);
	int i;
	for(i = 0; i < n; i++)
		read_word(names[i]);

	for(i = 0; i < n; i++){
		int index;
		for(index = 1; search(names, names[i], i); index++)
			rewrite(names[i], index);
		if(index < 2)
			puts("ok");
		else puts(names[i]);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
void read_word(char word[]){
	int i;
	char c;
	for(i = 0; (c = getchar()) != EOF && c != '\n'; i++)
		word[i] = c;
	word[i] = '\0';
}

int search(char names[][40], char name[], int n){
	int i;
	for(i = 0; i < n; i++)
		if(strcmp(names[i], name) == 0)
			return 1;
	return 0;
}

void rewrite(char name[], int index){
	char *tmp = name + strlen(name);
	//��������, ��� ���������� ���� �� ������ 9
	*tmp = index + '0';
	*(tmp+1) = '\0';
}

int main(void){
	char names[100100][40];
	int n;
	scanf("%d\n", &n);
	int i;
	for(i = 0; i < n; i++)
		read_word(names[i]);

	for(i = 0; i < n; i++){
		int index;
		for(index = 1; search(names, names[i], i); index++)
			rewrite(names[i], index);
		if(index < 2)
			puts("OK");
		else puts(names[i]);
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 	1000000
#define MOD	(size_t)(1e9 + 7)

typedef struct list {
	char str[33];
	struct list *next;
	int i;
} list;

void append(char *str, list **head) {
	list *n = malloc(sizeof(list));
	strcpy(n->str, str);
	n->i = 0;
	n->next = *head;
	*head = n;
}

void init(list *table[]) {
	int i;
	for (i = 0; i < N; i++)
		table[i] = NULL;
}

list* find(char str[33], list *head) {
	list *p;
	for (p = head; p; p = p->next)
		if (!strcmp(str, p->str))
			return p;
	return NULL;
}

unsigned hash(const char *str) {
	unsigned h = 0;
	const char *p;
	for (p = str; *p; p++) {
		h = 31 * h + (*str - 'a') + 1;
	}
	return h % N;
}

int main(void) {
	static list *table[N];
	init(table);

	int n;
	scanf("%d", &n);

	int i;
	for (i = 0; i < n; i++) {
		char str[33];
		scanf("%33s", str);
		unsigned h = hash(str);
		list *p = find(str, table[h]);
		if (!p) {
			append(str, &table[h]);
			puts("OK");
		}
		else
			printf("%s%d\n", p->str, (++p->i));
	}
	return EXIT_SUCCESS;
}


#include<stdio.h>
#include<string.h>

int search(char names[][60], char name[], int n){
	int i, count = 0;
	for(i = 0; i < n; i++)
		if(strcmp(names[i], name) == 0)
			count++;
	return count;
}

int main(void){
	char names[100100][60];
	int n;
	scanf("%d\n", &n);
	int i;
	for(i = 0; i < n; i++)
		scanf("%s", names[i]);

	for(i = 0; i < n; i++){
		int index = search(names, names[i], i);
		if(index > 0)
			printf("%s%d\n", names[i], index);
		else puts("OK");
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 1000000

typedef struct list {
	char str[33];
	struct list *next;
	int i;
} list;

void append(char *str, list **head) {
	list *n = malloc(sizeof(list));
	strcpy(n->str, str);
	n->i = 0;
	n->next = *head;
	*head = n;
}

void init(list *table[]) {
	int i;
	for (i = 0; i < N; i++)
		table[i] = NULL;
}

list* find(char str[33], list *head) {
	list *p;
	for (p = head; p; p = p->next)
		if (!strcmp(str, p->str))
			return p;
	return NULL;
}

unsigned hash(const char *str) {
	unsigned a = 33, b = 3333, c = 99999;
	unsigned h = 123;
	const char *p;
	for (p = str; *p; p++) {
		h = (h * a + (h ^ b) + (h & c)) * b - ~(h + c);
	}
	return h % N;
}

int main(void) {
	static list *table[N];
	init(table);

	int n;
	scanf("%d", &n);

	int i;
	for (i = 0; i < n; i++) {
		char str[33];
		scanf("%33s", str);
		unsigned h = hash(str);
		list *p = find(str, table[h]);
		if (!p) {
			append(str, &table[h]);
			printf("OK\n");
		}
		else {
			printf("%s%d\n", p->str, p->i);
			p->i++;
		}
	}
	return EXIT_SUCCESS;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 1000000

typedef struct list {
	char str[33];
	struct list *next;
	int i;
} list;

void append(char *str, list **head) {
	list *n = malloc(sizeof(list));
	strcpy(n->str, str);
	n->i = 0;
	n->next = *head;
	*head = n;
}

void init(list *table[]) {
	int i;
	for (i = 0; i < N; i++)
		table[i] = NULL;
}

list* find(char str[33], list *head) {
	list *p;
	for (p = head; p; p = p->next)
		if (!strcmp(str, p->str))
			return p;
	return NULL;
}

unsigned hash(const char *str) {
	unsigned a = 33, b = 3333, c = 99999;
	unsigned h = 123;
	const char *p;
	for (p = str; *p; p++) {
		h = (h * a + (h ^ b) + (h & c)) * b - ~(h + c);
	}
	return h % N;
}

int main(void) {
	static list *table[N];
	init(table);

	int n;
	scanf("%d", &n);

	int i;
	for (i = 0; i < n; i++) {
		char str[33];
		scanf("%33s", str);
		unsigned h = hash(str);
		list *p = find(str, table[h]);
		if (!p) {
			append(str, &table[h]);
			printf("OK\n");
		}
		else
			printf("%s%d\n", p->str, (++p->i));
	}
	return EXIT_SUCCESS;
}


#include<stdio.h>
#include<string.h>
void read_word(char word[]){
	int i;
	char c;
	for(i = 0; (c = getchar()) != EOF && c != '\n'; i++)
		word[i] = c;
	word[i] = '\0';
}

int search(char names[][40], char name[], int n){
	int i;
	for(i = 0; i < n; i++)
		if(strcmp(names[i], name) == 0)
			return 1;
	return 0;
}

int rewrite(char name[], int index, int back){
	char *tmp = name + strlen(name) - back;
	char a[10];
	int i;
	for(i = 0; index; i++){
		a[i] = index % 10 + '0';
		index /= 10;
	}
	int n = i;
	int j = 0;
	while(j > i){
		int tmp = a[i];
		a[i] = a[j];
		a[j] = tmp;
		i++;
		j++;
	}
	for(i = 0; i < n; i++){
		*tmp = a[i];
		tmp++;
	}
	*(tmp+1) = '\0';
	return n;
}

int main(void){
	char names[100100][40];
	int n;
	scanf("%d\n", &n);
	int i;
	for(i = 0; i < n; i++)
		read_word(names[i]);

	for(i = 0; i < n; i++){
		int index, back = 0;
		for(index = 1; search(names, names[i], i); index++)
			back = rewrite(names[i], index, back);
		if(index < 2)
			puts("OK");
		else puts(names[i]);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
void read_word(char word[]){
	int i;
	char c;
	for(i = 0; (c = getchar()) != EOF && c != '\n'; i++)
		word[i] = c;
	word[i] = '\0';
}

int search(char names[][60], char name[], int n){
	int i;
	for(i = 0; i < n; i++)
		if(strcmp(names[i], name) == 0)
			return 1;
	return 0;
}

int rewrite(char name[], int index, int back){
	char *tmp = name + strlen(name) - back;
	char a[10];
	int i;
	for(i = 0; index; i++){
		a[i] = index % 10 + '0';
		index /= 10;
	}
	int n = i;
	int j = 0;
	while(j > i){
		int tmp = a[i];
		a[i] = a[j];
		a[j] = tmp;
		i++;
		j++;
	}
	for(i = 0; i < n; i++){
		*tmp = a[i];
		tmp++;
	}
	*(tmp+1) = '\0';
	return n;
}

int main(void){
	char names[100100][60];
	int n;
	scanf("%d\n", &n);
	int i;
	for(i = 0; i < n; i++)
		read_word(names[i]);

	for(i = 0; i < n; i++){
		int index, back = 0;
		for(index = 1; search(names, names[i], i); index++)
			back = rewrite(names[i], index, back);
		if(index < 2)
			puts("OK");
		else puts(names[i]);
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 	1000000
#define MOD	(size_t)(1e9 + 7)

typedef struct list {
	char str[33];
	struct list *next;
	int i;
} list;

void append(char *str, list **head) {
	list *n = malloc(sizeof(list));
	strcpy(n->str, str);
	n->i = 0;
	n->next = *head;
	*head = n;
}

void init(list *table[]) {
	int i;
	for (i = 0; i < N; i++)
		table[i] = NULL;
}

list* find(char str[33], list *head) {
	list *p;
	for (p = head; p; p = p->next)
		if (!strcmp(str, p->str))
			return p;
	return NULL;
}

unsigned hash(const char *str) {
	unsigned a = 33, b = 3333, c = 99999;
	unsigned h = 123;
	const char *p;
	for (p = str; *p; p++) {
		h = h * MOD + *str;
	}
	return h % N;
}

int main(void) {
	static list *table[N];
	init(table);

	int n;
	scanf("%d", &n);

	int i;
	for (i = 0; i < n; i++) {
		char str[33];
		scanf("%33s", str);
		unsigned h = hash(str);
		list *p = find(str, table[h]);
		if (!p) {
			append(str, &table[h]);
			printf("OK\n");
		}
		else
			printf("%s%d\n", p->str, (++p->i));
	}
	return EXIT_SUCCESS;
}


#include<stdio.h>
#include<string.h>
void read_word(char word[]){
	int i;
	char c;
	for(i = 0; (c = getchar()) != EOF && c != '\n'; i++)
		word[i] = c;
	word[i] = '\0';
}

int search(char names[][40], char name[], int n){
	int i;
	for(i = 0; i < n; i++)
		if(strcmp(names[i], name) == 0)
			return 1;
	return 0;
}

void rewrite(char name[], int index){
	char *tmp = name + strlen(name);
	if (index > 1) tmp--;
	//��������, ��� ���������� ���� �� ������ 9
	*tmp = index + '0';
	*(tmp+1) = '\0';
}

int main(void){
	char names[100100][40];
	int n;
	scanf("%d\n", &n);
	int i;
	for(i = 0; i < n; i++)
		read_word(names[i]);

	for(i = 0; i < n; i++){
		int index;
		for(index = 1; search(names, names[i], i); index++)
			rewrite(names[i], index);
		if(index < 2)
			puts("OK");
		else puts(names[i]);
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 	1000000
#define MOD	(size_t)(1e9 + 7)

typedef struct list {
	char str[33];
	struct list *next;
	int i;
} list;

void append(char *str, list **head) {
	list *n = malloc(sizeof(list));
	strcpy(n->str, str);
	n->i = 0;
	n->next = *head;
	*head = n;
}

void init(list *table[]) {
	int i;
	for (i = 0; i < N; i++)
		table[i] = NULL;
}

list* find(char str[33], list *head) {
	list *p;
	for (p = head; p; p = p->next)
		if (!strcmp(str, p->str))
			return p;
	return NULL;
}

unsigned hash(const char *str) {
	unsigned a = 33, b = 3333, c = 99999;
	unsigned h = 123;
	const char *p;
	for (p = str; *p; p++) {
		h = h * (*str ^ a) * b;
	}
	return h % N;
}

int main(void) {
	static list *table[N];
	init(table);

	int n;
	scanf("%d", &n);

	int i;
	for (i = 0; i < n; i++) {
		char str[33];
		scanf("%33s", str);
		unsigned h = hash(str);
		list *p = find(str, table[h]);
		if (!p) {
			append(str, &table[h]);
			printf("OK\n");
		}
		else
			printf("%s%d\n", p->str, (++p->i));
	}
	return EXIT_SUCCESS;
}


#include<stdio.h>
#include<string.h>
void read_word(char word[]){
	int i;
	char c;
	for(i = 0; (c = getchar()) != EOF && c != '\n'; i++)
		word[i] = c;
	word[i] = '\0';
}

int search(char names[][60], char name[], int n){
	int i, count = 0;
	for(i = 0; i < n; i++)
		if(strcmp(names[i], name) == 0)
			count++;
	return count;
}

int main(void){
	char names[100100][60];
	int indexs[100100] = {0};
	int n;
	scanf("%d\n", &n);
	int i;
	for(i = 0; i < n; i++)
		read_word(names[i]);

	for(i = 0; i < n; i++){
		indexs[i] = search(names, names[i], i);
		if(indexs[i] > 0)
			printf("%s%d\n", names[i], indexs[i]);
		else puts("OK");
	}
	return 0;
}


#include<stdio.h>
#include<string.h>

int main()
{
    char d[10001][33],temp[33];
    int a[100001];
    int n,i,b,s,j;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		b=0;
		gets(temp);
		for(j=0;j<s;j++)
        {
			if(strcmp(temp,d[j])==0)
            {
				b=1;
				a[j]++;
				break;
			}
		}
		if(b)
            printf("%s%d\n",d[j],a[j]);
		else
		{
			strcpy(d[j],temp);
			s++;
			puts("OK");
		}
	}
	return 0;
}


#include<stdio.h>
#include<string.h>

int main()
{
	int n,i,j,s=0;
	scanf("%d\n",&n);
	int b[n+1];
	char a[n][33],c[33];
	for(i=0;i<n;i++)
	    b[i]=0;
	for(i=0;i<n;i++)
	{
		gets(c);
		for(j=0;j<s;j++)
		{
			if(strcmp(c,a[j])==0)
			break;
		}
		if(j==s)
		{
		    printf("OK\n");
		    strcpy(a[s++],c);
        }
		else
		{
			b[j]++;
			printf("%s%d\n",c,b[j]);
		}
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct list {
	char *str;
	int count;
	struct list *next;
};

struct hash_table {
	int size;
	struct list **table;
};

struct hash_table *
ht_create(int size)
{
	int i;
	struct hash_table *new_table;

	if (size < 1) return NULL;

	if ((new_table = malloc(sizeof(struct hash_table))) == NULL) {
		return NULL;
	}
	if ((new_table->table = malloc(sizeof(struct list *) * size)) == NULL) {
		return NULL;
	}

	for(i = 0; i < size; i++) new_table->table[i] = NULL;
	new_table->size = size;

	return new_table;
}

void
ht_destroy(struct hash_table *ht)
{
	int i;
	struct list *list, *tmp;
	if (ht == NULL) return;

	for(i = 0; i < ht->size; i++) {
		list = ht->table[i];
		while(list != NULL) {
			tmp = list;
			list = list->next;
			free(tmp->str);
			free(tmp);
		}
	}

	free(ht->table);
	free(ht);
}

unsigned int
hash(struct hash_table *ht, char *str)
{
	unsigned int hashval;

	hashval = 0;
	for (; *str != '\0'; str++) hashval = *str + (hashval << 5) - hashval;

	return hashval % ht->size;
}

struct list *
ht_lookup(struct hash_table *ht, char *str)
{
	struct list *list;
	unsigned int hashval = hash(ht, str);

	for (list = ht->table[hashval]; list != NULL; list = list->next) {
		if (strcmp(str, list->str) == 0) return list;
	}
	return NULL;
}

int
ht_add(struct hash_table *ht, char *str)
{
	struct list *new_list;
	unsigned int hashval = hash(ht, str);

	if ((new_list = malloc(sizeof(struct list))) == NULL) return 1;

	new_list->str = strdup(str);
	new_list->count = 1;
	new_list->next = ht->table[hashval];
	ht->table[hashval] = new_list;

	return 0;
}

int
main(int argc, char *argv[])
{
	int n, i;
	char line[32], newname[32];
	struct list *tmp;
	struct hash_table *ht;

	/* Ввод кол-ва строк */
	scanf("%d", &n);
	ht = ht_create(n);

	/* пока не конец ввода */
	for (i = 0; i < n; i++) {
		scanf("%s", line);

		/* если имя уже есть в хеш-таблице */
		if (tmp = ht_lookup(ht, line)) {
			/* сгенерировать новое имя */
			sprintf(newname, "%s%d", tmp->str, tmp->count++);
			/* сохранить новое имя  */
			ht_add(ht, newname);
			/* вывести новое имя */
			printf("%s\n", newname);

		/* иначе */
		} else {
			/* сохранить имя */
			ht_add(ht, line);
			/* вывести OK */
			printf("OK\n");
		}
	}

	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct list {
	char *str;
	struct list *next;
};

struct hash_table {
	int size;
	struct list **table;
};

struct hash_table *
ht_create(int size)
{
	int i;
	struct hash_table *new_table;

	if (size < 1) return NULL;

	if ((new_table = malloc(sizeof(struct hash_table))) == NULL) {
		return NULL;
	}
	if ((new_table->table = malloc(sizeof(struct list *) * size)) == NULL) {
		return NULL;
	}

    for(i = 0; i < size; i++) new_table->table[i] = NULL;
    new_table->size = size;

    return new_table;
}

void
ht_destroy(struct hash_table *ht)
{
	int i;
	struct list *list, *tmp;
	if (ht == NULL) return;

	for(i = 0; i < ht->size; i++) {
		list = ht->table[i];
		while(list != NULL) {
			tmp = list;
			list = list->next;
			free(tmp->str);
			free(tmp);
		}
	}

	free(ht->table);
	free(ht);
}

unsigned int
hash(struct hash_table *ht, char *str)
{
	unsigned int hashval;

	hashval = 0;
	for(; *str != '\0'; str++) hashval = *str + (hashval << 5) - hashval;

	return hashval % ht->size;
}

struct list *
ht_lookup(struct hash_table *ht, char *str)
{
	struct list *list;
	unsigned int hashval = hash(ht, str);

	for(list = ht->table[hashval]; list != NULL; list = list->next) {
		if (strcmp(str, list->str) == 0) return list;
	}
	return NULL;
}

int
ht_add(struct hash_table *ht, char *str)
{
	struct list *new_list;
	unsigned int hashval = hash(ht, str);

	if ((new_list = malloc(sizeof(struct list))) == NULL) return 1;

	new_list->str = strdup(str);
	new_list->next = ht->table[hashval];
	ht->table[hashval] = new_list;

	return 0;
}

int
main(int argc, char *argv[])
{
	int n, i, j;
	char line[32], newname[32];
	struct list *tmp;
	struct hash_table *ht;

	/* Ввод кол-ва строк */
	scanf("%d", &n);
	ht = ht_create(n);

	/* пока не конец ввода */
	for (i = 0; i < n; i++) {
		scanf("%s", line);

		/* если имя уже есть в хеш-таблице */
		if (tmp = ht_lookup(ht, line)) {
			/* сгенерировать новое имя */
			j = 1;
			do {
				snprintf(newname, 32, "%s%d", line, j++);
			} while ((tmp = ht_lookup(ht, newname)) != NULL);
			/* сохранить новое имя  */
			ht_add(ht, newname);
			/* вывести новое имя */
			printf("%s\n", newname);

		/* иначе */
		} else {
			/* сохранить имя */
			ht_add(ht, line);
			/* вывести OK */
			printf("OK\n");
		}
	}

	ht_destroy(ht);

	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct list {
	char *str;
	int count;
	struct list *next;
};

struct hash_table {
	int size;
	struct list **table;
};

struct hash_table *
ht_create(int size)
{
	int i;
	struct hash_table *new_table;

	if (size < 1) return NULL;

	if ((new_table = malloc(sizeof(struct hash_table))) == NULL) {
		return NULL;
	}
	if ((new_table->table = malloc(sizeof(struct list *) * size)) == NULL) {
		return NULL;
	}

	for(i = 0; i < size; i++) new_table->table[i] = NULL;
	new_table->size = size;

	return new_table;
}

void
ht_destroy(struct hash_table *ht)
{
	int i;
	struct list *list, *tmp;
	if (ht == NULL) return;

	for(i = 0; i < ht->size; i++) {
		list = ht->table[i];
		while(list != NULL) {
			tmp = list;
			list = list->next;
			free(tmp->str);
			free(tmp);
		}
	}

	free(ht->table);
	free(ht);
}

unsigned int
hash(struct hash_table *ht, char *str)
{
	unsigned int hashval;

	hashval = 0;
	for (; *str != '\0'; str++) hashval = *str + (hashval << 5) - hashval;

	return hashval % ht->size;
}

struct list *
ht_lookup(struct hash_table *ht, char *str)
{
	struct list *list;
	unsigned int hashval = hash(ht, str);

	for (list = ht->table[hashval]; list != NULL; list = list->next) {
		if (strcmp(str, list->str) == 0) return list;
	}
	return NULL;
}

int
ht_add(struct hash_table *ht, char *str)
{
	struct list *new_list;
	unsigned int hashval = hash(ht, str);

	if ((new_list = malloc(sizeof(struct list))) == NULL) return 1;

	new_list->str = strdup(str);
	new_list->count = 1;
	new_list->next = ht->table[hashval];
	ht->table[hashval] = new_list;

	return 0;
}

int
main(int argc, char *argv[])
{
	int n, i;
	char line[32], newname[32];
	struct list *tmp;
	struct hash_table *ht;

	/* Ввод кол-ва строк */
	scanf("%d", &n);
	ht = ht_create(n);

	/* пока не конец ввода */
	for (i = 0; i < n; i++) {
		scanf("%s", line);

		/* если имя уже есть в хеш-таблице */
		if (tmp = ht_lookup(ht, line)) {
			/* сгенерировать новое имя */
			snprintf(newname, 32, "%s%d", tmp->str, tmp->count++);
			/* сохранить новое имя  */
			ht_add(ht, newname);
			/* вывести новое имя */
			printf("%s\n", newname);

		/* иначе */
		} else {
			/* сохранить имя */
			ht_add(ht, line);
			/* вывести OK */
			printf("OK\n");
		}
	}

	return 0;
}


#include<stdio.h>
struct storage{
char input[50];
int gate;
}arr[250000];
int compute[250000];
int compare(struct storage *a,struct storage *b){
if(strcmp(a->input,b->input)==0){
 return a->gate-b->gate;
}
return strcmp(a->input,b->input);
}
int main(){
 int a,b,c,d;
 scanf("%d",&a);
 getchar();
 int i;
 for(i=0;i<a;i++){
  gets(arr[i].input);
  arr[i].gate=i;
}
qsort(arr,a,sizeof(struct storage),compare);
 for(i=0;i<a;){
 compute[arr[i].gate]=i;
 char temp[50];
 strcpy(temp,arr[i].input);
 strcpy(arr[i].input,"OK");
 int k=1; int len=strlen(temp);
 if(strcmp(arr[i+1].input,temp)==0){
  i++;
  compute[arr[i].gate]=i;
  while(strcmp(arr[i].input,temp)==0){
  arr[i].input[len]=k+'0';
  compute[arr[i].gate]=i;
  k++;
  i++;
  }
 }
 else i++;
 }
 for(i=0;i<a;i++)
 puts(arr[compute[i]].input);
}


#include<stdio.h>
struct storage{
char input[50];
int gate;
}arr[250000];
int compute[250000];
int compare(struct storage *a,struct storage *b){
if(strcmp(a->input,b->input)==0){
 return a->gate-b->gate;
}
return strcmp(a->input,b->input);
}
int main(){
 int a,b,c,d;
 scanf("%d",&a);
 getchar();
 int i;
 for(i=0;i<a;i++){
  gets(arr[i].input);
  arr[i].gate=i;
}
qsort(arr,a,sizeof(struct storage),compare);
 for(i=0;i<a;){
 compute[arr[i].gate]=i;
 char temp[50]; char number[250];
 strcpy(temp,arr[i].input);
 strcpy(arr[i].input,"OK");
 int k=1; int len=strlen(temp);
 if(strcmp(arr[i+1].input,temp)==0){
  i++;
  compute[arr[i].gate]=i;
  while(strcmp(arr[i].input,temp)==0){
  itoa(k,number,10);
  strcat(arr[i].input,number);
  compute[arr[i].gate]=i;
  k++;
  i++;
  }
 }
 else i++;
 }
 for(i=0;i<a;i++)
 puts(arr[compute[i]].input);
}


#include <stdio.h>
#include <string.h>
#define A 23209
#define B 63689
#define C 2097593
#define FIRSTH 103

unsigned chash(const char* s)
{
    unsigned h=FIRSTH;
    while (*s)
    {
     h=((h*A)^(s[0]*B));
     ++s;
    }
    return h%C;
}

int a[3000000];

int main()
{
    int n, h;
    char s[33];
    scanf("%d", &n);
    for(int i=0; i<n; ++i)
    {
        scanf("%s", s);
        h=chash(s);
        if(a[h]==0)
            printf("OK\n");
        else
            printf("%s%d\n", s, a[h]);
        ++a[h];
    }
}


#include <stdio.h>
#include <string.h>

unsigned chash(const char* s)
{
    unsigned int b=378551;
    unsigned int a=63689;
    unsigned int h=0;
    while(*s)
    {
        h=h*a+s[0];
        a=a*b;
        ++s;
    }
}

int a[3000000];

int main()
{
    int n, h;
    char s[33];
    scanf("%d", &n);
    for(int i=0; i<n; ++i)
    {
        scanf("%s", s);
        h=chash(s);
        if(a[h]==0)
            printf("OK\n");
        else
            printf("%s%d\n", s, a[h]);
        ++a[h];
    }
}


#include <stdio.h>
#define A 63689
#define B 378551
#define C 993319
#define FIRSTH 17

unsigned chash(const char* s)
{
   unsigned h = FIRSTH;
   while (*s) {
     h=((h*A)^(s[0]*B))+s[1];
     ++s;
   }
   return h%C;
}

int a[1000000];

int main()
{
    int n, h;
    char s[33];
    scanf("%d", &n);
    for(int i=0; i<n; ++i)
    {
        scanf("%s", s);
        h=chash(s);
        if(a[h]==0)
            printf("OK\n");
        else
            printf("%s%d\n", s, a[h]);
        ++a[h];
    }
}


#include <stdio.h>
#define A 63689
#define B 378551
#define C 993319
#define FIRSTH 37

unsigned chash(const char* s)
{
   unsigned h = FIRSTH;
   while (*s) {
     h=(h*A)^(s[0]*B);
     ++s;
   }
   return h%C;
}

int a[1000000];

int main()
{
    int n, h;
    char s[33];
    scanf("%d", &n);
    for(int i=0; i<n; ++i)
    {
        scanf("%s", s);
        h=chash(s);
        if(a[h]==0)
            printf("OK\n");
        else
            printf("%s%d\n", s, a[h]);
        ++a[h];
    }
}


#include <stdio.h>
#define A 63689
#define B 378551
#define C 1315423911
#define FIRSTH 37

unsigned chash(const char* s)
{
   unsigned h = FIRSTH;
   while (*s) {
     h=(h*A)^(s[0]*B);
     ++s;
   }
   return h%C;
}

int a[1000000];

int main()
{
    int n, h;
    char s[33];
    scanf("%d", &n);
    for(int i=0; i<n; ++i)
    {
        scanf("%s", s);
        h=chash(s);
        if(a[h]==0)
            printf("OK\n");
        else
            printf("%s%d\n", s, a[h]);
        ++a[h];
    }
}


#include <stdio.h>
#include <string.h>

unsigned chash(const char* s)
{
    unsigned int b=378551;
    unsigned int a=63689;
    unsigned int h=0;
    while(*s)
    {
        h=h*a+str[0];
        a=a*b;
        ++s;
    }
}

int a[3000000];

int main()
{
    int n, h;
    char s[33];
    scanf("%d", &n);
    for(int i=0; i<n; ++i)
    {
        scanf("%s", s);
        h=chash(s);
        if(a[h]==0)
            printf("OK\n");
        else
            printf("%s%d\n", s, a[h]);
        ++a[h];
    }
}


#include <stdio.h>
#include <string.h>
#define A 63689
#define B 378551
#define C 993319
#define FIRSTH 103

unsigned chash(const char* s)
{
    unsigned h = FIRSTH;
    while (*s)
    {
     h=((h*A)^(s[0]*B));
     ++s;
    }
    return h%C;
}

int a[1000000];

int main()
{
    int n, h;
    char s[33];
    scanf("%d", &n);
    for(int i=0; i<n; ++i)
    {
        scanf("%s", s);
        h=chash(s);
        if(a[h]==0)
            printf("OK\n");
        else
            printf("%s%d\n", s, a[h]);
        ++a[h];
    }
}


#include <stdio.h>
#define A 54059
#define B 76963
#define C  999331
#define FIRSTH 37

unsigned chash(const char* s)
{
   unsigned h = FIRSTH;
   while (*s) {
     h=(h*A)^(s[0]*B);
     ++s;
   }
   return h%C;
}

int a[1000000];

int main()
{
    int n, h;
    char s[33];
    scanf("%d", &n);
    for(int i=0; i<n; ++i)
    {
        scanf("%s", s);
        h=chash(s);
        if(a[h]==0)
            printf("OK\n");
        else
            printf("%s%d\n", s, a[h]);
        ++a[h];
    }
}


#include <stdio.h>
#include <string.h>
#define A 63689
#define B 378551
#define C 2097593
#define FIRSTH 103

unsigned chash(const char* s)
{
    unsigned h=FIRSTH;
    while (*s)
    {
     h=((h*A)^(s[0]*B));
     ++s;
    }
    return h%C;
}

int a[3000000];

int main()
{
    int n, h;
    char s[33];
    scanf("%d", &n);
    for(int i=0; i<n; ++i)
    {
        scanf("%s", s);
        h=chash(s);
        if(a[h]==0)
            printf("OK\n");
        else
            printf("%s%d\n", s, a[h]);
        ++a[h];
    }
}


#include <stdio.h>
#include <string.h>
#define A 63689
#define B 378551
#define C 514229
#define FIRSTH 103

unsigned chash(const char* s)
{
    unsigned h=FIRSTH;
    while (*s)
    {
     h=((h*A)^(s[0]*B));
     ++s;
    }
    return h%C;
}

int a[1000000];

int main()
{
    int n, h;
    char s[33];
    scanf("%d", &n);
    for(int i=0; i<n; ++i)
    {
        scanf("%s", s);
        h=chash(s);
        if(a[h]==0)
            printf("OK\n");
        else
            printf("%s%d\n", s, a[h]);
        ++a[h];
    }
}


#include <stdio.h>
#define A 63689
#define B 378551
#define C 993319
#define FIRSTH 17

unsigned chash(const char* s)
{
   unsigned h = FIRSTH;
   while (*s) {
     h=(h*A)^(s[0]*B);
     ++s;
   }
   return h%C;
}

int a[1000000];

int main()
{
    int n, h;
    char s[33];
    scanf("%d", &n);
    for(int i=0; i<n; ++i)
    {
        scanf("%s", s);
        h=chash(s);
        if(a[h]==0)
            printf("OK\n");
        else
            printf("%s%d\n", s, a[h]);
        ++a[h];
    }
}


#include <stdio.h>
#include <string.h>
#define A 63689
#define B 378551
#define C 993319
#define FIRSTH 17

unsigned chash(const char* s)
{
   unsigned h = FIRSTH;
   while (*s) {
     h=((h*A)^(s[0]*B))+strlen(s);
     ++s;
   }
   return h%C;
}

int a[1000000];

int main()
{
    int n, h;
    char s[33];
    scanf("%d", &n);
    for(int i=0; i<n; ++i)
    {
        scanf("%s", s);
        h=chash(s);
        if(a[h]==0)
            printf("OK\n");
        else
            printf("%s%d\n", s, a[h]);
        ++a[h];
    }
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct t_node node;
struct t_node
{
    char *name;
    node *next;
};

typedef struct t_hash hash;

struct t_hash
{
    char *key;
    int value;
    hash *next;
};

node *makeNode(char *newName)
{
    node *newElement = (node*)malloc(1);
    newElement->name = (char*)malloc(strlen(newName) + 1);
    strcpy(newElement->name, newName);
    newElement->next = NULL;
    return newElement;
}

hash *makeHash(char *newKey, int newValue)
{
    hash *newElement = (hash*)malloc(1);
    newElement->key = (char*)malloc(strlen(newKey) + 1);
    strcpy(newElement->key, newKey);
    newElement->value = newValue;
    newElement->next = NULL;
    return newElement;
}

hash *findInHash(hash *head, char *keyToFind)
{
    while(head)
    {
        if ( !strcmp(head->key, keyToFind) )
            return head;
        head = head->next;
    }
    return NULL;
}

int main()
{
    int n;
    scanf("%d", &n);
    char *name = (char*)malloc(36);
    scanf("%s", name);
    --n;
    node *names = makeNode(name);
    node *lastName = names;
    while (n--)
    {
        scanf("%s", name);
        lastName->next = makeNode(name);
        lastName = lastName->next;
    }

    hash *logins = makeHash(names->name, 1);
    hash *lastHash = logins;
    hash *findResult;
    printf("OK\n");
    names = names->next;

    while (names)
    {
        findResult = findInHash(logins, names->name);
        if ( !findResult )
        {
            printf("OK\n");
            lastHash->next = makeHash(names->name, 1);
        }
        else
        {
            printf("%s%d\n", names->name, findResult->value);
            ++findResult->value;
        }
        findResult = NULL;
        names = names->next;
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct t_node node;
struct t_node
{
    char *name;
    node *next;
};

typedef struct t_hash hash;

struct t_hash
{
    char *key;
    int value;
    hash *next;
};

node *makeNode(char *newName)
{
    node *newElement = (node*)malloc(1);
    newElement->name = (char*)malloc(strlen(newName) + 1);
    strcpy(newElement->name, newName);
    newElement->next = NULL;
    return newElement;
}

hash *makeHash(char *newKey, int newValue)
{
    hash *newElement = (hash*)malloc(1);
    newElement->key = (char*)malloc(strlen(newKey) + 1);
    strcpy(newElement->key, newKey);
    newElement->value = newValue;
    newElement->next = NULL;
    return newElement;
}

hash *findInHash(hash *head, char *keyToFind)
{
    while(head)
    {
        if ( !strcmp(head->key, keyToFind) )
            return head;
        head = head->next;
    }
    return NULL;
}

int main()
{
    int n;
    scanf("%d", &n);
    char *name = (char*)malloc(36);
    scanf("%s", name);
    --n;
    node *names = makeNode(name);
    node *lastName = names;
    while (n--)
    {
        scanf("%s", name);
        lastName->next = makeNode(name);
        lastName = lastName->next;
    }

    hash *logins = makeHash(names->name, 1);
    hash *lastHash = logins;
    hash *findResult;
    printf("OK\n");
    names = names->next;

    while (names)
    {
        findResult = findInHash(logins, names->name);
        if ( !findResult )
        {
            printf("OK\n");
            lastHash->next = makeHash(names->name, 1);
            lastHash = lastHash->next;
        }
        else
        {
            printf("%s%d\n", names->name, findResult->value);
            ++findResult->value;
        }
        findResult = NULL;
        names = names->next;
    }

    return 0;
}


#include<stdio.h>
#include<string.h>
typedef struct tip
{
	int x;
	char str[40];
}tip;
int main ()
{
	char arr[40];
	tip str[100005];
	int u,j,n,f=0,i;
	scanf("%d",&n);
	for (i=0; i<n; i++)
	{
		scanf("%s",arr);
		for(j=0; j<u; j++)
			if (!strcmp(arr,str[j].str))
			{
				str[j].x++;
				f=j+1;
				break;
			}
		if (f)
		printf("%s%d\n",str[f-1].str,str[f-1].x);
		else
		{
			strcpy(str[u++].str,arr);
			puts("OK");
		}
		f=0;
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
typedef struct tip
{
	int x;
	char str[40];
}tip;
int main ()
{
	char arr[40];
	tip str[100005];
	int u,j,n,f=0,i;
	scanf("%d",&n);
	for (i=0; i<n; i++)
	{
		scanf("%s",arr);
		for(j=0; j<u; j++)
			if (!strcmp(arr,str[j].str))
			{
				str[j].x++;
				f=j+1;
				break;
			}
		if (f)
		printf("%s%d",str[f-1].str,str[f-1].x);
		else
		{
			strcpy(str[u++].str,arr);
			puts("OK");
		}
		f=0;
	}
	return 0;
}


#include <stdio.h>
#include <string.h>
int n, i, j, yer, k, e[100005];
char s[35], a[100005][35];

int esit(char[],char[]);

int main () {
	scanf("%d",&n);
	for(i = 0 ; i < n ; i++){
		scanf("%s",s);
		k = 1;
		for(j = 0 ; j < yer ; j++){
			if(esit(s,a[j])){
				e[j]++;
				printf("%s%d\n",s,e[j]);
				k = 0;
				break;
			}
		}
		if(k){
			printf("OK\n");
			for(j = 0 ; j <= strlen(s) ; j++)
				a[yer][j] = s[j];
			yer++;
		}
	}
	return 0;
}

int esit(char a[], char b[]){
	int i;
	if(strlen(a) != strlen(b))
		return 0;
	for(i = 0 ; i<strlen(a); i++){
		if(a[i] != b[i])
			return 0;
	}
	return 1;
}


#include <stdio.h>
#include <string.h>

int n, i, j, yer, k, e[100005];
char s[35], a[100005][35];

int esit(char[],char[]);

int main () {
	scanf("%d",&n);
	for(i = 0 ; i < n ; i++){
		scanf("%s",s);
		k = 1;
		for(j = 0 ; j < yer ; j++){
			if(esit(s,a[j])){
				e[j]++;
				printf("%s%d\n",s,e[j]);
				k = 0;
				break;
			}
		}
		if(k){
			printf("OK\n");
			for(j = 0 ; j <= strlen(s) ; j++)
				a[yer][j] = s[j];
			yer++;
		}
	}
	return 0;
}

int esit(char a[], char b[]){
	int k = 1, i;
	if(strlen(a) != strlen(b))
		return 0;
	for(i = 0 ; a[i] ; i++){
		if(a[i] != b[i]){
			k = 0;
			break;
		}
	}
	return k;
}


#include <stdio.h>
#include <string.h>

int n, i, j, yer, k, e[100005];
char s[35], a[100005][35];

int esit(char[],char[]);

int main () {
	scanf("%d",&n);
	for(i = 0 ; i < n ; i++){
		scanf("%s",s);
		k = 1;
		for(j = 0 ; j < yer ; j++){
			if(esit(s,a[j])){
				e[j]++;
				printf("%s%d\n",s,e[j]);
				k = 0;
				break;
			}
		}
		if(k){
			printf("OK\n");
			for(j = 0 ; j <= strlen(s) ; j++)
				a[yer][j] = s[j];
			yer++;
		}
	}
	return 0;
}

int esit(char a[], char b[]){
	int k = 1, i;
	if(strlen(a) != strlen(b))
		return 0;
	for(i = 0 ; i<strlen(a) ; i++){
		if(a[i] != b[i]){
			k = 0;
			break;
		}
	}
	return k;
}


#include <stdio.h>
#include <string.h>

int n, i, j, yer, k;
char s[35], a[100005][35], e[100005];

int esit(char[],char[]);

int main () {
	scanf("%d",&n);
	for(i = 0 ; i < n ; i++){
		scanf("%s",s);
		k = 1;
		for(j = 0 ; j < yer ; j++){
			if(esit(s,a[j])){
				e[j]++;
				printf("%s%d\n",s,e[j]);
				k = 0;
				break;
			}
		}
		if(k){
			printf("ok\n");
			for(j = 0 ; j <= strlen(s) ; j++)
				a[yer][j] = s[j];
			yer++;
		}
	}
	return 0;
}

int esit(char a[], char b[]){
	int k = 1, i;
	if(strlen(a) != strlen(b))
		return 0;
	for(i = 0 ; a[i] ; i++){
		if(a[i] != b[i]){
			k = 0;
			break;
		}
	}
	return k;
}


#include <stdio.h>
#include <string.h>

int n, i, j, yer, k;
char s[35], a[100005][35], e[100005];

int esit(char[],char[]);

int main () {
	scanf("%d",&n);
	for(i = 0 ; i < n ; i++){
		scanf("%s",s);
		k = 1;
		for(j = 0 ; j < yer ; j++){
			if(esit(s,a[j])){
				e[j]++;
				printf("%s%d\n",s,e[j]);
				k = 0;
				break;
			}
		}
		if(k){
			printf("OK\n");
			for(j = 0 ; j <= strlen(s) ; j++)
				a[yer][j] = s[j];
			yer++;
		}
	}
	return 0;
}

int esit(char a[], char b[]){
	int k = 1, i;
	if(strlen(a) != strlen(b))
		return 0;
	for(i = 0 ; a[i] ; i++){
		if(a[i] != b[i]){
			k = 0;
			break;
		}
	}
	return k;
}


#include <stdio.h>
#include <string.h>

int n, i, j, yer, k, e[100005];
char s[35], a[100005][35];

int esit(char[],char[]);

int main () {
	scanf("%d",&n);
	for(i = 0 ; i < n ; i++){
		scanf("%s",s);
		k = 1;
		for(j = 0 ; j < yer ; j++){
			if(!strcmp(s,a[j])){
				e[j]++;
				printf("%s%d\n",s,e[j]);
				k = 0;
				break;
			}
		}
		if(k){
			printf("OK\n");
			for(j = 0 ; j <= strlen(s) ; j++)
				a[yer][j] = s[j];
			yer++;
		}
	}
	return 0;
}

int esit(char a[], char b[]){
	int k = 1, i;
	if(strlen(a) != strlen(b))
		return 0;
	for(i = 0 ; a[i] ; i++){
		if(a[i] != b[i]){
			k = 0;
			break;
		}
	}
	return k;
}


s=input()
s=list(s)
l=len(s)-1
z=0
for i in range(l,-1,-1):
    if(s[i]=='0'):
        z=z+1
    elif z:
        z-=1
    else:
        s[i]='0'
s="".join(s)
print(s)


st = input().rstrip()
a = []
for item in st:
    a.append(int(item))

a.reverse()
zero_count = 0
for i, item in enumerate(a):
    if item == 0:
        zero_count += 1
    elif zero_count > 0:
        zero_count -= 1
    else:
        a[i] = 0
a.reverse()
print("".join([str(item) for item in a]))


s=[int(x) for x in list(input())]
n=len(s)
b=[0]*n
counter=0
for i in range(n-1,-1,-1):
    if s[i]==0:
        counter+=1
    elif counter>0:
        counter-=1
    else:
        s[i]=0
arr=''
for item in s:
    arr+=str(item)
print(arr)


line = input()
res = []
zer = 0
one = 0

for i in range(len(line)):
    ind = len(line) - 1 - i

    if line[ind] == '0':
        res.append('0')
        zer += 1
    else:
        if zer <= one:
            res.append('0')
            zer += 1
        else:
            res.append('1')
        one += 1

for i in range(len(res)):
    print(res[len(res) - 1 - i], end = '')
print('\n', end = '')


line = input()
res = []
zer = 0
one = 0

for i in range(len(line)):
    ind = len(line) - 1 - i

    if line[ind] == '0':
        res.append('0')
        zer += 1
    else:
        one += 1
        if zer <= one:
            res.append('0')
            zer += 1
        else:
            res.append('1')

for i in range(len(res)):
    print(res[len(res) - 1 - i], end = '')


import fileinput
def D(a):print(a)
def S(s,I):return int(s.split(" ")[I])
def main():
    for l in fileinput.input():
        l=l[:-1]
        N=len(l)
        Q=[0]*N
        I=0
        o=[0]*N
        for i in range(N):
            if(I==0):
                Q[I]=i
                I+=1
            elif(l[Q[I-1]]=='1' and l[i]=='0'):
                I-=1
                o[Q[I]]="1"
            else:
                Q[I]=i
                I+=1
        D(''.join([str(v) for v in o]))
main()


from sys import stdin
def main():
    s = stdin.readline().strip()
    n = len(s)
    t = [None for _ in xrange(n)]
    for i in xrange(n):
        if s[i] == '0':
            t[i] = '0'
            if i and s[i-1] == '1':
                t[i-1] = '1'
    x = y = z = 0
    for i in xrange(n - 1, -1, -1):
        if s[i] == '1':
            y += 1
        else:
            x = x if x > y else y
            x += 1
        r = x if x > y else y
        if t[i] is None:
            if r > z:
                t[i] = '0'
            else:
                t[i] = '1'
        z += t[i] == '0'
    print ''.join(t)
main()


/* upsolve with Dukkha */
#include <stdio.h>
#include <string.h>

#define N	100000

int main() {
	static char cc[N + 1];
	static int stack[N];
	int n, i, cnt;

	scanf("%s", cc), n = strlen(cc);
	cnt = 0;
	for (i = 0; i < n; i++)
		if (cc[i] == '1')
			cc[stack[cnt++] = i] = '0';
		else if (cnt > 0)
			cc[stack[--cnt]] = '1';
	printf("%s\n", cc);
	return 0;
}


using System;
using System.Linq;
using CompLib.Util;
using System.Threading;
using CompLib.Collections.Generic;

public class Program
{
    private string S;
    private int N;

    public void Solve()
    {
        var sc = new Scanner();
        S = sc.Next();
        N = S.Length;

        // max (0の個数,1の個数,0000...1111...)

        (int o, int i, int oi)[] ar = new (int o, int i, int oi)[N];
        for (int i = 0; i < N; i++)
        {
            if (S[i] == '1') ar[i] = (0, 1, 1);
            else ar[i] = (1, 0, 1);
        }

        var st = new SegmentTree<(int o, int i, int oi)>(ar,
            (l, r) => (l.o + r.o, l.i + r.i, Math.Max(l.o + r.oi, l.oi + r.i)),
            (0, 0, 0));

        char[] t = new char[N];
        int cnt = 0;
        for (int i = N - 1; i >= 0; i--)
        {
            if (cnt + 1 <= st.Query(i, N).oi)
            {
                t[i] = '0';
                cnt++;
            }
            else
            {
                t[i] = '1';
            }
        }

        Console.WriteLine(new string(t));
    }

    public static void Main(string[] args) => new Program().Solve();
    // public static void Main(string[] args) => new Thread(new Program().Solve, 1 << 27).Start();
}

namespace CompLib.Collections.Generic
{
    using System;
    using System.Diagnostics;

    public class SegmentTree<T>
    {
        // 見かけ上の大きさ、実際の大きさ
        private readonly int _n, _size;
        private T[] _array;

        private T _identity;
        private Func<T, T, T> _operation;

        public SegmentTree(int n, Func<T, T, T> operation, T identity)
        {
            _n = n;
            _size = 1;
            while (_size < _n)
            {
                _size *= 2;
            }

            _identity = identity;
            _operation = operation;
            _array = new T[_size * 2];
            for (int i = 1; i < _size * 2; i++)
            {
                _array[i] = _identity;
            }
        }

        public SegmentTree(T[] a, Func<T, T, T> operation, T identity)
        {
            _n = a.Length;
            _size = 1;
            while (_size < _n)
            {
                _size *= 2;
            }

            _identity = identity;
            _operation = operation;
            _array = new T[_size * 2];
            for (int i = 0; i < a.Length; i++)
            {
                _array[i + _size] = a[i];
            }

            for (int i = a.Length; i < _size; i++)
            {
                _array[i + _size] = identity;
            }

            for (int i = _size - 1; i >= 1; i--)
            {
                _array[i] = operation(_array[i * 2], _array[i * 2 + 1]);
            }
        }

        /// <summary>
        /// A[i]をnに更新 O(log N)
        /// </summary>
        /// <param name="i"></param>
        /// <param name="n"></param>
        public void Update(int i, T n)
        {
            Debug.Assert(0 <= i && i < _n);
            i += _size;
            _array[i] = n;
            while (i > 1)
            {
                i /= 2;
                _array[i] = _operation(_array[i * 2], _array[i * 2 + 1]);
            }
        }

        /// <summary>
        /// A[left] op A[left+1] ... op A[right-1]を求める
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <returns></returns>
        public T Query(int left, int right)
        {
            Debug.Assert(0 <= left && left <= right && right <= _n);
            T sml = _identity;
            T smr = _identity;

            left += _size;
            right += _size;
            while (left < right)
            {
                if ((left & 1) != 0) sml = _operation(sml, _array[left++]);
                if ((right & 1) != 0) smr = _operation(_array[--right], smr);
                left >>= 1;
                right >>= 1;
            }

            return _operation(sml, smr);
        }

        /// <summary>
        /// op(a[0],a[1],...,a[n-1])を返します
        /// </summary>
        /// <returns></returns>
        public T All()
        {
            return _array[1];
        }

        /// <summary>
        /// f(op(a[l],a[l+1],...a[r-1])) = trueとなる最大のrを返します
        /// </summary>
        /// <param name="l"></param>
        /// <param name="f"></param>
        /// <returns></returns>
        public int MaxRight(int l, Func<T, bool> f)
        {
            Debug.Assert(0 <= l && l <= _n);
#if DEBUG
            Debug.Assert(f(_identity));
#endif
            if (l == _n) return _n;
            l += _size;
            T sm = _identity;
            do
            {
                while (l % 2 == 0) l >>= 1;
                if (!f(_operation(sm, _array[l])))
                {
                    while (l < _size)
                    {
                        l <<= 1;
                        if (f(_operation(sm, _array[l])))
                        {
                            sm = _operation(sm, _array[l]);
                            l++;
                        }
                    }

                    return l - _size;
                }

                sm = _operation(sm, _array[l]);
                l++;
            } while ((l & -l) != l);

            return _n;
        }

        /// <summary>
        /// f(op(a[l],a[l+1],...a[r-1])) = trueとなる最小のlを返します
        /// </summary>
        /// <param name="r"></param>
        /// <param name="f"></param>
        /// <returns></returns>
        public int MinLeft(int r, Func<T, bool> f)
        {
            Debug.Assert(0 <= r && r <= _n);
#if DEBUG
            Debug.Assert(f(_identity));
#endif
            if (r == 0) return 0;
            r += _size;
            T sm = _identity;

            do
            {
                r--;
                while (r > 1 && (r % 2 != 0)) r >>= 1;
                if (!f(_operation(_array[r], sm)))
                {
                    while (r < _size)
                    {
                        r = (2 * r + 1);
                        if (f(_operation(_array[r], sm)))
                        {
                            sm = _operation(_array[r], sm);
                            r--;
                        }
                    }

                    return r + 1 - _size;
                }

                sm = _operation(_array[r], sm);
            } while ((r & -r) != r);

            return 0;
        }

        public T this[int i]
        {
            set { Update(i, value); }
            get
            {
                Debug.Assert(0 <= i && i < _n);
                return _array[i + _size];
            }
        }
    }
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            if (_index >= _line.Length)
            {
                string s;
                do
                {
                    s = Console.ReadLine();
                } while (s.Length == 0);

                _line = s.Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public string ReadLine()
        {
            _index = _line.Length;
            return Console.ReadLine();
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            string s = Console.ReadLine();
            _line = s.Length == 0 ? new string[0] : s.Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}