#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct pro{
    char name[40];
    int st;
    bool operator <(const pro &a)const{
        return st <= a.st;
    }
}p[600];
char nam[600][40];
int pi[600];
int d[600][2];
int cal(int l,int r)
{
    for(int i = l; i <= r; ++i){
        d[i][0] = 0,d[i][1] = 1;
        for(int j = l; j < i; ++j){
            if(d[i][0] < d[j][0])
                d[i][0] = d[j][0];
            if(d[i][0] < d[j][1])
                d[i][0] = d[j][1];
            if(p[i].st - p[j].st>=30&&d[i][1] < d[j][0]+1)
                d[i][1] = d[j][0]+1;
            if(p[i].st - p[j].st>=30&&d[i][1] < d[j][1]+1)
                d[i][1] = d[j][1]+1;
        }
    }
    int ans = 0;
    for(int i = l; i <= r; ++i){
        if(ans < d[i][0])
            ans = d[i][0];
        if(ans < d[i][1])
            ans = d[i][1];
    }
    return ans;
}
int main()
{
    //freopen("d:\\in.txt","r",stdin);
    int n,m;
    while(scanf("%d",&n),n){
        for(int i = 0; i < n; ++i){
            int a,b;
            scanf("%s %d %d",p[i].name,&a,&b);
            p[i].st = a*1440+b/100*60+b%100;
            //printf("%d %s %d %d %d\n",i,p[i].name,a,b,p[i].st);
        }
        sort(p,p+n);
        //for(int i = 0; i < n; ++i)
        //    printf("%d %s %d\n",i,p[i].name,p[i].st);
        scanf("%d",&m);
        int cnt = 0;
        bool fg = true;
        for(int i = 0 ;i < m; ++i){
            scanf("%s",nam[i]);
            bool flag = true;
            for(int j = 0; j < n; ++j){
                if(strcmp(nam[i],p[j].name) == 0){
                    pi[cnt++] = j;
                    flag = false;
                }
            }
            if(flag)
                fg = false;
        }
        if(fg == false){
            puts("-1");
            continue;
        }
        m = cnt;
        sort(pi,pi+m);
        fg = true;
        for(int i = 1;i < m; ++i){
            if(p[pi[i]].st -p[pi[i-1]].st < 30){
                fg = false;
                break;
            }
        }
        if(fg == false){
            puts("-1");
            continue;
        }
        int ans = m;
        for(int i = 1; i < m; ++i){
            int l = pi[i-1];
            while(l < pi[i]){
                if(p[l].st-p[pi[i-1]].st >= 30)
                    break;
                ++l;
            }
            if(l == pi[i])
                continue;
            //printf("#%d\n",l);
            int r = pi[i];
            while(r > pi[i-1]){
                if(p[pi[i]].st-p[r].st >= 30)
                    break;
                --r;
            }
            if(r == pi[i-1]){
                continue;
            }
            //printf("%d %d %d\n",i,l,r);
            ans += cal(l,r);
        }
        //printf("%d\n",pi[0]);
        int l = pi[m-1];
        while(l < n){
            if(p[l].st-p[pi[m-1]].st >= 30)
                break;
            ++l;
        }
        if(l < n)
            ans += cal(l,n-1);
        //printf("%d\n",ans);
        int r = pi[0];
        while(r >= 0){
            if(p[pi[0]].st-p[r].st >= 30)
                break;
            --r;
        }
        if(r >= 0){
            ans += cal(0,r);
        }
        printf("%d\n",ans);
    }
    return 0;
}

Measured memory_bytes: 0