Problem:
Problem A: Membership Management

Peter is a senior manager of Agile Change Management (ACM) Inc., where each employee is a member of one or more task groups. Since ACM is agile, task groups are often reorganized and their members frequently change, so membership management is his constant headache.

Peter updates the membership information whenever any changes occur: for instance, the following line written by him means that Carol and Alice are the members of the Design Group.

design:carol,alice.

The name preceding the colon is the group name and the names following it specify its members.

A smaller task group may be included in a larger one. So, a group name can appear as a member of another group, for instance, as follows.

development:alice,bob,design,eve.

Simply unfolding the design above gives the following membership specification, which is equivalent to the original.

development:alice,bob,carol,alice,eve.

In this case, however, alice occurs twice. After removing one of the duplicates, we have the following more concise specification.

development:alice,bob,carol,eve.

Your mission in this problem is to write a program that, given group specifications, identifies group members.

Note that Peter's specifications can include deeply nested groups. In the following, for instance, the group one contains a single member dave.

one:another.
another:yetanother.
yetanother:dave.

Input

The input is a sequence of datasets, each being in the following format.

n

group1:member1,1,...,member1,m1.

.

.

.

groupi:memberi,1,...,memberi,mi.

.

.

.

groupn:membern,1,...,membern,mn.

The first line contains n, which represents the number of groups and is a positive integer no more than 100. Each of the following n lines contains the membership information of a group: groupi (1 ≤ i ≤ n) is the name of the i-th task group and is followed by a colon (:) and then the list of its mi member s that are delimited by a comma (,) and terminated by a period (.).

Those group names are mutually different. Each mi (1 ≤ i ≤ n) is between 1 and 10, inclusive. A member is another group name if it is one of group1, group2,..., or groupn. Otherwise it is an employee name.

There are no circular (or recursive) definitions of group(s). You may assume that mi member names of a group are mutually different.

Each group or employee name is a non-empty character string of length between 1 and 15, inclusive, and consists of lowercase letters.

The end of the input is indicated by a line containing a zero.

Output

For each dataset, output the number of employees included in the first group of the dataset, that is group1, in a line. No extra characters should occur in the output.

Sample Input

2
development:alice,bob,design,eve.
design:carol,alice.
3
one:another.
another:yetanother.
yetanother:dave.
3
friends:alice,bob,bestfriends,carol,fran,badcompany.
bestfriends:eve,alice.
badcompany:dave,carol.
5
a:b,c,d,e.
b:c,d,e,f.
c:d,e,f,g.
d:e,f,g,h.
e:f,g,h,i.
4
aa:bb.
cc:dd,ee.
ff:gg.
bb:cc.
0

Output for the Sample Input

4
1
6
4
2

Solution in C (judge verdict: Time Limit Exceeded)

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define TRUE 1
#define WHITE 0
#define BLACK 1

int maxlength();

int main(void) {
	int i,j,k;
	char black[6] = "Black";
	char white[6] = "White";
	char draw[6] = "Draw";
	char winner[6] = "\0";
	int turn; 
	int n; 
	int m; 
	int p; 
	int field[8][8][8]; 
	int bmax; 
	int wmax; 
	int max;
	int x,y;
	int instant;
	
	while(TRUE) {
		scanf("%d %d %d", &n, &m, &p);
		//printf(">> %d %d %d\n",n,m,p);
		if(n == 0 && m == 0 && p == 0) break;
		
		for(i=0;i<8;i++) {
			for(j=0;j<8;j++) {
				for(k=0;k<8;k++) {
					field[i][j][k] = -1;
				}
			}
		}
		bmax = 0;
		wmax = 0;
		max = 0;
		for(turn = 1;turn <= p; turn++) {
			scanf("%d %d", &x, &y);
			
			for(i=0;i<8;i++) {
				if(field[x][y][i] == -1) {
					field[x][y][i] = turn % 2;
					break;
				}
			}
			instant = maxlength(field, turn, x, y, i, n);
			if(turn % 2 == BLACK && instant > bmax) {
				bmax = instant;
			}
			if(turn % 2 == WHITE && instant > wmax) {
				wmax = instant;
			}
			if(max < instant) max = instant;
			if(max == m) {
				for(j=turn+1;j <=p;j++) scanf("%d %d", &x, &y);
				break;
			}
			if(turn == p) break;
		}
		if(bmax > wmax) strcpy(winner, black);
		else if(wmax > bmax) strcpy(winner, white);
		else strcpy(winner, draw);
		
		printf("%s", winner);
		if(strcmp(draw,winner) != 0) printf(" %d", turn);
		printf("\n");
	}
	
	exit(0);
}

