#include <bits/stdc++.h>
using namespace std;
void solve(vector<string>v)
{
unordered_map<string,int>mp;

for(int i=0;i<v.size();i++)
{

    if(mp[v[i]]==0)
     {
     cout<<"OK"<<endl;
     mp[v[i]]++;
     }
    else
     {
          cout<<v[i]<<mp[v[i]]<<endl;
          mp[v[i]]++;
     }
}
}
int main()
{
     int n;
     cin>>n;
     vector<string>v;
     while(n--)
     {
     string s;
     cin>>s;
     v.push_back(s);
     }
     solve(v);
     return 0;
}


#include <bits/stdc++.h>
using namespace std;
void solve(vector<string>v)
{
unordered_set<string>st;
int j=0;
for(int i=0;i<v.size();i++)
{
     if(st.find(v[i])!=st.end())
     {
     j++;
     cout<<v[i]<<j<<endl;
     }
     else
     {
     cout<<"OK"<<endl;
     st.insert(v[i]);
     }
}
}
int main()
{
     int n;
     cin>>n;
     vector<string>v;
     while(n--)
     {
     string s;
     cin>>s;
     v.push_back(s);
     }
     solve(v);
     return 0;
}


#include <bits/stdc++.h>
using namespace std;
void solve(vector<string>v)
{
unordered_set<string>st;
int j=1;
for(int i=0;i<v.size();i++)
{
     if(st.find(v[i])!=st.end())
     {
     cout<<v[i]<<j<<endl;
     j++;
     }
     else
     {
     cout<<"OK"<<endl;
     st.insert(v[i]);
     }
}
}
int main()
{
     int n;
     cin>>n;
     vector<string>v;
     while(n--)
     {
     string s;
     cin>>s;
     v.push_back(s);
     }
     solve(v);
     return 0;
}


#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <vector>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <string.h>
#include <limits.h>
#include <algorithm>
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#define ff first
#define ss second
#define ll long long
#define pb push_back
#define mp make_pair
#define inf 1000000007
#define mod 1000000007
#define pii pair <int, int>
#define all(x) x.begin(), x.end()
//#define tr(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)

//using namespace __gnu_pbds;

using namespace std;

//template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int n, now;

string s;

int M[3000005];

char type[3000005];

vector <int> T[3000005];

void upd()
{
	int id = 0;
	for (int i = 0; i < s.length(); i++)
	{
		int pos = -1;
		for (auto h: T[id])
			if (type[h] == s[i])
				pos = h;

		if (pos != -1)
			id = pos;
		else
			T[id].pb(++now), id = now, type[id] = s[i];
	}

	if (M[id])
		cout << s << M[id] << "\n";
	else
		cout << "OK\n";

	M[id]++;
}

int main()
{
	cin >> n;

	while (n--)
	{
		cin >> s;

		upd();
	}
}


#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <vector>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <string.h>
#include <limits.h>
#include <algorithm>
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#define ff first
#define ss second
#define ll long long
#define pb push_back
#define mp make_pair
#define inf 1000000007
#define mod 1000000007
#define pii pair <int, int>
#define all(x) x.begin(), x.end()
//#define tr(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)

//using namespace __gnu_pbds;

using namespace std;

//template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int n;

string s;

unordered_map <ll, map <ll, int> > M;

int main()
{
	cin >> n;

	while (n--)
	{
		cin >> s;

		ll H = 0, T = 0;
		for (int i = 0; i < s.length(); i++)
			H = H*29 + (s[i] - 'a'), H %= mod,
			T = T*53 + (s[i] - 'a'), T %= mod;

		if (M[H][T])
			cout << s << M[H][T] << "\n";
		else
			cout << "OK\n";

		M[H][T]++;
	}
}


#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <vector>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <string.h>
#include <limits.h>
#include <algorithm>
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#define ff first
#define ss second
#define ll long long
#define pb push_back
#define mp make_pair
#define inf 1000000007
#define mod 1000000007
#define pii pair <int, int>
#define all(x) x.begin(), x.end()
//#define tr(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)

//using namespace __gnu_pbds;

using namespace std;

//template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int n;

string s;

unordered_map <ll, int> M, J;

int main()
{
	cin >> n;

	while (n--)
	{
		cin >> s;

		ll H = 0, T = 0;
		for (int i = 0; i < s.length(); i++)
			H = H*29 + (s[i] - 'a'), H %= mod,
			T = T*53 + (s[i] - 'a'), T %= mod;

		if (M[H] && J[T])
			cout << s << M[H] << "\n";
		else
			cout << "OK\n";

		M[H]++, J[T]++;
	}
}


#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <vector>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <string.h>
#include <limits.h>
#include <algorithm>
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#define ff first
#define ss second
#define ll long long
#define pb push_back
#define mp make_pair
#define inf 1000000007
#define mod 1000000007
#define pii pair <int, int>
#define all(x) x.begin(), x.end()
//#define tr(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)

//using namespace __gnu_pbds;

using namespace std;

//template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int n;

string s;

unordered_map <ll, int> M;

int main()
{
	cin >> n;

	while (n--)
	{
		cin >> s;

		ll H = 0;
		for (int i = 0; i < s.length(); i++)
			H = H*27 + (s[i] - 'a'), H %= mod;

		if (M[H])
			cout << s << M[H] << "\n";
		else
			cout << "OK\n";

		M[H]++;
	}
}


#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <vector>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <string.h>
#include <limits.h>
#include <algorithm>
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#define ff first
#define ss second
#define ll long long
#define pb push_back
#define mp make_pair
#define inf 1000000007
#define mod 1000000007
#define pii pair <int, int>
#define all(x) x.begin(), x.end()
//#define tr(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)

//using namespace __gnu_pbds;

using namespace std;

//template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int n;

string s;

map <ll, map <ll, int> > M;

int main()
{
	cin >> n;

	while (n--)
	{
		cin >> s;

		ll H = 0, T = 0;
		for (int i = 0; i < s.length(); i++)
			H = H*27 + (s[i]), H %= mod,
			T = T*27 + (s[i]);

		if (M[H][T])
			cout << s << M[H][T] << "\n";
		else
			cout << "OK\n";

		M[H][T]++;
	}
}


#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <vector>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <string.h>
#include <limits.h>
#include <algorithm>
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#define ff first
#define ss second
#define ll long long
#define pb push_back
#define mp make_pair
#define inf 1000000007
#define mod 1000000007
#define pii pair <int, int>
#define all(x) x.begin(), x.end()
//#define tr(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)

//using namespace __gnu_pbds;

using namespace std;

//template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int n;

string s;

unordered_map <ll, map <ll, int> > M;

int main()
{
	cin >> n;

	while (n--)
	{
		cin >> s;

		ll H = 0, T = 0;
		for (int i = 0; i < s.length(); i++)
			H = H*27 + (s[i]), H %= mod,
			T = T*27 + (s[i]);

		if (M[H][T])
			cout << s << M[H][T] << "\n";
		else
			cout << "OK\n";

		M[H][T]++;
	}
}


#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <vector>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <string.h>
#include <limits.h>
#include <algorithm>
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#define ff first
#define ss second
#define ll long long
#define pb push_back
#define mp make_pair
#define inf 1000000007
#define mod 1000000007
#define pii pair <int, int>
#define all(x) x.begin(), x.end()
//#define tr(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)

//using namespace __gnu_pbds;

using namespace std;

//template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int n, now;

char s[40];

int M[3000005];

char type[3000005];

vector <int> T[3000005];

void upd()
{
	int sz = strlen(s);

	int id = 0;
	for (int i = 0; i < sz; i++)
	{
		int pos = -1;
		for (auto h: T[id])
			if (type[h] == s[i])
				pos = h;

		if (pos != -1)
			id = pos;
		else
			T[id].pb(++now), id = now, type[id] = s[i];
	}

	if (M[id])
		printf("%s%d\n", s, M[id]);
	else
		printf("OK\n");

	M[id]++;
}

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

	while (n--) {
		scanf("%s", s);
		upd();
	}
}


#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <vector>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <string.h>
#include <limits.h>
#include <algorithm>
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#define ff first
#define ss second
#define ll long long
#define pb push_back
#define mp make_pair
#define inf 1000000007
#define mod 1000000007
#define pii pair <int, int>
#define all(x) x.begin(), x.end()
//#define tr(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)

//using namespace __gnu_pbds;

using namespace std;

//template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int n;

string s;

map <ll, map <ll, int> > M;

int main()
{
	cin >> n;

	while (n--)
	{
		cin >> s;

		ll H = 0, T = 0;
		for (int i = 0; i < s.length(); i++)
			H = H*29 + (s[i]), H %= mod,
			T = T*53 + (s[i]), T %= mod;

		if (M[H][T])
			cout << s << M[H][T] << "\n";
		else
			cout << "OK\n";

		M[H][T]++;
	}
}


#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <vector>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <string.h>
#include <limits.h>
#include <algorithm>
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#define ff first
#define ss second
#define ll long long
#define pb push_back
#define mp make_pair
#define inf 1000000007
#define mod 1000000007
#define pii pair <int, int>
#define all(x) x.begin(), x.end()
//#define tr(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)

//using namespace __gnu_pbds;

using namespace std;

//template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int n, now;

string s;

int M[3000005];

char type[100005];

vector <int> T[3000005];

void upd()
{
	int id = 0;
	for (int i = 0; i < s.length(); i++)
	{
		int pos = -1;
		for (auto h: T[id])
			if (type[h] == s[i])
				pos = h;

		if (pos != -1)
			id = pos;
		else
			T[id].pb(++now), id = now, type[id] = s[i];
	}

	if (M[id])
		cout << s << M[id] << "\n";
	else
		cout << "OK\n";

	M[id]++;
}

int main()
{
	cin >> n;

	while (n--)
	{
		cin >> s;

		upd();
	}
}


#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <vector>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <string.h>
#include <limits.h>
#include <algorithm>
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#define ff first
#define ss second
#define ll long long
#define pb push_back
#define mp make_pair
#define inf 1000000007
#define mod 1000000007
#define pii pair <int, int>
#define all(x) x.begin(), x.end()
//#define tr(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)

//using namespace __gnu_pbds;

using namespace std;

//template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int n, now;

char s[40];

int M[3000005];

char type[3000005];

vector <int> T[3000005];

void upd()
{
	int sz = strlen(s);

	int id = 0;
	for (int i = 0; i < sz; i++)
	{
		int pos = -1;
		for (auto h: T[id])
			if (type[h] == s[i])
				pos = h;

		if (pos != -1)
			id = pos;
		else
			T[id].pb(++now), id = now, type[id] = s[i];
	}

	if (M[id])
		printf("%s%d", s, M[id]);
	else
		printf("OK\n");

	M[id]++;
}

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

	while (n--) {
		scanf("%s", s);
		upd();
	}
}


#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <vector>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <string.h>
#include <limits.h>
#include <algorithm>
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#define ff first
#define ss second
#define ll long long
#define pb push_back
#define mp make_pair
#define inf 1000000007
#define mod 1000000007
#define pii pair <int, int>
#define all(x) x.begin(), x.end()
//#define tr(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)

