#define _CRT_DISABLE_PERFCRIT_LOCKS
#define _CRT_SECURE_NO_WARNINGS

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

#if _DLL || __MINGW32__
  #define getc _getc_nolock
  #define putc _putc_nolock
#endif

int main(int argc, char **argv)
{
    int n, m, c, i;
    FILE *in = stdin, *out = stdout;
    char *p, *field[64];
    char buf[32768];

    if (!freopen("list.txt", "rb", in))
        return -1;
    if (!freopen("nodup.txt", "wb", out))
        return -1;

    setvbuf(in, NULL, _IOFBF, 16*1024);
    setvbuf(out, NULL, _IOFBF, 16*1024);

    field[i = 0] = p = buf;

    while ((c = getc(in)) != EOF) {
        if (c == ';') {
            *p++ = 0;
            if (i < sizeof(field)/sizeof(*field) - 1)
                field[++i] = p;
        }
        else if (c != '\n') {
            if (p < buf + sizeof(buf) - 2)
                *p++ = c;
        }
        else {
            *p = 0;

            for (n = 3; n < i - 1; n++) {
                if (field[n][0]) {
                    for (m = n + 1; m < i; m++) {
                        if (strcmp(field[n], field[m]) == 0)
                            field[m][0] = 0;
                    }
                }
            }

            for (n = 0; n < i; n++) {
#if _MSC_VER
                // fputs without multithreaded locks
                p = field[n];
                _fwrite_nolock(p, 1, strlen(p), out);
                putc(';', out);
#else
                fputs(field[n], out);
                putc(';', out);
#endif
            }

            putc('\n', out);

            field[i = 0] = p = buf;
        }
    }
}
