// AOJ 1063: Watchin' TVA
// 2017.9.30 bal4u@uu

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

#define HSIZ 1999
typedef struct { char *w; int k; } HASH;
HASH hash[HSIZ + 5], *hashend = hash + HSIZ;

int lookup(char *w)
{
	int i, l;
	HASH *tp;
	i = (101 * *w + 103 * *(w + (l = strlen(w)) - 1) + 107 * l) % HSIZ;
	tp = hash + i;
	while (tp->w != NULL) {
		if (!strcmp(tp->w, w)) return tp->k;
		if (++tp == hashend) tp = hash;
	}
	return -1;
}

int insert(char *w, int k)
{
	int i, l;
	HASH *tp;
	i = (101 * *w + 103 * *(w + (l = strlen(w)) - 1) + 107 * l) % HSIZ;
	tp = hash + i;
	while (tp->w != NULL) {
		if (!strcmp(tp->w, w)) return -1;
		if (++tp == hashend) tp = hash;
	}
	tp->w = w, tp->k = k;
	return 0;
}

typedef struct { char nm[35]; int s, e, f; } T;
T tbl[502]; int n;

int cmp(T *a, T *b) { return a->s - b->s; }

int main()
{
	int p, i, j, k, e, ans;
	char nm[35];
	T *t;

	while (scanf("%d", &n) && n > 0) {
		memset(hash, 0, sizeof(hash));
		for (t = tbl, i = 0; i < n; i++, t++) {
			int w, s, h, m;
			scanf("%s%d%d", t->nm, &w, &s);
			h = s / 100, m = s % 100;
			t->s = (1440*w + h*60 + m) % 10080, t->e = t->s + 30; 
			t->f = 0;
			insert(t->nm, i);
		}
		scanf("%d", &p);
		while (p-- > 0) {
			scanf("%s", nm);
			(tbl + lookup(nm))->f = 1;
		}
		if (n == 1) { puts("1"); continue; }
		qsort(tbl, n, sizeof(T), cmp);

		for (k = 0; k < n; k++) if (tbl[k].f) break;
		if (k >= n) k = 0;
		for (ans = 1, i = k, j = i+1; j != k; j++) {
			if (i >= n) i = 0;
			if (j >= n) j = 0;
			if (tbl[i].e > 10080) e = tbl[i].e - 10080; else e = 0;
			if ((tbl[i].s <= tbl[j].s && tbl[j].s < tbl[i].e) || tbl[j].s < e) {
				if (tbl[j].f & tbl[i].f) { ans = -1; break; }
				else if (tbl[j].f) i = j;
			} else if (tbl[j].s <= tbl[k].s && tbl[k].s < tbl[j].e) ;
			else {
				ans++, i = j;
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}