//using namespace __gnu_pbds;

using namespace std;

//template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int n;

string s;

map <ll, map <ll, int> > M;

int main()
{
	cin >> n;

	while (n--)
	{
		cin >> s;

		ll H = 0, T = 0;
		for (int i = 0; i < s.length(); i++)
			H = H*27 + (s[i]), H %= mod,
			T = T*53 + (s[i]), T %= mod;

		if (M[H][T])
			cout << s << M[H][T] << "\n";
		else
			cout << "OK\n";

		M[H][T]++;
	}
}


#include<iostream>
#include<string>
#include<set>

using namespace std;

int main()
{
    int n;
    cin>>n;

    set<string> names;

    for(int i=0;i<n;i++)
    {
        string str;
        cin>>str;

        int s=0;

        string strf=str;

        while(names.find(strf)!=names.end())
        {
            s++;
            std::string ss=std::to_string(s);
            strf=str+ss;
        }
        if(strf==str)
        {
            cout<<"OK"<<endl;
            names.insert(str);
        }
        else
        {
            cout<<strf<<endl;
            names.insert(strf);
        }

    }
    return 0;

}


#include<iostream>
#include<string>
#include<set>

using namespace std;

int main()
{
    int n;
    cin>>n;

    set<string> names;

    for(int i=0;i<n;i++)
    {
        string str;
        cin>>str;

        int s=0;

        string strf=str;

        if(names.find(str)==names.end())
        {
            cout<<"OK"<<endl;
            names.insert(str);
        }
        else
        {
            while(names.find(strf)!=names.end())
            {
                s++;
                std::string ss=std::to_string(s);
                strf=str+ss;
            }
            cout<<strf<<endl;
            names.insert(strf);
        }

    }
    return 0;

}


#include<iostream>
#include<string>
#include<vector>

using namespace std;

int main()
{
    int n;
    cin>>n;

    vector<string> names;

    for(int i=0;i<n;i++)
    {
        string str;
        cin>>str;

        int sum=0;

        for(int j=0;j<names.size();j++)
        {
            if(str==names[j])
            {
                sum++;
                break;
            }
        }

        if(sum==0)
        {
            names.push_back(str);
            cout<<"OK"<<endl;
        }
        else
        {
            int s=1;
            auto ss=std::to_string(s);
            string strf=str+ss;

            for(int j=0;j<names.size();j++)
            {
                strf=str+ss;
                if(strf==names[j])
                {
                    s++;
                    j=0;
                }
            }
            names.push_back(strf);
            cout<<strf<<endl;

        }
    }
    return 0;

}


#include<iostream>
#include<string>
#include<map>
#include<iterator>

using namespace std;

int main()
{
    long int n;
    cin>>n;

    map<string,int>names;
    map<string,int>::iterator itr;

    for(long int i=0;i<n;i++)
    {
        string str;
        cin>>str;

        long int s=0;
        string strf=str;

        while(names.find(strf)!=names.end())
        {
            s++;
            std::string ss=std::to_string(s);
            strf=str+ss;
        }
        if(strf==str)
        {
            cout<<"OK"<<endl;
            names.insert({str,0});
        }
        else
        {
            cout<<strf<<endl;
            names.insert({strf,s});
        }

        // if(names.find(strf)!=names.end() & s==0)
        // {
        //     s++;
        //     std::string ss=std::to_string(s);
        //     strf=str+ss;
        //     cout<<strf<<endl;
        //     names.insert({strf,s});
        // }
        // else
        // {
        //     cout<<"OK"<<endl;
        //     names.insert({str,s});
        // }
    }
    return 0;

}


#include<iostream>
#include<string>
#include<vector>

using namespace std;

int main()
{
    int n;
    cin>>n;

    vector<string> names;

    for(int i=0;i<n;i++)
    {
        string str;
        cin>>str;

        int s=0;

        string strf=str;

        for(int j=0;j<names.size();j++)
        {
            int tot=-1;
            if(strf==names[j])
            {
                tot++;
                s++;
                j=0;

                auto ss=std::to_string(s);
                strf=str+ss;
            }
        }
        if(s==0)
        {
            cout<<"OK"<<endl;
            names.push_back(str);
        }
        else if(str!=strf)
        {
            cout<<strf<<endl;
            names.push_back(strf);
        }

    }
    return 0;

}


#include<iostream>
#include<string>
#include<vector>

using namespace std;

int main()
{
    int n;
    cin>>n;

    vector<string> names;

    for(int i=0;i<n;i++)
    {
        string str;
        cin>>str;

        int s=0;

        string strf=str;;

        for(int j=0;j<names.size();j++)
        {
            int tot=-1;
            if(strf==names[j])
            {
                tot++;
                s++;
                j=0;

                auto ss=std::to_string(s);
                strf=str+ss;
            }
        }
        if(s==0 && str==strf)
        {
            cout<<"OK"<<endl;
            names.push_back(strf);
        }
        else if(str!=strf)
        {
            cout<<strf<<endl;
            names.push_back(strf);
        }

    }
    return 0;

}


#include<iostream>
#include<string>
#include<vector>

using namespace std;

int main()
{
    int n;
    cin>>n;

    vector<string> names;

    for(int i=0;i<n;i++)
    {
        string str;
        cin>>str;

        int sum=0;

        for(int j=0;j<names.size();j++)
        {
            if(str==names[j])
            {
                sum++;
                break;
            }
        }

        if(sum==0)
        {
            names.push_back(str);
            cout<<"OK"<<endl;
        }
        else
        {
            int s=1;
            auto ss=std::to_string(s);

            string strf=str+ss;

            for(int j=0;j<names.size();j++)
            {
                ss=std::to_string(s);
                strf=str+ss;

                if(strf==names[j])
                {
                    ++s;
                    j=0;
                }
            }
            names.push_back(strf);
            cout<<strf<<endl;

        }
    }
    return 0;

}


#include<iostream>
#include<string>
#include<vector>

using namespace std;

int main()
{
    int n;
    cin>>n;

    vector<string> names;

    for(int i=0;i<n;i++)
    {
        string str;
        cin>>str;

        int s=0;

        string strf=str;

        for(int j=0;j<names.size();j++)
        {
            int tot=-1;
            if(strf==names[j])
            {
                tot++;
                s++;

                auto ss=std::to_string(s);
                strf=str+ss;
            }
        }
        if(s==0)
        {
            cout<<"OK"<<endl;
            names.push_back(str);
        }
        else if(str!=strf)
        {
            cout<<strf<<endl;
            names.push_back(strf);
        }

    }
    return 0;

}


#include<iostream>
#include<string>
#include<map>
#include<iterator>

using namespace std;

int main()
{
    long int n;
    cin>>n;

    map<string,int>names;
    map<string,int>::iterator itr;

    for(long int i=0;i<n;i++)
    {
        string str;
        cin>>str;

        int s=0;
        string strf=str;

        if(names.find(strf)==names.end())
        {
            cout<<"OK"<<endl;
            names.insert({str,0});
        }

        else
        {
            s=names.find(str)->second;
            names.find(str)->second++;
            std::string ss=std::to_string(++s);
            strf=str+ss;

            cout<<strf<<endl;
            names.insert({strf,s});
        }
   }
    return 0;

}


#include<iostream>
#include<string>
#include<vector>

using namespace std;

int main()
{
    int n;
    cin>>n;

    vector<string> names;

    for(int i=0;i<n;i++)
    {
        string str;
        cin>>str;

        int s=0;

        string strf=str;

        for(int j=0;j<names.size();j++)
        {
            if(strf==names[j])
            {
                s++;

                auto ss=std::to_string(s);
                strf=str+ss;
            }
        }
        if(s==0)
        {
            cout<<"OK"<<endl;
            names.push_back(str);
        }
        else
        {
            cout<<strf<<endl;
            names.push_back(strf);
        }

    }
    return 0;

}


#include<iostream>
#include<string>
#include<set>

using namespace std;

int main()
{
    long int n;
    cin>>n;

    set<string> names;

    for(long int i=0;i<n;i++)
    {
        string str;
        cin>>str;

        long int s=0;
        string strf=str;

        while(names.find(strf)!=names.end())
        {
            s++;
            std::string ss=std::to_string(s);
            strf=str+ss;
        }
        if(strf==str)
        {
            cout<<"OK"<<endl;
            names.insert(str);
        }
        else
        {
            cout<<strf<<endl;
            names.insert(strf);
        }

    }
    return 0;

}


#include<iostream>
#include<string>
#include<vector>

using namespace std;

int main()
{
    int n;
    cin>>n;

    vector<string> names;

    for(int i=0;i<n;i++)
    {
        string str;
        cin>>str;

        int s=0;

        string strf=str;

        for(int j=0;j<names.size();j++)
        {
            if(strf==names[j])
            {
                s++;

                std::string ss=std::to_string(s);
                strf=str+ss;
            }
        }
        if(s==0)
        {
            cout<<"OK"<<endl;
            names.push_back(str);
        }
        else
        {
            cout<<strf<<endl;
            names.push_back(strf);
        }

    }
    return 0;

}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 33307;
const int mod = (int)1e9;

string s;
int pw[35];
map<int, int>mp;

string combine(string a, int n)
{
    string b = "";

    while (n)
    {
        b = char(n % 10 + '0') + b;
        n /= 10;
    }

    return (a + b);
}

int j;

void gen(string a, int hash)
{
    string b;
    for (; j <= int(1e5 + 7); j++)
    {
        b = combine(a, j);

        int hs = hash;

        for (int i = a.size(); i < b.size(); i++)
        {
            hs += ((b[i] - 'a' + 1) * pw[i]) % mod;
        }
        if (!mp[hs])
        {
            s = b;
            mp[hs] = 1;
            break;
        }
    }
}

main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            gen(s, hs);
            cout << s << endl;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 997;
const int mod = (int)1e9;

string s;
int pw[35];
map<int, int>mp;

string combine(string a, int n)
{
    string b = "";

    while (n)
    {
        b = char(n % 10 + '0') + b;
        n /= 10;
    }

    return (a + b);
}

void gen(string a, int hash)
{
    string b;
    for (int j = 0; j <= 35; j++)
    {
        b = combine(a, j);

        int hs = hash;

        for (int i = a.size(); i < b.size(); i++)
        {
            hs += ((b[i] - 'a' + 1) * pw[i]) % mod;
        }
        if (!mp[hs])
        {
            s = b;
            mp[hs] = 1;
            break;
        }
    }
}

main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            gen(s, hs);
            cout << s << endl;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 3037;
const int mod = (int)1e9;

string s;
int pw[35];
map<int, int>mp;
map<int, int>cnt;

string combine(string a, int b)
{
    string c = "";

    while (b)
    {
        c.pb(b % 10);
        b /= 10;
    }
    reverse(c.begin(), c.end());
    return (a + c);
}

main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            cout << s;
            cout << cnt[hs] + 1 << endl;
            cnt[hs]++;
            s = combine(s, cnt[hs]);
            for (int i = 0; i < s.size(); i++)
            {
                hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
                hs %= mod;
            }
            mp[hs] = 1;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 3037;
