/*
Problem: 	F: Statistical trouble
Description:	print the table of survey
Class:		text
Subclass:	formating
Algorithm:	direct O(input+output)
Author:		Josef Zlomek
Date:		Mar 10, 2002
*/

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

#define E 0.0000000001

char survey[256];

struct question {
	union {
		char code[4];
		unsigned c;
	};
	char q[100];
	int na;
	struct answer {
		char a[48];
		char code;
	} a[10];
} q[100];
int nq;
int lq;
int nr;
char r[10000][104];

int num[12][12];
double pfh[12][12];
int pih[12][12];
double pfv[12][12];
int piv[12][12];

int t1[128],t2[128];

int main()
{
	char line[256];

	gets(survey);
	nq=0;
	lq=-1;

	//for(;;)
	{
		gets(line);
		//if(line[0]=='#') break;

		if(line[0]==' ')
		{
			q[lq].a[q[lq].na].code = line[1];
			strcpy(q[lq].a[q[lq].na].a, line+3);
			q[lq].na++;
		} else {
			lq=nq;
			nq++;
			line[3]=0;
			memcpy(q[lq].code,line,4);
			strcpy(q[lq].q,line+4);
			q[lq].na=0;
		}
	}
	nr=0;
	for(;;)
	{
		gets(r[nr]);
		if(r[nr][0]=='#') break;
		nr++;
	}
	for(;;)
	{
		int q1,q2;
		int i1,i2;
		int i,j,k;

		gets(line);
		if(line[0]=='#') break;
		line[3]=0;
		line[7]=0;
		q1=((unsigned*)line)[0];
		q2=((unsigned*)line)[1];
		for(i=0;i<nq;i++)
		{
			if(q[i].c==q1) i1=i;
			if(q[i].c==q2) i2=i;
		}

		printf ("%s - %s\n", survey, line+8);
		printf ("%s %s\n", q[i1].code, q[i1].q);
		for(i=0;i<q[i1].na;i++)
			printf(" %c %s\n", q[i1].a[i].code, q[i1].a[i].a);
		printf ("%s %s\n", q[i2].code, q[i2].q);
		for(i=0;i<q[i2].na;i++)
			printf(" %c %s\n", q[i2].a[i].code, q[i2].a[i].a);
		printf("\n");

		for(i=0;i<=q[i1].na;i++)
			for(j=0;j<=q[i2].na;j++)
				num[i][j]=0;

		/* create translation table (code -> index)  */
		for(i=0;i<128;i++)
		{
			t1[i]=q[i1].na;
			t2[i]=q[i2].na;
		}
		for(i=0;i<q[i1].na;i++)
			t1[q[i1].a[i].code]=i;
		for(i=0;i<q[i2].na;i++)
			t2[q[i2].a[i].code]=i;

		for(k=0;k<nr;k++)
			num[t1[r[k][i1]]][t2[r[k][i2]]]++;

		/* sum the numbers*/
		for(i=0;i<q[i1].na;i++)
		{
			num[i][q[i2].na]=0;
			for(j=0;j<q[i2].na;j++)
				num[i][q[i2].na]+=num[i][j];
		}
		for(i=0;i<=q[i2].na;i++)
		{
			num[q[i1].na][i]=0;
			for(j=0;j<q[i1].na;j++)
				num[q[i1].na][i]+=num[j][i];
		}

		/* compute the per cents */
		for(i=0;i<=q[i1].na;i++)
			if(num[i][q[i2].na])
			{
				for(j=0;j<q[i2].na;j++)
					pfh[i][j]=100 * (double)num[i][j] / num[i][q[i2].na];
				pfh[i][q[i2].na]=100;
				pih[i][q[i2].na]=100;
			}
			for(j=0;j<=q[i2].na;j++)
				if(num[q[i1].na][j])
				{
					for(i=0;i<q[i1].na;i++)
						pfv[i][j]=100 * (double)num[i][j] / num[q[i1].na][j];
					pfv[q[i1].na][j]=100;
					piv[q[i1].na][j]=100;
				}

				/* round the per cents */
				for(i=0;i<=q[i1].na;i++)
					if(num[i][q[i2].na])
					{
						k=0;
						for(j=0;j<q[i2].na;j++)
						{
							pih[i][j]=pfh[i][j];
							k+=pih[i][j];
						}
						for(j=0;k<100 && j<q[i2].na;j++)
							if((pfh[i][j] - (double)pih[i][j]) > E)
							{
								pih[i][j]++;
								k++;
							}
					}
					for(j=0;j<=q[i2].na;j++)
						if(num[q[i1].na][j])
						{
							k=0;
							for(i=0;i<q[i1].na;i++)
							{
								piv[i][j]=pfv[i][j];
								k+=piv[i][j];
							}
							for(i=0;k<100 && i<q[i1].na;i++)
								if((pfv[i][j] - (double)piv[i][j]) > E)
								{
									piv[i][j]++;
									k++;
								}
						}

						/* print the table */
						printf ("      ");
						for(j=0;j<q[i2].na;j++)
							printf (" %s:%c", q[i2].code, q[i2].a[j].code);
						printf (" TOTAL\n");

						for(i=0;i<=q[i1].na;i++)
						{
							if (i<q[i1].na)
								printf (" %s:%c", q[i1].code, q[i1].a[i].code);
							else
								printf (" TOTAL");

							for(j=0;j<=q[i2].na;j++)
								printf("%6d",num[i][j]);
							printf("\n");

							printf ("      ");
							if(num[i][q[i2].na])
								for(j=0;j<=q[i2].na;j++)
									printf("%5d%%", pih[i][j]);
							else
								for(j=0;j<=q[i2].na;j++)
									printf("     -");
							printf("\n");

							printf ("      ");
							for(j=0;j<=q[i2].na;j++)
								if(num[q[i1].na][j])
									printf("%5d%%", piv[i][j]);
								else
									printf("     -");
							printf("\n");
						}
						printf("\n");
	}

	return 0;
}