int maxlength(int field[][8][8],int turn,int x,int y,int z,int n) {
	//printf("[%d][%d][%d]\n",x,y,z);
	int i,j;
	int max=0;
	int instant[13];
	int out[26];
	int sum=0;
	
	for(i=0;i<13;i++) {
		instant[i] = 1;
	}
	for(i=0;i<26;i++) out[i] = 0;
	
	for(i=1;;i++) {
		//x????????????
		if(y+i <= n && z+i <= n && field[x][y+i][z+i] == turn % 2) instant[0]++;
		else out[0] = -1;
		if(y-i >= 1 && z-i >= 1 && field[x][y-i][z-i] == turn % 2) instant[0]++;
		else out[1] = -1;
		
		if(y+i <= n && z-i >= 1 && field[x][y+i][z-i] == turn % 2) instant[1]++;
		else out[2] = -1;
		if(y-i >= 1 && z+i <= n && field[x][y-i][z+i] == turn % 2) instant[1]++;
		else out[3] = -1;
		//y????????????
		if(x+i <= n && z+i <= n && field[x+i][y][z+i] == turn % 2) instant[2]++;
		else out[4] = -1;
		if(x-i >= 1 && z-i >= 1 && field[x-i][y][z-i] == turn % 2) instant[2]++;
		else out[5] = -1;
		
		if(x+i <= n && z-i >= 1 && field[x+i][y][z-i] == turn % 2) instant[3]++;
		else out[6] = -1;
		if(x-i >= 1 && z+i <= n && field[x-i][y][z+i] == turn % 2) instant[3]++;
		else out[7] = -1;
		//z????????????
		if(y+i <= n && x+i <= n && field[x+i][y+i][z] == turn % 2) instant[4]++;
		else out[8] = -1;
		if(y-i >= 1 && x-i >= 1 && field[x-i][y-i][z] == turn % 2) instant[4]++;
		else out[9] = -1;
		
		if(y+i <= n && x-i >= 1 && field[x-i][y+i][z] == turn % 2) instant[5]++;
		else out[10] = -1;
		if(y-i >= 1 && x+i <= n && field[x+i][y-i][z] == turn % 2) instant[5]++;
		else out[11] = -1;
		//x??¨y?????????
		if(z+i <= n && field[x][y][z+i] == turn % 2) instant[6]++;
		else out[12] = -1;
		
		if(z-i >= 1 && field[x][y][z-i] == turn % 2) instant[6]++;
		else out[13] = -1;
		//x??¨z?????????
		if(y+i <= n && field[x][y+i][z] == turn % 2) instant[7]++;
		else out[14] = -1;
		
		if(y-i >= 1 && field[x][y-i][z] == turn % 2) instant[7]++;
		else out[15] = -1;
		//y??¨z?????????
		if(x+i <= n && field[x+i][y][z] == turn % 2) instant[8]++;
		else out[16] = -1;
		
		if(x-i >= 1 && field[x-i][y][z] == turn % 2) instant[8]++;
		else out[17] = -1;
		
		//????????????
		//x+i,y+i,z+i 
		if(x+i <= n && y+i <= n && z+i <= n && field[x+i][y+i][z+i] == turn % 2) instant[9]++;
		else out[18] = -1;
		if(x-i >= 1 && y-i >= 1 && z-i >= 1 && field[x-i][y-i][z-i] == turn % 2) instant[9]++;
		else out[19] = -1;
		
		if(x-i >= 1 && y+i <= n && z+i <= n && field[x-i][y+i][z+i] == turn % 2) instant[10]++;
		else out[20] = -1;
		if(x+i <= n && y-i >= 1 && z-i >= 1 && field[x+i][y-i][z-i] == turn % 2) instant[10]++;
		else out[21] = -1;
		
		if(x+i <= n && y-i >= 1 && z+i <= n && field[x+i][y-i][z+i] == turn % 2) instant[11]++;
		else out[22] = -1;
		if(x-i >= 1 && y+i <= n && z-i >= 1 && field[x-i][y+i][z-i] == turn % 2) instant[11]++;
		else out[23] = -1;
		
		if(x+i <= n && y+i <= n && z-i >= n && field[x+i][y+i][z-i] == turn % 2) instant[12]++;
		else out[24] = -1;
		if(x-i >= 1 && y-i >= 1 && z+i <= n && field[x-i][y-i][z+i] == turn % 2) instant[12]++;
		else out[25] = -1;
		
		sum = 0;
		for(j=0;j<26;j++) {
			sum+=out[j];
			//if(sum==-18)printf("%d\n",sum);
		}
		if(sum <= -26) break;
	}
	
	for(i=0;i<13;i++) {
		if(max < instant[i]) max = instant[i];
	}
	return max;
}