const int mod = (int)1e9;

string s;
int pw[35];
map<int, int>mp;
map<int, int>cnt;

main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            cout << s;
            cout << cnt[hs] + 1 << endl;
            cnt[hs] ++;
            mp[hs + (cnt[hs] * 1LL * K) % mod] = 1;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 997;
const int mod = (int)1e9;

string s;
int pw[35];
map<int, int>mp;
map<int, int>cnt;

main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            cout << s;
            cout << cnt[hs] + 1 << endl;
            cnt[hs] ++;
            mp[hs + cnt[hs]] = 1;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 3037;
const int mod = (int)1e9;

string s;
int pw[35];
map<int, int>mp;
map<int, int>cnt;

main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            cout << s;
            cout << cnt[hs] + 1 << endl;
            cnt[hs] ++;
           // mp[hs + cnt[hs]] = 1;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 997;
const int mod = (int)1e9;

string s;
int pw[35];
map<int, int>mp;

string combine(string a, int n)
{
    string b = "";

    while (n)
    {
        b = char(n % 10 + '0') + b;
        n /= 10;
    }

    return (a + b);
}

void gen(string a, int hash)
{
    string b;
    for (int j = 0; j <= 45; j++)
    {
        b = combine(a, j);

        int hs = hash;

        for (int i = a.size(); i < b.size(); i++)
        {
            hs += ((b[i] - 'a' + 1) * pw[i]) % mod;
        }
        if (!mp[hs])
        {
            s = b;
            mp[hs] = 1;
            break;
        }
    }
}

main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            gen(s, hs);
            cout << s << endl;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 997;
const int mod = (int)1e9;

string s;
int pw[35];
map<int, int>mp;

string combine(string a, int n)
{
    string b = "";

    while (n)
    {
        b = char(n % 10 + '0') + b;
        n /= 10;
    }

    return (a + b);
}

void gen(string a, int hash)
{
    string b;
    for (int j = 0; j <= 10; j++)
    {
        b = combine(a, j);

        int hs = hash;

        for (int i = a.size(); i < b.size(); i++)
        {
            hs += ((b[i] - 'a' + 1) * pw[i]) % mod;
        }
        if (!mp[hs])
        {
            s = b;
            mp[hs] = 1;
            break;
        }
    }
}

main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            gen(s, hs);
            cout << s << endl;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 997;
const int mod = (int)1e9;

string s;
int pw[35];
map<int, int>mp;

string combine(string a, int n)
{
    string b = "";

    while (n)
    {
        b = char(n % 10 + '0') + b;
        n /= 10;
    }

    return (a + b);
}

void gen(string a, int hash)
{
    string b;
    for (int j = 0; j <= 54; j++)
    {
        b = combine(a, j);

        int hs = hash;

        for (int i = a.size(); i < b.size(); i++)
        {
            hs += ((b[i] - 'a' + 1) * pw[i]) % mod;
        }
        if (!mp[hs])
        {
            s = b;
            mp[hs] = 1;
            break;
        }
    }
}

main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            gen(s, hs);
            cout << s << endl;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 33307;
const int mod = (int)1e9 + 7;

string s;
int pw[35];
map<int, int>mp;

string combine(string a, int n)
{
    string b = "";

    while (n)
    {
        b.pb(n % 10 + '0');
        n /= 10;
    }

    return (a + b);
}

int j;

void gen(string a, int hash)
{
    string b;
    for (; j <= int(1e5 + 7); j++)
    {
        b = combine(a, j);

        int hs = hash;

        for (int i = a.size(); i < b.size(); i++)
        {
            hs += ((b[i] - 'a' + 1) * pw[i]) % mod;
        }
        if (!mp[hs])
        {
            s = b;
           // mp[hs] = 1;
            break;
        }
    }
}

main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            gen(s, hs);
            cout << s << endl;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 997;
const int mod = (int)1e9;

string s;
int pw[35];
map<int, int>mp;

string combine(string a, int n)
{
    string b = "";

    while (n)
    {
        b = char(n % 10 + '0') + b;
        n /= 10;
    }

    return (a + b);
}

void gen(string a, int hash)
{
    string b;
    for (int j = 0; j <= int(1e3 + 7); j++)
    {
        b = combine(a, j);

        int hs = hash;

        for (int i = a.size(); i < b.size(); i++)
        {
            hs += ((b[i] - 'a' + 1) * pw[i]) % mod;
        }
        if (!mp[hs])
        {
            s = b;
            mp[hs] = 1;
            break;
        }
    }
}

main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            gen(s, hs);
            cout << s << endl;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 307;
const int mod = (int)1e9;

string s;
int pw[35];
map<int, int>mp;

string combine(string a, int n)
{
    string b = "";

    while (n)
    {
        b = char(n % 10 + '0') + b;
        n /= 10;
    }

    return (a + b);
}

int j;

void gen(string a, int hash)
{
    string b;
    for (; j <= int(1e5 + 7); j++)
    {
        b = combine(a, j);

        int hs = hash;

        for (int i = a.size(); i < b.size(); i++)
        {
            hs += ((b[i] - 'a' + 1) * pw[i]) % mod;
        }
        if (!mp[hs])
        {
            s = b;
            mp[hs] = 1;
            break;
        }
    }
}

main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            gen(s, hs);
            cout << s << endl;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 997;
const int mod = (int)1e9;

string s;
int pw[35];
map<int, int>mp;

string combine(string a, int n)
{
    string b = "";

    while (n)
    {
        b.pb(char(n % 10 + '0'));
        n /= 10;
    }
    reverse(b.begin(), b.end());
    return (a + b);
}

void gen(string a, int hash)
{
    string b;
    for (int j = 0; j <= 54; j++)
    {
        b = combine(a, j);

        int hs = hash;

        for (int i = a.size(); i < b.size(); i++)
        {
            hs += ((b[i] - 'a' + 1) * pw[i]) % mod;
        }
        if (!mp[hs])
        {
            s = b;
            mp[hs] = 1;
            break;
        }
    }
}

main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            gen(s, hs);
            cout << s << endl;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 997;
const int mod = (int)1e9;

string s;
int pw[35];
map<int, int>mp;

string combine(string a, int n)
{
    string b = "";

    while (n)
    {
        b = char(n % 10 + '0') + b;
        n /= 10;
    }

    return (a + b);
}

void gen(string a, int hash)
{
    string b;
    for (int j = 0; j <= int(1e5 + 7); j++)
    {
        b = combine(a, j);

        int hs = hash;

        for (int i = a.size(); i < b.size(); i++)
        {
            hs += ((b[i] - 'a' + 1) * pw[i]) % mod;
        }
        if (!mp[hs])
        {
            s = b;
            mp[hs] = 1;
            break;
        }
    }
}

main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            gen(s, hs);
            cout << s << endl;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 33307;
const int mod = (int)1e9 + 7;

string s;
int pw[35];
map<int, int>mp;

string combine(string a, int n)
{
    string b = "";

    while (n)
    {
        b = char(n % 10 + '0') + b;
        n /= 10;
    }

    return (a + b);
}

int j;

void gen(string a, int hash)
{
    string b;
    for (; j <= int(1e5 + 7); j++)
    {
        b = combine(a, j);

        int hs = hash;

        for (int i = a.size(); i < b.size(); i++)
        {
            hs += ((b[i] - 'a' + 1) * pw[i]) % mod;
        }
        if (!mp[hs])
        {
            s = b;
            mp[hs] = 1;
            break;
        }
    }
}

main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            gen(s, hs);
            cout << s << endl;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 997;
const int mod = (int)1e9;

string s;
int pw[35];
map<int, int>mp;

string combine(string a, int n)
{
    string b = "";

    while (n)
    {
        b = char(n % 10 + '0') + b;
        n /= 10;
    }

    return (a + b);
}

void gen(string a, int hash)
{
    string b;
    for (int j = 0; j <= 55; j++)
    {
        b = combine(a, j);

        int hs = hash;

        for (int i = a.size(); i < b.size(); i++)
        {
            hs += ((b[i] - 'a' + 1) * pw[i]) % mod;
        }
        if (!mp[hs])
        {
            s = b;
            mp[hs] = 1;
            break;
        }
    }
}

main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            gen(s, hs);
            cout << s << endl;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 3037;
const int mod = (int)1e9;

string s;
int pw[35];
map<int, int>mp;
map<int, int>cnt;

main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            cout << s;
            cout << cnt[hs] + 1 << endl;
            cnt[hs] ++;
            mp[hs + cnt[hs]] = 1;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 997;
const int mod = (int)1e9;

string s;
int pw[35];
map<int, int>mp;

string combine(string a, int n)
{
    string b = "";

    while (n)
    {
        b = char(n % 10 + '0') + b;
        n /= 10;
    }

    return (a + b);
}

void gen(string a, int hash)
{
    string b;
    for (int j = 0; j <= 50; j++)
    {
        b = combine(a, j);

        int hs = hash;

        for (int i = a.size(); i < b.size(); i++)
        {
            hs += ((b[i] - 'a' + 1) * pw[i]) % mod;
        }
        if (!mp[hs])
        {
            s = b;
            mp[hs] = 1;
            break;
        }
    }
}

main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            gen(s, hs);
            cout << s << endl;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 997;
const int mod = (int)1e9;

string s;
int pw[35];
map<int, int>mp;

string combine(string a, int n)
{
    string b = "";

    while (n)
    {
        b = char(n % 10 + '0') + b;
        n /= 10;
    }

    return (a + b);
}

void gen(string a, int hash)
{
    string b;
    for (int j = 0; j <= 40; j++)
    {
        b = combine(a, j);

        int hs = hash;

        for (int i = a.size(); i < b.size(); i++)
        {
            hs += ((b[i] - 'a' + 1) * pw[i]) % mod;
        }
        if (!mp[hs])
        {
            s = b;
            mp[hs] = 1;
            break;
        }
    }
}

main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            gen(s, hs);
            cout << s << endl;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 33307;
const int mod = (int)1e9 + 7;

string s;
int pw[35];
map<int, int>mp;

string combine(string a, int n)
{
    string b = "";

    while (n)
    {
        b.pb(n % 10 + '0');
        n /= 10;
    }

    return (a + b);
}
int j;
void gen(string a, int hash)
{
    string b;
    for (; j <= int(1e5 + 7); j++)
    {
        b = combine(a, j);
        int hs = hash;
        for (int i = a.size(); i < b.size(); i++)
        {
            hs += ((b[i] - 'a' + 1) * pw[i]) % mod;
        }
        if (!mp[hs])
        {
            s = b;
            mp[hs] = 1;
            break;
        }
    }
}
main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            gen(s, hs);
            cout << s << endl;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 3037;
const int mod = (int)1e9;

string s;
int pw[35];
map<int, int>mp;
map<int, int>cnt;

main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            cout << s;
            cout << cnt[hs] + 1 << endl;
            cnt[hs] ++;
            mp[hs + (cnt[hs] * 1LL * K * pw[K / hs]) % mod] = 1;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 997;
