#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info{
	bool operator<(const struct Info &arg) const{
		if(weekday != arg.weekday){
			return weekday < arg.weekday;
		}else{
			return start < arg.start;
		}
	}
	int weekday,start,end;
	char name[33];
};

int N;
Info info[500];
bool check[7][1440];
bool used[500];

bool strCmp(char* base, char* comp){
	int length1,length2;
	for(length1=0;base[length1] != '\0';length1++);
	for(length2=0;comp[length2] != '\0';length2++);
	if(length1 != length2)return false;

	for(int i=0;base[i] != '\0'; i++){
		if(base[i] != comp[i])return false;
	}
	return true;
}

int translate_TIME(char buf[5]){
	int length;
	for(length = 0; buf[length] != '\0'; length++);

	int hour,minute;

	if(length == 3){
		hour = buf[0]-'0';
		minute = 10*(buf[1]-'0')+(buf[2]-'0');
	}else{
		hour = 10*(buf[0]-'0')+(buf[1]-'0');
		minute = 10*(buf[2]-'0')+(buf[3]-'0');
	}
	hour -= 6;
	return 60*hour+minute;
}

void func(){

	for(int i = 0; i < 7; i++){
		for(int k = 0; k < 1440; k++)check[i][k] = false;
	}

	for(int i = 0; i < N; i++)used[i] = false;

	char start[5];
	int tmp;

	for(int i = 0; i < N; i++){
		scanf("%s %d %s",info[i].name,&info[i].weekday,start);

		tmp = translate_TIME(start);

		info[i].start = tmp;
		info[i].end = tmp+29;
	}

	sort(info,info+N);

	vector<int> MUST;

	int P;
	scanf("%d",&P);

	char tmp_name[33];

	for(int loop = 0; loop < P; loop++){
		scanf("%s",tmp_name);

		for(int i = 0; i < N; i++){
			if(strCmp(info[i].name,tmp_name)){
				MUST.push_back(i);
				break;
			}
		}
	}

	bool FLG = true;

	for(int i = 0; i < P-1; i++){
		for(int k = i+1; k < P; k++){
			if((info[MUST[i]].weekday != info[MUST[k]].weekday) || (info[MUST[i]].start > info[MUST[k]].end || info[MUST[i]].end < info[MUST[k]].start)){
				//Do nothing
			}else{
				FLG = false;
				break;
			}
		}
	}
	if(!FLG){
		printf("-1\n");
		return;
	}

	for(int i = 0; i < P; i++){
		used[MUST[i]] = true;
		for(int k = info[MUST[i]].start; k <= info[MUST[i]].end; k++){
			check[info[MUST[i]].weekday][k] = true;
		}
	}

	vector<int> V;
	for(int i = 0; i < N; i++){
		if(used[i] == false){
			V.push_back(i);
		}
	}

	int ans = P;
	int right,V_index = 0;
	bool Found;

	if(V.size() == 0){
		printf("%d\n",P);
		return;
	}

	for(int w_day = 0; w_day < 7; w_day++){
		for(int time = 0; time < 1440;){

			if(V_index == V.size()){
				printf("%d\n",ans);
				return;
			}

			if(check[w_day][time]){
				time++;
				continue;
			}
			for(right = time; right+1 < 1440 && check[w_day][right+1] == false; right++);

			if(right-time+1 < 30){

				for(; V_index < V.size();){
					if((info[V[V_index]].weekday < w_day) || (info[V[V_index]].weekday == w_day && info[V[V_index]].start <= right)){
						 V_index++;
					}else{
						break;
					}
				}

				time = right+1;
			}else{

				Found = false;

				for(; V_index < V.size();){
					if((info[V[V_index]].weekday < w_day) || (info[V[V_index]].weekday == w_day && info[V[V_index]].start < time)){
						V_index++;
					}else if(info[V[V_index]].weekday == w_day && info[V[V_index]].start >= time && info[V[V_index]].end <= right){
						ans++;
						time = info[V[V_index]].end+1;
						Found = true;
						V_index++;
						break;
					}else{
						break;
					}
				}
				if(!Found){
					time = right+1;
				}
			}
		}
	}

	printf("%d\n",ans);
}


int main(){

	while(true){
		scanf("%d",&N);
		if(N == 0)break;

		func();
	}

	return 0;
}

Measured memory_bytes: 3140