#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
#define CRP(t, x) const t &x
#define OPL(t, x) bool operator<(CRP(t, x)) const
#define FIL(x, v) memset(x, v, sizeof(x))
#define CLR(x) FIL(x, 0)
#define NE1(x) FIL(x, -1)
#define INF(x) FIL(x, 0x3f)
typedef long long ll, i64;
const int N = 1e5 + 50;
char buf[12];
int a[N][12];
int mp[10];
int dayOfWeek[10000][13][32];
const int monthDay[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool check(int i)
{
    auto p = a[i];
    int y = mp[p[0]] * 1000 + mp[p[1]] * 100 + mp[p[2]] * 10 + mp[p[3]];
    int m = mp[p[4]] * 10 + mp[p[5]];
    int d = mp[p[6]] * 10 + mp[p[7]];
    return y >= 1600 && m <= 12 && d <= 31 && dayOfWeek[y][m][d] == 5;
}
inline unsigned long long hs(char *s)
{
    unsigned long long r = 1;
    while (*s) r = r * 131 + *s++;
    return r;
}
int main()
{
    mt19937_64 mt(time(0));
    for (int y = 1600, n = 6; y < 10000; y++)
        for (int m = 1; m <= 12; m++)
        {
            int dayNum = monthDay[m] + ((m == 2) && (y % 400 == 0 || (y % 100 != 0 && y % 4 == 0)));
            for (int d = 1; d <= dayNum; ++d)
            {
                dayOfWeek[y][m][d] = n++;
                if (n >= 7) n -= 7;
            }
        }
    int T, n;
    scanf("%d", &T);
    set<unsigned long long> S;
    for (int t = 1; t <= T; t++)
    {
        S.clear();
        scanf("%d", &n);
        int cnt = 0;
        for (int i = 0; i < n; i++)
        {
            scanf("%s", buf);
            auto h = hs(buf);
            if (S.count(h)) continue;
            S.insert(h);
            auto p = a[cnt++];
            p[0] = buf[0] - 'A';
            p[1] = buf[1] - 'A';
            p[2] = buf[2] - 'A';
            p[3] = buf[3] - 'A';
            p[4] = buf[5] - 'A';
            p[5] = buf[6] - 'A';
            p[6] = buf[8] - 'A';
            p[7] = buf[9] - 'A';
        }
        n = cnt;
        for (int i = 0; i < 10; i++) mp[i] = i;
        bool flag = true;
        do
        {
            flag = true;
            for (int i = 0; i < 10 && flag; i++)
                flag &= check(mt() % n);
            for (int i = 0; i < n && flag; i++)
                flag &= check(i);
            if (flag) break;
        } while (next_permutation(mp, mp + 10));
        if (flag)
            for (int i = buf[10] = 0; i < 10; i++) buf[i] = mp[i] + '0';
        else
            strcpy(buf, "Impossible");
        printf("Case #%d: %s\n", t, buf);
    }
    return 0;
}