const int mod = (int)1e9;

string s;
int pw[35];
map<int, int>mp;

string combine(string a, int n)
{
    string b = "";

    while (n)
    {
        b.pb(char(n % 10 + '0'));
        n /= 10;
    }
    reverse(b.begin(), b.end());
    return (a + b);
}

void gen(string a, int hash)
{
    string b;
    for (int j = 0; j <= 100; j++)
    {
        b = combine(a, j);

        int hs = hash;

        for (int i = a.size(); i < b.size(); i++)
        {
            hs += ((b[i] - 'a' + 1) * pw[i]) % mod;
        }
        if (!mp[hs])
        {
            s = b;
            mp[hs] = 1;
            break;
        }
    }
}

main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            gen(s, hs);
            cout << s << endl;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 997;
const int mod = (int)1e9;

string s;
int pw[35];
map<int, int>mp;

string combine(string a, int n)
{
    string b = "";

    while (n)
    {
        b = char(n % 10 + '0') + b;
        n /= 10;
    }

    return (a + b);
}

int j;

void gen(string a, int hash)
{
    string b;
    for (; j <= int(1e5 + 7); j++)
    {
        b = combine(a, j);

        int hs = hash;

        for (int i = a.size(); i < b.size(); i++)
        {
            hs += ((b[i] - 'a' + 1) * pw[i]) % mod;
        }
        if (!mp[hs])
        {
            s = b;
            mp[hs] = 1;
            break;
        }
    }
}

main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            gen(s, hs);
            cout << s << endl;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 997;
const int mod = (int)1e9;

string s;
int pw[35];
map<int, int>mp;

string combine(string a, int n)
{
    string b = "";

    while (n)
    {
        b = char(n % 10 + '0') + b;
        n /= 10;
    }

    return (a + b);
}

void gen(string a, int hash)
{
    string b;
    for (int j = 0; j <= 100; j++)
    {
        b = combine(a, j);

        int hs = hash;

        for (int i = a.size(); i < b.size(); i++)
        {
            hs += ((b[i] - 'a' + 1) * pw[i]) % mod;
        }
        if (!mp[hs])
        {
            s = b;
            mp[hs] = 1;
            break;
        }
    }
}

main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            gen(s, hs);
            cout << s << endl;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 997;
const int mod = (int)1e9;

string s;
int pw[35];
map<int, int>mp;

string combine(string a, int n)
{
    string b = "";

    while (n)
    {
        b = char(n % 10 + '0') + b;
        n /= 10;
    }

    return (a + b);
}

void gen(string a, int hash)
{
    string b;
    for (int j = 0; j <= int(1e2 + 7); j++)
    {
        b = combine(a, j);

        int hs = hash;

        for (int i = a.size(); i < b.size(); i++)
        {
            hs += ((b[i] - 'a' + 1) * pw[i]) % mod;
        }
        if (!mp[hs])
        {
            s = b;
            mp[hs] = 1;
            break;
        }
    }
}

main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            gen(s, hs);
            cout << s << endl;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 997;
const int mod = (int)1e9;

string s;
int pw[35];
map<int, int>mp;

string combine(string a, int n)
{
    string b = "";

    while (n)
    {
        b = char(n % 10 + '0') + b;
        n /= 10;
    }

    return (a + b);
}

void gen(string a, int hash)
{
    string b;
    for (int j = 0; j <= int(5e2 + 7); j++)
    {
        b = combine(a, j);

        int hs = hash;

        for (int i = a.size(); i < b.size(); i++)
        {
            hs += ((b[i] - 'a' + 1) * pw[i]) % mod;
        }
        if (!mp[hs])
        {
            s = b;
            mp[hs] = 1;
            break;
        }
    }
}

main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            gen(s, hs);
            cout << s << endl;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define OK cout << "OK" << endl;

using namespace std;

const int K = 3037;
const int mod = (int)1e9;

string s;
int pw[35];
map<int, int>mp;
map<int, int>cnt;

string combine(string a, int b)
{
    string c = "";

    while (b)
    {
        c.pb(b % 10);
        b /= 10;
    }
    reverse(c.begin(), c.end());
    return (a + c);
}

main()
{
    int n;

    cin >> n;

    pw[0] = 1;

    for (int i = 1; i <= 33; i++)
    {
        pw[i] = (pw[i - 1] * 1LL * K) % mod;
    }

    while (n--)
    {
        cin >> s;

        int hs = 0;

        for (int i = 0; i < s.size(); i++)
        {
            hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
            hs %= mod;
        }

        if (mp[hs])
        {
            cout << s;
            cout << cnt[hs] + 1 << endl;
            s = combine(s, cnt[hs]);
            for (int i = 0; i < s.size(); i++)
            {
                hs = hs + ((s[i] - 'a' + 1) * 1LL * pw[i]) % mod;
                hs %= mod;
            }
            mp[hs] = 1;
        }
        else
        {
            cout << "OK" << endl;
            mp[hs] = 1;
        }
    }
}


#pragma GCC optimize(2)

#include <iostream>
#include <tr1/unordered_map>

std::tr1::unordered_map <std::string, int> wib;

int main(void)
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(NULL), std::cout.tie(NULL);
	register int n;
	register std::string str;
	std::cin >> n;
	while(n --)
	{
		std::cin >> str;
		int& temp = wib[str];
		if(!temp)
			std::cout << "OK" << std::endl;
		else
			std::cout << str << temp << std::endl;
		++ temp;
	}
	return 0;
}


#pragma G++ optimize(3)

#include <iostream>
#include <tr1/unordered_map>

std::tr1::unordered_map <std::string, int> wib;

int main(void)
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(NULL), std::cout.tie(NULL);
	register int n;
	register std::string str;
	std::cin >> n;
	while(n --)
	{
		std::cin >> str;
		int& temp = wib[str];
		if(!temp)
			std::cout << "OK" << std::endl;
		else
			std::cout << str << temp << std::endl;
		++ temp;
	}
	return 0;
}


#pragma GCC optimize(2)

#include <iostream>

template <typename Key, typename Value>
class map
{
	static int rand()
	{
		static unsigned seed = 233;
		return seed = ((int)seed * 481233LL) & 0x7FFFFFFF;
	}

	struct Node
	{
		Key key;
		Value val;
		int size, pri;
		Node *ch[2];

		Node(const Key &_key)
		{
			size = 1;
			ch[0] = ch[1] = NULL;
			key = _key;
			pri = rand();
		}

		void update()
		{
			size = 1;
			if(ch[0])
				size += ch[0]->size;
			if(ch[1])
				size += ch[1]->size;
		}
	} *root;

	void Rotate(Node *&now, const bool &flag)
	{
		register Node *temp = now->ch[!flag];
		now->ch[!flag] = temp->ch[flag];
		temp->ch[flag] = now;
		temp->size = now->size;
		now->update();
		now = temp;
	}

	Value& Insert(Node *&now, const Key &key)
	{
		if(!now)
		{
			now = new Node(key);
			return now->val;
		}
		if(now->key == key)
			return now->val;
		bool flag = key > now->key;
		Value &temp = Insert(now->ch[flag], key);
		if(now->ch[flag]->pri < now->pri)
			Rotate(now, !flag);
		else
			now->update();
		return temp;
	}

public :
	Value& operator [] (const Key &key)
	{
		return Insert(root, key);
	}

	bool count(const Key &key)
	{
		register Node *temp = root;
		while(temp)
		{
			if(temp->key == key)
				return true;
			temp = temp->ch[key > temp->key];
		}
		return false;
	}
};

map <std::string, int> wib;
int n;
std::string str;

int main(void)
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(NULL), std::cout.tie(NULL);
	std::cin >> n;
	while(n --)
	{
		std::cin >> str;
		if(wib.count(str))
		{
		    int& temp = wib[str];
		    std::cout << str << temp << std::endl;
		    ++ temp;
		}
		else
		{
		    wib[str] = 1;
		    std::cout << "OK" << std::endl;
		}
	}
	return 0;
}


#include <iostream>
#include <map>

__attribute__ ((optimize("-O2")))

std::map <std::string, int> wib;

int main(void)
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(NULL), std::cout.tie(NULL);
    register int n;
    register std::string str;
    std::cin >> n;
    while(n --)
    {
        std::cin >> str;
        int& temp = wib[str];
        if(!temp)
            std::cout << "OK" << std::endl;
        else
            std::cout << str << temp << std::endl;
        ++ temp;
    }
    return 0;
}


#pragma G++ optimize(3)

#include <iostream>
#include <map>

std::map <std::string, int> wib;

int main(void)
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(NULL), std::cout.tie(NULL);
	register int n;
	register std::string str;
	std::cin >> n;
	while(n --)
	{
		std::cin >> str;
		int& temp = wib[str];
		if(!temp)
			std::cout << "OK" << std::endl;
		else
			std::cout << str << temp << std::endl;
		++ temp;
	}
	return 0;
}


/**********************************************************
 * Author        : EndSaH
 * Email         : 2499808100@qq.com
 * Created Time  : 2018-11-02 08:49
 * FileName      : temp.cpp
 * *******************************************************/

#pragma GCC optimize(3)

#include <iostream>
#include <tr1/unordered_set>

std::tr1::unordered_multiset<std::string> SET;

int main()
{
	register int n, cnt;
	register std::string str;
	std::ios::sync_with_stdio(false);
	std::cin.tie(NULL), std::cout.tie(NULL);

	std::cin >> n;
	while(n--)
	{
		std::cin >> str;
		cnt = SET.count(str);
		if(cnt)
			std::cout << str << cnt << '\n';
		else
			std::cout << "OK\n";
		SET.insert(str);
	}

	return 0;
}


#pragma GCC optimize(2)

#include <iostream>

template <typename Key, typename Value>
class map
{
	static int rand()
	{
		static unsigned seed = 233;
		return seed = ((int)seed * 481233LL) & 0x7FFFFFFF;
	}

	struct Node
	{
		Key key;
		Value val;
		int size, pri;
		Node *ch[2];

		Node(const Key &_key)
		{
			size = 1;
			ch[0] = ch[1] = NULL;
			key = _key;
			pri = rand();
		}

		void update()
		{
			size = 1;
			if(ch[0])
				size += ch[0]->size;
			if(ch[1])
				size += ch[1]->size;
		}
	} *root;

	void Rotate(Node *&now, const bool &flag)
	{
		register Node *temp = now->ch[!flag];
		now->ch[!flag] = temp->ch[flag];
		temp->ch[flag] = now;
		temp->size = now->size;
		now->update();
		now = temp;
	}

	Value& Insert(Node *&now, const Key &key)
	{
		if(!now)
		{
			now = new Node(key);
			return now->val;
		}
		if(now->key == key)
			return now->val;
		bool flag = key > now->key;
		Value &temp = Insert(now->ch[flag], key);
		if(now->ch[flag]->pri < now->pri)
			Rotate(now, !flag);
		else
			now->update();
		return temp;
	}

public :
	Value& operator [] (const Key &key)
	{
		return Insert(root, key);
	}

