#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ll long long
#define pii pair<int,int>
#define tii tuple <int,int,int>
#define N 100005
#define mod 2000003
#define X first
#define Y second
#define eps 0.0000000001
#define all(x) x.begin(),x.end()
#define tot(x) x+1,x+n+1
using namespace std;
map<char, int>M;
int n, m, i, j, k, l, x, y, dif;
char sol;
string s;
int main() {
    cin.sync_with_stdio(0);
    cout.sync_with_stdio(0);

    for(i = '0'; i <= '9'; i++)
        M[i] = i - '0';

    M['A'] = 10;
    M['B'] = 11;
    M['C'] = 12;
    M['D'] = 13;
    M['E'] = 14;
    M['F'] = 15;
    cin >> n >> m;

    for(i = 1; i <= n; i++) {
        for(j = 1; j <= m; j++) {
            cin >> s;
            cout << '#';
            s.erase(s.begin());

            for(l = 0; l < 3; l++) {
                dif = 100000;
                x = M[s[l * 2]] * 16 + M[s[l * 2 + 1]];

                for(k = 0; k <= 9; k++) {
                    y = M[k + '0'] * 17;

                    if(dif > abs(x - y)) {
                        dif = abs(x - y);
                        sol = k + '0';
                    }
                }

                for(k = 'A'; k <= 'F'; k++) {
                    y = M[k] * 17;

                    if(dif > abs(x - y)) {
                        dif = abs(x - y);
                        sol = k ;
                    }
                }

                cout << sol;
            }

            cout << ' ';
        }

        cout << '\n';
    }

    return 0;
}