//* Problem  : C - Compromise
//* Contest  : UzhNU Local
//* Origin   : Ulm-1997
//* Type     : Judge
//* Date     : 2008.02.04 - 2008.05.31
//* Author   : alt
//* Language : C++
//* Compiler : Microsoft Visual C++ 6.0

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include "testlib.h"

using namespace std;

int X[128], Y[128], na, nb, R[128], nr;

int c[128][128], b[128][128];

map <string, int> mp;

int buildLCS(int *X, int n, int *Y, int m)
{
	int i, j;
	for (i = 1; i <= n; i++)
		c[i][0] = 0;
	for (j = 0; j <= m; j++)
		c[0][j] = 0;
	
	for (i = 1; i <= n; i++)
		for (j = 1; j <= m; j++)
		{
			if (X[i - 1] == Y[j - 1]) 
			{
				c[i][j] = c[i - 1][j - 1] + 1;
				b[i][j] = 1;
			}
			else if (c[i - 1][j] >= c[i][j - 1]) 
			{
				c[i][j] = c[i - 1][j];
				b[i][j] = 2;
			}
			else
			{
				c[i][j] = c[i][j - 1];
				b[i][j] = 3;
			}
		}
		return c[n][m];
}

int is_subsequence(int *a, int na, int *b, int nb)
{
	int i = 0, j = 0;
	while (j < nb)
	{
		while (i < na && a[i] != b[j]) i++;
		if (i >= na) return 0;
		i++; j++;
	}
	return 1;
}

int main(int argc, char *argv[])
{
	registerTestlibCmd(argc, argv);
	///
	int it = 0, nt = 0, imp;
	while (!ans.eof())
	{
		string s = ans.readString();
		if (s == "") break;
		nt++;
	}
	while (1)
	{
		it++; na = nb = imp = 0;
		mp.clear();
		string t;
		while (1)
		{
			t = inf.readWord(); if (t == "#") break;
			na++; if (!mp[t]) mp[t] = ++imp; X[na++] = mp[t];
		}
		while (1)
		{
			t = inf.readWord(); if (t == "#") break;
			if (!mp[t]) mp[t] = ++imp; Y[nb++] = mp[t];
		}
		int res = buildLCS(X, na, Y, nb);
		nr = 0;
		while (!ouf.eoln())
		{
			t = ouf.readWord();
			if (!mp[t])
				quitf(_wa, "Test #%d. Unknown word: '%s'", it, t.c_str());
			R[nr++] = mp[t];
			if (nr > res)
				quitf(_wa, "Test #%d. Answer too long", it);
		}
		if (nr != res)
			quitf(_wa, "Test #%d. Different length of answer. Expected: %d, received: %d", it, res, nr);
//		ouf.skipChar();
		if (!is_subsequence(X, na, R, nr))
			quitf(_wa, "Test #%d. Answer is not a subsequence of first sequence", it);
		if (!is_subsequence(Y, nb, R, nr))
			quitf(_wa, "Test #%d. Answer is not a subsequence of second sequence", it);
		if (it == nt) break;
	}
	quitf(_ok, "%d tests passed", it);
}