	bool count(const Key &key)
	{
		register Node *temp = root;
		while(temp)
		{
			if(temp->key == key)
				return true;
			temp = temp->ch[key > temp->key];
		}
		return false;
	}
};

map <std::string, int> wib;
int n;
std::string str;

int main(void)
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(NULL), std::cout.tie(NULL);
	std::cin >> n;
	while(n --)
	{
		std::cin >> str;
		int& temp = wib[str];
		if(!temp)
			std::cout << "OK" << std::endl;
		else
			std::cout << str << temp << std::endl;
		++ temp;
	}
	return 0;
}


#pragma GCC optimize(2)

#include <iostream>
#include <cstring>

class Trie
{
	struct Node
	{
		int cnt;
		Node *ch[26];

		Node()
		{
		    cnt = 0;
		    memset(ch, 0, 26);
		}
	} *root = new Node;

public :
	int Insert(const std::string &str)
	{
		register int m;
		register Node *temp = root;
		for(register int i = 0; i ^ str.size(); ++ i)
		{
			m = str[i] - 'a';
			if(!temp->ch[m])
				temp->ch[m] = new Node;
			temp = temp->ch[m];
		}
		++ temp->cnt;
		return temp->cnt - 1;
	}
} wib;

int main(void)
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(NULL), std::cout.tie(NULL);
	register int n, tmp;
	register std::string str;
	std::cin >> n;
	while(n --)
	{
		std::cin >> str;
		tmp = wib.Insert(str);
		if(!tmp)
			std::cout << "OK" << std::endl;
		else
			std::cout << str << tmp << std::endl;
	}
	return 0;
}


#include <iostream>
#include <map>

#pragma GCC optimize(2)

std::map <std::string, int> wib;

int main(void)
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(NULL), std::cout.tie(NULL);
    register int n;
    register std::string str;
    std::cin >> n;
    while(n --)
    {
        std::cin >> str;
        int& temp = wib[str];
        if(!temp)
            std::cout << "OK" << std::endl;
        else
            std::cout << str << temp << std::endl;
        ++ temp;
    }
    return 0;
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            string [] st = Console.ReadLine().Split(':');
            int[] A = new int [4];
            int a = int.Parse(st[0]) / 10;
            int b = int.Parse(st[0]) % 10;
            int x = int.Parse(st[1]) / 10;
            int y = int.Parse(st[1]) % 10;
            x = (x > 5) ? 5 : x;
            if (n == 24)
            {
                a = (a > 2) ? 1 : a;
                b = (a == 2 && b > 4) ? 3 : b;
            }

            if (n == 12)
            {
                a = a > 1 ? 0 : a;
                b = (a == 1 && b > 2) ? 1 : b;
                if (a == 0 && b == 0) a = 1;
            }

            Console.WriteLine(a+""+b + ":"+ x+""+y);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            string [] st = Console.ReadLine().Split(':');
            int[] A = new int [4];
            int a = int.Parse(st[0]) / 10;
            int b = int.Parse(st[0]) % 10;
            int x = int.Parse(st[1]) / 10;
            int y = int.Parse(st[1]) % 10;
            x = (x > 5) ? 5 : x;
            if (n == 24)
            {
                a = (a > 2) ? 1 : a;
                b = (a == 2 && b > 4) ? 3 : b;
                if (a == 2 && b == 4) a = 1;
            }

            if (n == 12)
            {
                a = a > 1 ? 0 : a;
                b = (a == 1 && b > 2) ? 1 : b;
                if (a == 0 && b == 0) a = 1;
            }

            Console.WriteLine(a+""+b + ":"+ x+""+y);
        }
    }
}


package main

import (
        "fmt"
        "strconv"
        "strings"
)

func main() {
        var format int
        fmt.Scanln(&format)
        var str string
        fmt.Scanln(&str)
        t := strings.Split(str, ":")
        aStr, bStr := t[0], t[1]
        a, _ := strconv.Atoi(aStr)
        b, _ := strconv.Atoi(bStr)
        if format == 12 {
                if a > 12 {
                        a = a % 10
                        aStr = fmt.Sprintf("0%v", a)
                } else if a == 0 {
                        aStr = "01"
                }
        } else if format == 24 {
                if a > 23 {
                        a = a % 10
                        aStr = fmt.Sprintf("0%v", a)
                }
        }
        if b > 59 {
                b = b % 10
                bStr = fmt.Sprintf("0%v", b)
        }
        fmt.Println(aStr + ":" + bStr)
}


package main

import (
        "fmt"
        "strconv"
        "strings"
)

func main() {
        var format int
        fmt.Scanln(&format)
        var str string
        fmt.Scanln(&str)
        t := strings.Split(str, ":")
        aStr, bStr := t[0], t[1]
        a, _ := strconv.Atoi(aStr)
        b, _ := strconv.Atoi(bStr)
        if format == 12 {
                if a > 12 || a == 0 {
                        a %= 10
                        if a == 0 {
                                aStr = "10"
                        } else {
                                aStr = fmt.Sprintf("0%v", a)
                        }
                }
        } else if format == 24 {
                if a > 23 {
                        a %= 10
                        aStr = fmt.Sprintf("0%v", a)
                }
        }
        if b > 59 {
                b %= 10
                bStr = fmt.Sprintf("0%v", b)
        }
        fmt.Println(aStr + ":" + bStr)
}


package main

import "bufio"
import "os"
import "io/ioutil"
import "fmt"

func changeHour(cat int, hour int) int {
	if cat == 12 {
		if (hour >= 1) && (hour <= 12) {
			return hour
		}
		hour = hour % 10
		if hour == 0 {
			hour++
		}
		return hour
	}
	if hour <= 23 {
		return hour
	}
	return hour % 10
}

func changeMinute(min int) int {
	if min <= 59 {
		return min
	}
	return min % 10
}

func main() {
	var cat, hour, min int
	readInt(&cat)
	readInt(&hour)
	readInt(&min)

	h := changeHour(cat, hour)
	m := changeMinute(min)
	fmt.Printf("%02d:%02d\n", h, m)
}

var out *bufio.Writer
var iPointer int
var nPointer int
var iBuffer []byte

func init() {
	iPointer = 0
	iBuffer, _ = ioutil.ReadAll(os.Stdin)
	nPointer = len(iBuffer)
}

func isDigit(c byte) bool {
	return (c >= '0') && (c <= '9')
}

func readInt(v *int) bool {
	neg := false
	for (iPointer < nPointer) && !isDigit(iBuffer[iPointer]) {
		neg = (iBuffer[iPointer] == '-')
		iPointer++
	}
	if iPointer >= nPointer {
		return false
	}
	var a int
	for (iPointer < nPointer) && isDigit(iBuffer[iPointer]) {
		a = a*10 + int(iBuffer[iPointer]-'0')
		iPointer++
	}
	if neg {
		*v = -a
	} else {
		*v = a
	}
	return true
}


package main

import "bufio"
import "os"
import "io/ioutil"
import "fmt"

func changeHour(cat int, hour int) int {
	if cat == 12 {
		if (hour >= 1) && (hour <= 12) {
			return hour
		}
		if hour == 0 {
			return 1
		}
		return hour % 10
	}
	if hour <= 23 {
		return hour
	}
	return hour % 10
}

func changeMinute(min int) int {
	if min <= 59 {
		return min
	}
	return min % 10
}

func main() {
	var cat, hour, min int
	readInt(&cat)
	readInt(&hour)
	readInt(&min)

	h := changeHour(cat, hour)
	m := changeMinute(min)
	fmt.Printf("%02d:%02d\n", h, m)
}

var out *bufio.Writer
var iPointer int
var nPointer int
var iBuffer []byte

func init() {
	iPointer = 0
	iBuffer, _ = ioutil.ReadAll(os.Stdin)
	nPointer = len(iBuffer)
}

func isDigit(c byte) bool {
	return (c >= '0') && (c <= '9')
}

func readInt(v *int) bool {
	neg := false
	for (iPointer < nPointer) && !isDigit(iBuffer[iPointer]) {
		neg = (iBuffer[iPointer] == '-')
		iPointer++
	}
	if iPointer >= nPointer {
		return false
	}
	var a int
	for (iPointer < nPointer) && isDigit(iBuffer[iPointer]) {
		a = a*10 + int(iBuffer[iPointer]-'0')
		iPointer++
	}
	if neg {
		*v = -a
	} else {
		*v = a
	}
	return true
}


package main

import "bufio"
import "os"
import "io/ioutil"
import "fmt"

func changeHour(cat int, hour int) int {
	if cat == 12 {
		if (hour >= 1) && (hour <= 12) {
			return hour
		}
		if hour%10 == 0 {
			return 10
		}
		return hour % 10
	}
	if hour <= 23 {
		return hour
	}
	return hour % 10
}

func changeMinute(min int) int {
	if min <= 59 {
		return min
	}
	return min % 10
}

func main() {
	var cat, hour, min int
	readInt(&cat)
	readInt(&hour)
	readInt(&min)

	h := changeHour(cat, hour)
	m := changeMinute(min)
	fmt.Printf("%02d:%02d\n", h, m)
}

var out *bufio.Writer
var iPointer int
var nPointer int
var iBuffer []byte

func init() {
	iPointer = 0
	iBuffer, _ = ioutil.ReadAll(os.Stdin)
	nPointer = len(iBuffer)
}

func isDigit(c byte) bool {
	return (c >= '0') && (c <= '9')
}

func readInt(v *int) bool {
	neg := false
	for (iPointer < nPointer) && !isDigit(iBuffer[iPointer]) {
		neg = (iBuffer[iPointer] == '-')
		iPointer++
	}
	if iPointer >= nPointer {
		return false
	}
	var a int
	for (iPointer < nPointer) && isDigit(iBuffer[iPointer]) {
		a = a*10 + int(iBuffer[iPointer]-'0')
		iPointer++
	}
	if neg {
		*v = -a
	} else {
		*v = a
	}
	return true
}


// http://codeforces.com/contest/722/problem/0
package main

import (
	"fmt"
	"bufio"
	"os"
)

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

const (
	MOD = 1e9 + 7
)

type (

)

func main () {
	var (
		N int
		str string
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &N)
	fmt.Fscanln(input, &str)

	ans := []rune(str)
	if N == 12 {
		if ans[0] > '1' {
			if ans[1] == '0' {
				ans[0] = '1'
			} else {
				ans[0] = '0'
			}
		}

		if ans[0] == '1' && ans[1] > '2' {
			ans[1] = '0'
		}

		if ans[0] == '0' && ans[1] == '0' {
			ans[1] = '1'
		}
	} else {
		if ans[1] > '9' || ans[1] < '0' {
			ans[1] = '0'
		}

		if ans[0] > '2' || ans[0] < '0' {
			ans[0] = '0'
		}

		if ans[0] == '2' && ans[1] > '3' {
			ans[1] = '0'
		}
	}

	if ans[3] > '5' || ans[3] < '0' {
		ans[3] = '0'
	}

	fmt.Println(string(ans))
}


// http://codeforces.com/contest/722/problem/0
package main

import (
	"fmt"
	"bufio"
	"os"
)

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

const (
	MOD = 1e9 + 7
)

type (

)

func main () {
	var (
		N int
		str string
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &N)
	fmt.Fscanln(input, &str)

	ans := []rune(str)
	if N == 12 {
		if ans[0] > '1' || ans[0] < '0' {
			ans[0] = '0'
		}

		if ans[1] > '9' || ans[1] < '0' {
			ans[1] = '0'
		}

		if ans[0] == '1' && ans[1] > '2' {
			ans[1] = '0'
		}

		if ans[0] == '0' && ans[1] == '0' {
			ans[1] = '1'
		}
	} else {
		if ans[1] > '9' || ans[1] < '0' {
			ans[1] = '0'
		}

		if ans[0] > '2' || ans[0] < '0' {
			ans[0] = '0'
		}

		if ans[0] == '2' && ans[1] > '3' {
			ans[1] = '0'
		}
	}

	if ans[3] > '5' || ans[3] < '0' {
		ans[3] = '0'
	}

	fmt.Println(string(ans))
}


// http://codeforces.com/contest/722/problem/0
package main

import (
	"fmt"
	"bufio"
	"os"
)

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

const (
	MOD = 1e9 + 7
)

type (

)

func main () {
	var (
		N int
		str string
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &N)
	fmt.Fscanln(input, &str)

	ans := []rune(str)
	if N == 12 {
		if ans[0] > '1' || ans[0] < '0' {
			ans[0] = '0'
		}

		if ans[0] == '1' && ans[1] > '2' {
			ans[1] = '0'
		}

		if ans[3] > '5'  {
			ans[3] = '0'
		}

	} else {
		if ans[0] > '2' || ans[0] < '0' {
			ans[0] = '0'
		}

		if ans[0] == '2' && ans[1] > '4' {
			ans[1] = '0'
		}

		if ans[3] > '5'  {
			ans[3] = '0'
		}
	}

	fmt.Println(string(ans))
}


// http://codeforces.com/contest/722/problem/0
package main

import (
	"fmt"
	"bufio"
	"os"
)

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

const (
	MOD = 1e9 + 7
)

type (

)

func main () {
	var (
		N int
		str string
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &N)
	fmt.Fscanln(input, &str)

	ans := []rune(str)
	if N == 12 {
		if ans[0] > '1' || ans[0] < '0' {
			ans[0] = '0'
		}

		if ans[0] == '1' && ans[1] > '2' {
			ans[1] = '0'
		}

		if ans[0] == '0' && ans[1] == '0' {
			ans[1] = '1'
		}
	} else {
		if ans[0] > '2' || ans[0] < '0' {
			ans[0] = '0'
		}

		if ans[0] == '2' && ans[1] > '3' {
			ans[1] = '0'
		}
	}

	if ans[3] > '5'  {
		ans[3] = '0'
	}

	fmt.Println(string(ans))
}


package main

import (
	"fmt"
)

func main() {
	var format, hour, minute int
	fmt.Scanf("%d\n", &format)
	fmt.Scanf("%d:%d", &hour, &minute)
	if minute > 59 {
		minute = 10 + minute%10
	}
	if format == 24 {
		if hour > 23 {
			hour = hour % 10
		}
	}
	if format == 12 {
		if hour > 12 {
			hour = hour % 10
		}
	}
	fmt.Printf("%02d:%d\n", hour, minute)
}


package main

import (
	"fmt"
)

func main() {
	var format, hour, minute int
	fmt.Scanf("%d\n", &format)
	fmt.Scanf("%d:%d", &hour, &minute)
	if minute > 59 {
		minute = 10 + minute%10
	}
	if format == 24 {
		if hour > 23 {
			hour = hour % 10
		}
	}
	if format == 12 {
		if hour/10 > 1 {
			if hour%10 == 0 {
				hour = 10 + hour%10
			} else {
				hour = hour % 10
			}
		}
		if hour > 12 {
			hour = hour % 10
		}
		if hour == 0 {
			hour = 10
		}
	}
	fmt.Printf("%02d:%02d\n", hour, minute)
}


package main

import (
	"fmt"
)

func main() {
	var format, hour, minute int
	fmt.Scanf("%d\n", &format)
	fmt.Scanf("%d:%d", &hour, &minute)
	if minute > 59 {
		minute = 10 + minute%10
	}
	if format == 24 {
		if hour > 23 {
			hour = hour % 10
		}
	}
	if format == 12 {
		if hour > 12 {
			hour = hour % 10
		}
	}
	fmt.Printf("%02d:%02d\n", hour, minute)
}


package main

import (
	"fmt"
)

func main() {
	var format, hour, minute int
	fmt.Scanf("%d\n", &format)
	fmt.Scanf("%d:%d", &hour, &minute)
	if minute > 59 {
		minute = 10 + minute%10
	}
	if format == 24 {
		if hour > 23 {
			hour = hour % 10
		}
	}
	if format == 12 {
		if hour > 12 {
			hour = hour % 10
		}
		if hour == 0 {
			hour = 1
		}
	}
	fmt.Printf("%02d:%02d\n", hour, minute)
}


package main

import (
	"fmt"
	"strconv"
	"strings"
)

func main() {
	var time_format int
	var time string
	fmt.Scanf("%d\n%s", &time_format, &time)

	new_time := []byte(time)
	time_list := strings.Split(time, ":")
	hours, _ := strconv.Atoi(time_list[0])
	minutes, _ := strconv.Atoi(time_list[1])
	if minutes > 59 {
		new_time[3] = '1'
	}
	if time_format == 12 {
		if hours < 1 {
			new_time[1] = '1'
		} else if hours > 12 {
			if time[1] != '0' {
				new_time[0] = '0'
			} else {
				new_time[0] = '1'
			}
		}
	} else if time_format == 24 && hours > 23 {
		new_time[0] = '0'
	}
	fmt.Println(string(new_time))
}


package main

import (
	"fmt"
	"strconv"
	"strings"
)

func main() {
	var time_format int
	var time string
	fmt.Scanf("%d\n%s", &time_format, &time)

	new_time := []byte(time)
	time_list := strings.Split(time, ":")
	hours, _ := strconv.Atoi(time_list[0])
	minutes, _ := strconv.Atoi(time_list[1])
	if minutes > 59 {
		new_time[3] = '1'
	}
	if (time_format == 12 && hours > 12) || (time_format == 24 && hours > 23) {
		new_time[0] = '0'
	}
	fmt.Println(string(new_time))
}


package main

import (
	"fmt"
	"strconv"
	"strings"
)

func main() {
	var time_format int
	var time string
	fmt.Scanf("%d\n%s", &time_format, &time)

	new_time := []byte(time)
	time_list := strings.Split(time, ":")
	hours, _ := strconv.Atoi(time_list[0])
	minutes, _ := strconv.Atoi(time_list[1])
	if minutes > 59 {
		new_time[3] = '1'
	}
	if time_format == 12 && hours < 1 {
		new_time[1] = '1'
	} else if (time_format == 12 && (hours > 12 || hours < 1)) || (time_format == 24 && hours > 23) {
		new_time[0] = '0'
	}
	fmt.Println(string(new_time))
}


<?php
$format=trim(fgets(STDIN));
$input=trim(fgets(STDIN));
$l=preg_split('//',$input,-1,PREG_SPLIT_NO_EMPTY);
$hh='';$mm='';
list($hh,$mm)=explode(':',$input);
if($format=='12'){
	if($hh>12)
		if($hh{0}>1){
			if($hh{1}>0) $l[0]='0';
			else $l[0]='1';
		}else $l[1]='2';
}else
	if($hh>23) $l[0]='0';
if($mm>59)
	$l[3]='0';
fwrite(STDOUT,implode('',$l));
fwrite(STDOUT,PHP_EOL.'Press "Enter" to exit...'); exit(fgets(STDIN)); //Остановка
?>


<?php
$format=trim(fgets(STDIN));
$input=trim(fgets(STDIN));
$l=preg_split('//',$input,-1,PREG_SPLIT_NO_EMPTY);
$hh='';$mm='';
list($hh,$mm)=explode(':',$input);
if($format=='12'){
	if($hh>12)
		if($hh{1}>0) $l[0]='0';
		else $l[1]='1';
	if($hh<1)
		$l[1]='1';
}else
	if($hh>23) $l[0]='0';
if($mm>59)
	$l[3]='0';
fwrite(STDOUT,implode('',$l));
?>


<?php
$format=trim(fgets(STDIN));
$input=trim(fgets(STDIN));
$l=preg_split('//',$input,-1,PREG_SPLIT_NO_EMPTY);
$hh='';$mm='';
list($hh,$mm)=explode(':',$input);
if($format=='12'){
	if($hh>12)
		if($hh{0}>1) $l[0]='0';
		else $l[1]='1';
	if($hh<1)
		$l[1]='1';
}else
	if($hh>23) $l[0]='0';
if($mm>59)
	$l[3]='0';
fwrite(STDOUT,implode('',$l));
?>


<?php
$format=trim(fgets(STDIN));
$input=trim(fgets(STDIN));
$l=preg_split('//',$input,-1,PREG_SPLIT_NO_EMPTY);
$hh='';$mm='';
list($hh,$mm)=explode(':',$input);
if($format=='12'){
	if($hh>12)
		if($hh{1}>0) $l[0]='0';
		else $l[1]='1';
}else
	if($hh>23) $l[0]='0';
if($mm>59)
	$l[3]='0';
fwrite(STDOUT,implode('',$l));
?>


<?php
$input = fopen("php://stdin", 'r');
$data = stream_get_contents($input);

$data_arr = explode(PHP_EOL,$data);
$time_format = $data_arr[0];
$time = $data_arr[1];
$time_arr = explode(':',$time);
$hours = $time_arr[0];
$minutes = $time_arr[1];

function correct_hours()
{
    global $hours;
    global $time_format;
    $h_1 = $hours[0];
    $h_2 = $hours[1];
    if ($time_format==12)
    {
        if ($h_2==0) {
            if ($h_1!==1) {
                $h_1 = 1;
            }
        }
        else {
            if (!in_array($h_1,[0,1])) {
                $h_1 = 0;
            }
            if ($h_1==1) {
                if (!in_array($h_2,[0,1,2]))
                {
                    $h_2 = 0;
                }
            }
        }
    }
    else {
        if ($h_1 > 2) {
            $h_1 = 1;
        }
        elseif ($h_1==2) {
            if ($h_2 > 3) {
                $h_2 = 2;
            }
        }
    }
    return $h_1.$h_2;
}

function correct_minutes()
{
    global $minutes;
    $m_1 = $minutes[0];
    $m_2 = $minutes[1];

    if ($m_1 > 5) {
        $m_1 = 3;
    }
    return $m_1.$m_2;
}

$out_hours = correct_hours();
$out_minutes = correct_minutes();
echo $out_hours.':'.$out_minutes;


<?php

###### IO ######
error_reporting(E_ALL);
error_reporting(0);

function readString($f) {
	$r = array(
		"\r" => '',
		"\n" => '',
	);
	return strtr(fgets($f), $r);
}

function readNumString($f) {
	return trim(fgets($f));
}

function stringToArray($str) {
	return str_split($str);
}

function readNum($f, $is_int = false) {
	$v = readNumString($f);
	return $is_int ? intval($v) : floatval($v);
}

function readStringExplode($f, $delimiter = ' ') {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = 'trim';
	return array_map($callf, $return);
}

function readStringExplodeNum($f, $delimiter = ' ', $is_int = false) {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = $is_int ? 'intval' : 'floatval';
	return array_map($callf, $return);
}

###### /IO ######

function diffCnt($baseTime, $time) {
	$return = 0;
	for ($i = 0; $i < 5; $i++) {
		if ($baseTime[$i] != $time[$i]) {
			$return++;
		}
	}
	return $return;
}

function getAnswer($is_test = false, $f = false) {
	$stream = STDIN;
	if ($is_test)
		$stream = $f;
	/* begin body */
	$format = readNum($stream);
	$baseTime = readString($stream);
	$answer = array(
		'res' => '',
		'diff' => 55,
	);
	$minHour = 0;
	$maxHour = 23;
	if ($format == 12) {
		$minHour = 1;
		$maxHour = 12;
	}
	for ($hour = $minHour; $hour < $maxHour; $hour++) {
		for ($minute = 0; $minute < 60; $minute++) {
			$checkHour = $hour;
			while (strlen($checkHour) < 2)
				$checkHour = '0' . $checkHour;
			$checkMinute = $minute;
			while (strlen($checkMinute) < 2)
				$checkMinute = '0' . $checkMinute;
			$checkTime = "{$checkHour}:{$checkMinute}";
			$diff = diffCnt($baseTime, $checkTime);
			if ($diff < $answer['diff']) {
				$answer['diff'] = $diff;
				$answer['res'] = $checkTime;
			}
		}
	}
	echo $answer['res'];
	/* / end body */
}

if (empty($is_test))
	getAnswer();


<?php

###### IO ######
error_reporting(E_ALL);
error_reporting(0);

function readString($f) {
	$r = array(
		"\r" => '',
		"\n" => '',
	);
	return strtr(fgets($f), $r);
}

function readNumString($f) {
	return trim(fgets($f));
}

function stringToArray($str) {
	return str_split($str);
}

function readNum($f, $is_int = false) {
	$v = readNumString($f);
	return $is_int ? intval($v) : floatval($v);
}

function readStringExplode($f, $delimiter = ' ') {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = 'trim';
	return array_map($callf, $return);
}

function readStringExplodeNum($f, $delimiter = ' ', $is_int = false) {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = $is_int ? 'intval' : 'floatval';
	return array_map($callf, $return);
}

###### /IO ######

function diffCnt($baseTime, $time) {
	$return = 0;
	for ($i = 0; $i < 5; $i++) {
		if ($baseTime[$i] != $time[$i]) {
			$return++;
		}
	}
	return $return;
}

function getAnswer($is_test = false, $f = false) {
	$stream = STDIN;
	if ($is_test)
		$stream = $f;
	/* begin body */
	$format = readNum($stream);
	$baseTime = readString($stream);
	$answer = array(
		'res' => '',
		'diff' => 55,
	);
	$minHour = 0;
	$maxHour = 23;
	if ($format == 12) {
		$minHour = 1;
		$maxHour = 12;
	}
	for ($hour = $minHour; $hour <= $maxHour; $hour++) {
		for ($minute = 0; $minute < 60; $minute++) {
			$checkHour = $hour;
			while (strlen($checkHour) < 2)
				$checkHour = '0' . $checkHour;
			$checkMinute = $minute;
			while (strlen($checkMinute) < 2)
				$checkMinute = '0' . $checkMinute;
			$checkTime = "{$checkHour}:{$checkMinute}";

			$diff = diffCnt($baseTime, $checkTime);
			if ($diff < $answer['diff']) {
				$answer['diff'] = $diff;
				$answer['res'] = $checkTime;
			}
		}
	}
	echo $answer['res'];
	/* / end body */
}

if (empty($is_test))
	getAnswer();


<?php

###### IO ######
error_reporting(E_ALL);
error_reporting(0);

function readString($f) {
	$r = array(
		"\r" => '',
		"\n" => '',
	);
	return strtr(fgets($f), $r);
}

function readNumString($f) {
	return trim(fgets($f));
}

function stringToArray($str) {
	return str_split($str);
}

function readNum($f, $is_int = false) {
	$v = readNumString($f);
	return $is_int ? intval($v) : floatval($v);
}

function readStringExplode($f, $delimiter = ' ') {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = 'trim';
	return array_map($callf, $return);
}

function readStringExplodeNum($f, $delimiter = ' ', $is_int = false) {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = $is_int ? 'intval' : 'floatval';
	return array_map($callf, $return);
}

###### /IO ######

function getAnswer($is_test = false, $f = false) {
	$stream = STDIN;
	if ($is_test)
		$stream = $f;
	/* begin body */
	$format = readNum($stream);
	$time = readString($stream);
	list($hour, $minute) = explode(':', $time);
	if ($minute >= 60) {
		$minute = '0' . ($minute % 10);
	}
	if ($format == 24) {
		if ($hour >= 24) {
			$hour = '0' . ($hour % 10);
		}
	} else {
		if ($hour > 12) {
			$hour = '0' . ($hour % 10);
		}
		if ($hour == '00') {
			$hour = '01';
		}
	}
	echo "{$hour}:{$minute}";
	/* / end body */
}

if (empty($is_test))
	getAnswer();


<?php

###### IO ######
error_reporting(E_ALL);
error_reporting(0);

function readString($f) {
	$r = array(
		"\r" => '',
		"\n" => '',
	);
	return strtr(fgets($f), $r);
}

function readNumString($f) {
	return trim(fgets($f));
}

function stringToArray($str) {
	return str_split($str);
}

function readNum($f, $is_int = false) {
	$v = readNumString($f);
	return $is_int ? intval($v) : floatval($v);
}

function readStringExplode($f, $delimiter = ' ') {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = 'trim';
	return array_map($callf, $return);
}

function readStringExplodeNum($f, $delimiter = ' ', $is_int = false) {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = $is_int ? 'intval' : 'floatval';
	return array_map($callf, $return);
}

###### /IO ######

function getAnswer($is_test = false, $f = false) {
	$stream = STDIN;
	if ($is_test)
		$stream = $f;
	/* begin body */
	$format = readNum($stream);
	$time = readString($stream);
	list($hour, $minute) = explode(':', $time);
	if ($minute >= 60) {
		$minute = '0' . ($minute % 10);
	}
	if ($format == 24) {
		if ($hour >= 24) {
			$hour = '0' . ($hour % 10);
		}
	} else {
		if ($hour >= 12) {
			$hour = '0' . ($hour % 10);
		}
		if ($hour == '00') {
			$hour = '01';
		}
	}
	echo "{$hour}:{$minute}";
	/* / end body */
}

if (empty($is_test))
	getAnswer();


<?php

###### IO ######
error_reporting(E_ALL);
error_reporting(0);

function readString($f) {
	$r = array(
		"\r" => '',
		"\n" => '',
	);
	return strtr(fgets($f), $r);
}

function readNumString($f) {
	return trim(fgets($f));
}

function stringToArray($str) {
	return str_split($str);
}

function readNum($f, $is_int = false) {
	$v = readNumString($f);
	return $is_int ? intval($v) : floatval($v);
}

function readStringExplode($f, $delimiter = ' ') {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = 'trim';
	return array_map($callf, $return);
}

function readStringExplodeNum($f, $delimiter = ' ', $is_int = false) {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = $is_int ? 'intval' : 'floatval';
	return array_map($callf, $return);
}

###### /IO ######

function getAnswer($is_test = false, $f = false) {
	$stream = STDIN;
	if ($is_test)
		$stream = $f;

	/* begin body */
	$type = readNum($stream);
	$baseTime = readString($stream);

    $clock = explode(":", $baseTime);
    $hour = $clock[0];
    $minutes = $clock[1];
    if ($minutes >= 60) {
        $minutes -= 60;
        $hour + 1;
    }
    if ($minutes < 10) {
        $minutes = '0' . $minutes;
    }
    if ($type == 12) {
        $hour %= $type;
        if ($hour < 10) {
            echo '0' . $hour . ":" . $minutes;
        }
    }
    if ($type == 24) {
        $hour %= $type;
        if ($hour < 10) {
            echo '0' . $hour . ":" . $minutes;
        }
    }
	/* / end body */
}

if (empty($is_test))
	getAnswer();


format = int(raw_input())

hours,mins = map(int, raw_input().split(':'))

if mins >= 60:
	mins -= 10*(mins/10)

if format == 12:
	if hours == 0:
		hours = 1
	if hours > 12:
		if hours % 10 == 0:
			hours = 10
		else:
			hours-= 10 * (hours / 10)
if format == 24:
	if hours > 23:
		hours-= 10 * (hours / 10)

print(('0' * (2 - len(str(hours)))) + str(hours) + ':' + ('0' * (2 - len(str(mins))))  + str(mins))


format = int(raw_input())

hours,mins = map(int, raw_input().split(':'))

if mins >= 60:
	mins -= 10*(mins/10)

if hours > format:
	hours-= 10 * (hours / 10)

print(('0' * (2 - len(str(hours)))) + str(hours) + ':' + ('0' * (2 - len(str(mins))))  + str(mins))


format = int(raw_input())

hours,mins = map(int, raw_input().split(':'))

if mins >= 60:
	mins -= 10*(mins/10)

if format == 12:
	if hours == 0:
		hours = 1
	if hours > 12:
		hours-= 10 * (hours / 10)
if format == 24:
	if hours > 23:
		hours-= 10 * (hours / 10)

print(('0' * (2 - len(str(hours)))) + str(hours) + ':' + ('0' * (2 - len(str(mins))))  + str(mins))


#map(int,raw_input().split())

n = int(raw_input())

def is_correct(x):
    a,b = map(int,x.split(":"))
    c= b >=0 and b <= 59
    if n == 24:
        return a >= 0 and a <= 23 and c
    elif n==12:
        return a>=1 and a <= 12 and c

def solve(s,ind):
    if ind == len(s):
        if is_correct(s): return [0,s]
        else: return [1000,s]
    if (s[ind] == ':'): return solve(s,ind+1)
    ans = solve(s,ind+1)
    for i in xrange(0,10):
        if str(i) != s[ind]:
            p = solve(s[:ind]+str(i) + s[ind+1:],ind+1)
            p[0] += 1
            if p < ans:
                ans = p
    return ans

s = raw_input()

print solve(s,0)[1]


time_format = int(raw_input())
s_hour, s_minute = raw_input().split(':')

res = 10**9
res_time = ""

if time_format == 12:
    lo_h = 1
    lo_m = 0
    hi_h = 12
    hi_m = 59
else:
    lo_h = 0
    lo_m = 0
    hi_h = 23
    hi_m = 59

def diff(a, b):
    return int(a[0] != b[0]) + int(a[1] != b[1])

for cur_h in xrange(0, 24):
    for cur_m in xrange(0, 60):
        if cur_h >= lo_h and cur_h <= hi_h and cur_m >= lo_m and cur_m <= hi_m:
            sh = str(cur_h).zfill(2)
            sm = str(cur_m).zfill(2)
            tmp = diff(s_hour, sh) + diff(s_minute, sm)
            if tmp < res:
                res = tmp
                res_time = "%s:%s" % (sh, sm)

print res_time


#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2016 vishalapr <vishalapr@vishal-Lenovo-G50-70>
#
# Distributed under terms of the MIT license.

"""

"""

n = input()
st = map(int,raw_input().split(':'))

if n == 24:
    if st[0] > 23:
        st[0] = st[0]%10
    if st[1] > 59:
        st[1] = st[1]%10
if n == 12:
    if st[0] < 1 or st[0] > 12:
        st[0] = st[0]%10;
        if st[0] == 0:
            st[0] += 10
    if st[1] > 59:
        st[1] = st[1]%10

print str(st[0]).zfill(2) + ":" + str(st[1]).zfill(2)


def hamming(s, t):
    a = 0
    for i in range(len(s)):
        if s[i] != t[i]:
            a += 1
    return a

def main():
    T = int(raw_input())
    S = raw_input()

    all_12s = set()
    for h in range(1, 13):
        for m in range(0, 60):
            hh = str(h).zfill(2)
            mm = str(m).zfill(2)
            #print hh, mm
            all_12s.add(hh + ':' + mm)

    all_24s = set()
    for h in range(24):
        for m in range(0, 60):
            hh = str(h).zfill(2)
            mm = str(m).zfill(2)
            #print hh, mm
            all_24s.add(hh + ':' + mm)

    if T == 12:
        best = 1000
        ans = ''
        for x in all_12s:
            if hamming(x, S) < best:
                ans = x
                best = hamming(x, S)

    if T == 24:
        best = 1000
        ans = ''
        for x in all_24s:
            if hamming(x, S) < best:
                ans = x
                best = hamming(x, S)

    print ans

main()


import itertools
from fractions import gcd
from math import sqrt
from bisect import bisect_right
#fast
def get(a):
	return map(a , raw_input().split())
def I():
	return int(raw_input())
def Str():
	return raw_input()
n = input()
h = Str()
ab = h.split(':')
ax = ab[0]
bx = ab[1]
pos = [[ax,bx] , ['0'+ax[1:] , '0'+bx[1:]] , ['0'+ax[1:] ,bx ] , [ax , '0'+bx[1:] ]  , ['1'+ax[1:] , '1'+bx[1:]] , ['1'+ax[1:] ,bx ] , [ax , '1'+bx[1:] ]]
cost = dict()
har = {0:0 , 1: 2 , 2:1 , 3:1 ,4:2 , 5:1 , 6:1}
#bc ek ek word se dikkat hoye he aj bad day today
j = 0
for i in pos:
    ax , bx = i[0] , i[1]
    fg = False
    if int(ax) >= 0 and int(ax) < 24 and int(bx) >= 0 and int(bx) < 60 and n == 24:
        fg = True
    if int(ax) >= 1 and int(ax) < 13 and int(bx) >= 0 and int(bx) < 60 and n == 12:
        fg = True
    if fg :
        if len(ax) == 1:
            ax = '0'+ax
        if len(bx) == 1:
            bx = '0'+bx
        cost[har[j]] = (ax , bx)
    j += 1
#print cost
ab = list(cost[min(cost.keys())])
print ':'.join(ab)


import itertools
from fractions import gcd
from math import sqrt
from bisect import bisect_right
#fast
def get(a):
	return map(a , raw_input().split())
def I():
	return int(raw_input())
def Str():
	return raw_input()
n = input()
h = Str()
ab = h.split(':')
ax = ab[0]
bx = ab[1]
pos = [[ax,bx] , ['0'+ax[1:] , '0'+bx[1:]] , ['0'+ax[1:] ,bx ] , [ax , '0'+bx[1:] ] ]
cost = dict()
j = 0
for i in pos:
    ax , bx = i[0] , i[1]
    fg = False
    if int(ax) >= 0 and int(ax) < 24 and int(bx) >= 0 and int(bx) < 60 and n == 24:
        fg = True
    if int(ax) >= 1 and int(ax) < 13 and int(bx) >= 0 and int(bx) < 60 and n == 12:
        fg = True
    if j == 0 and fg:
        cost[0] = (ax, bx)
    elif j == 1 and fg:
        if len(ax) == 1:
            ax = '0'+ax
        if len(bx) == 1:
            bx = '0'+bx
        cost[2] = (ax , bx)
    elif fg and (j == 2 or j == 3) :
        #print ax , bx
        if len(ax) == 1:
            ax = '0'+ax
        if len(bx) == 1:
            bx = '0'+bx
        cost[1] = (ax , bx)
    j += 1
#print cost
ab = list(cost[min(cost.keys())])
print ':'.join(ab)


import itertools
from fractions import gcd
from math import sqrt
from bisect import bisect_right
#fast
def get(a):
	return map(a , raw_input().split())
def I():
	return int(raw_input())
def Str():
	return raw_input()
n = input()
h = Str()
ab = h.split(':')
ax = ab[0]
bx = ab[1]
pos = [[ax,bx] , ['0'+ax[1:] , '0'+bx[1:]] , ['0'+ax[1:] ,bx ] , [ax , '0'+bx[1:] ] ]
cost = dict()
j = 0
for i in pos:
    ax , bx = i[0] , i[1]
    fg = False
    if int(ax) >= 0 and int(ax) < n and int(bx) >= 0 and int(bx) < 60:
        fg = True
    if j == 0 and fg:
        cost[0] = (ax, bx)
    elif j == 1 and fg:
        if len(ax) == 1:
            ax = '0'+ax
        if len(bx) == 1:
            bx = '0'+bx
        cost[2] = (ax , bx)
    elif fg and (j == 2 or j == 3) :
        #print ax , bx
        if len(ax) == 1:
            ax = '0'+ax
        if len(bx) == 1:
            bx = '0'+bx
        cost[1] = (ax , bx)
    j += 1
#print cost
ab = list(cost[min(cost.keys())])
print ':'.join(ab)


import itertools
from fractions import gcd
from math import sqrt
from bisect import bisect_right
#fast
def get(a):
	return map(a , raw_input().split())
def I():
	return int(raw_input())
def Str():
	return raw_input()
n = input()
h = Str()
ab = h.split(':')
ax = ab[0]
bx = ab[1]
if int(ab[0]) > n:
    ax = '0'+ab[0][1]
if int(ab[1]) >= 60:
    bx = '0'+ ab[1][1]
print ':'.join([ax,bx])


time_format = input()
hours, minutes = input().split(':')
if time_format == '12':
    if not 1 <= int(hours) <= 12:
        if hours[1] != '0':
            hours = '0' + hours[1]
        else:
            hours = '10'
if time_format == '24':
    if not 0 <= int(hours) <= 23:
        hours = '1' + hours[1]
if not 0 <= int(minutes) <= 59:
    minutes = '1' + str(minutes)[1]
print(':'.join([hours, minutes]))


def correct_hour(is24, a, b):
    if is24:
        if a > 2:
            a = 0
        elif a == 2 and b > 3:
            b = 0
    else:
        if a == b == 0:
            b = 1
        elif a > 1:
            if b == 0:
                a = 1
            else:
                a = 0
        elif a == 1 and b > 2:
            b = 0
    return (a, b)

def correct_minutes(c, d):
    if c > 6:
        c = 0
    return (c, d)

if __name__ == '__main__':
    is24 = input() == '24'
    a, b, _, c, d = input()
    a, b = correct_hour(is24, int(a), int(b))
    c, d = correct_minutes(int(c), int(d))
    print(f'{a}{b}:{c}{d}')


def correct_hour(is24, a, b):
    if is24:
        if a > 2:
            a = 0
        elif a == 2 and b > 3:
            b = 0
    else:
        if a == b == 0:
            b = 1
        elif a > 1:
            if b == 0:
                a = 1
            else:
                a = 0
        elif a == 1 and b > 2:
            b = 0
    return (a, b)

def correct_minutes(c, d):
    if c > 5:
        c = 0
    return (c, d)

if __name__ == '__main__':
    is24 = input() == '24'
    a, b, _, c, d = input()
    a, b = correct_hour(is24, int(a), int(b))
    c, d = correct_minutes(int(c), int(d))
    print(f'{a}{b}:{c}{d}')


format = int(input())
h, m = map(int, input().split(':'))
if m > 59:
    m = int(str(0) + str(m % 10))
if format == 12:
    if h > 12:
        h = int(str(0) + str(h % 10))
    elif h == 0:
        h = 1
elif format == 24:
    if h > 23:
        h = int(str(0) + str(h % 10))
print(str(h // 10) + str(h % 10) + ":" + str(m // 10) + str(m % 10))


class CodeforcesTask722ASolution:
    def __init__(self):
        self.result = ''
        self.format = 0
        self.hour = ''

    def read_input(self):
        self.format = int(input())
        self.hour = input()

    def process_task(self):
        valid_minutes = [x for x in range(60)]
        valid_hours_12 = [x for x in range(1, 13)]
        valid_hours_24 = [x for x in range(24)]
        out_hour = list(self.hour)
        if self.format == 12:
            if int(self.hour[:2]) not in valid_hours_12:
                if self.hour[1] != "0":
                    out_hour[0] = "0"
                else:
                    out_hour[1] = "1"
        else:
            if int(self.hour[:2]) not in valid_hours_24:
                out_hour[0] = "1"
        if int(self.hour[3:]) not in valid_minutes:
            out_hour[3] = "1"
        self.result = "".join(out_hour)

    def get_result(self):
        return self.result

if __name__ == "__main__":
    Solution = CodeforcesTask722ASolution()
    Solution.read_input()
    Solution.process_task()
    print(Solution.get_result())


class CodeforcesTask722ASolution:
    def __init__(self):
        self.result = ''
        self.format = 0
        self.hour = ''

    def read_input(self):
        self.format = int(input())
        self.hour = input()

    def process_task(self):
        valid_minutes = [x for x in range(60)]
        valid_hours_12 = [x for x in range(1, 13)]
        valid_hours_24 = [x for x in range(24)]
        out_hour = list(self.hour)
        if self.format == 12:
            if int(self.hour[:2]) not in valid_hours_12:

                if self.hour[1] != "0":
                    out_hour[0] = "0"
                else:
                    if self.hour[0] not in ["0", "1"]:
                        out_hour[0] = "1"
                    else:
                        out_hour[1] = "1"
        else:
            if int(self.hour[:2]) not in valid_hours_24:
                out_hour[0] = "1"
        if int(self.hour[3:]) not in valid_minutes:
            out_hour[3] = "1"
        self.result = "".join(out_hour)

    def get_result(self):
        return self.result

if __name__ == "__main__":
    Solution = CodeforcesTask722ASolution()
    Solution.read_input()
    Solution.process_task()
    print(Solution.get_result())