extern crate core;

use std::str::FromStr;
use std::cmp::Ordering;
use std::collections::HashSet;

struct Istream {
    line: Vec<String>,
    position: usize
}
type Parse<T: FromStr> = Result<T, T::Err>;
impl Istream {
    fn new() -> Istream {
        Istream {line:vec![], position: 0}
    }
    fn read_word(&mut self) -> &String {
        if self.position == self.line.len() {
            let mut line= String::new();
            std::io::stdin().read_line(&mut line).ok();
            self.line = line.split_whitespace().map(|x|x.to_string()).collect();
            self.position = 0
        }
        let result = &self.line[self.position];
        self.position += 1;
        result
    }
    fn read(&mut self) -> String {
        self.read_word().to_string()
    }
    fn get<T: FromStr>(&mut self) -> T {self.read_word().parse().ok().unwrap()}
    fn read_usize(&mut self) -> usize {self.get()}
    fn read_int(&mut self) -> i32 {self.get()}
    fn read_long(&mut self) -> i64 {self.get()}
    fn read_double(&mut self) -> f64 {self.get()}
}
fn tabulate<T, F>(count: usize, elements: &mut F) -> Vec<T> where F: FnMut(usize) -> T {
    let mut result:Vec<T> = Vec::with_capacity(count);
    for i in 0 .. count {
        result.push(elements(i))
    }
    result
}
fn fill<T, F>(count: usize, generator: F) -> Vec<T> where F: Fn() -> T {
    let mut result: Vec<T> = Vec::with_capacity(count);
    for _i in 0 .. count {
        result.push(generator())
    }
    result
}
#[derive(Debug)]
struct Program {
    title: String,
    date: Date,
}
#[derive(Debug, Copy, Clone)]
struct Date {
    day: i32,
    hour: i32,
    minute: i32,
}
impl Date {
    fn from_int(day: &i32, time: &i32) -> Date {
        if time / 100 >= 24 {
            Date{day: (day + 1) % 7, hour: (time / 100) % 24, minute: (time % 100)}
        }else {
            Date{day: day + 0, hour: (time / 100), minute: (time % 100)}
        }
    }
    fn from_minute(minute: i32) -> Date {
        Date{day: (minute / 1440) % 7, hour: (minute / 60) % 24, minute: minute % 60}
    }
    fn to_minute(&self) -> i32 {
        self.day * 1440 + self.hour * 60 + self.minute
    }
    fn add_minute(&self, minute: i32) -> Date {
        Date::from_minute(self.to_minute() + minute)
    }
}
impl Ord for Date {
    fn cmp(&self, other: &Self) -> Ordering {
        if self.day == other.day {
            if self.hour == other.hour {
                self.minute.cmp(&other.minute)
            }else {
                self.hour.cmp(&other.hour)
            }
        }else {
            self.day.cmp(&other.day)
        }
    }
}
impl PartialOrd for Date {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }

    fn lt(&self, other: &Self) -> bool {
        self.cmp(other) == Ordering::Less
    }

    fn le(&self, other: &Self) -> bool {
        self.cmp(other) != Ordering::Greater
    }

    fn gt(&self, other: &Self) -> bool {
        self.cmp(other) == Ordering::Greater
    }

    fn ge(&self, other: &Self) -> bool {
        self.cmp(other) != Ordering::Less
    }
}
impl PartialEq for Date {
    fn eq(&self, other: &Self) -> bool {
        self.day == other.day && self.hour == other.hour && self.minute == other.minute
    }

    fn ne(&self, other: &Self) -> bool {
        !self.eq(other)
    }
}
impl Eq for Date {}
impl Program {
    fn new(title: String, day: i32, start: i32) -> Program {
        let date = Date::from_int(&day, &start);
        Program{title: title, date: date}
    }
    fn overlapped(&self, other: &Program) -> bool {
        let s = self.date.to_minute();
        let o = other.date.to_minute();
        (s - o + 24 * 60 * 7) % (24 * 60 * 7) < 30 || (o - s + 24 * 60 * 7) % (24 * 60 * 7) < 30
    }
}
fn main() {
    let mut stdin = Istream::new();
    let input = &mut stdin;
    loop {
        let n = input.read_usize();
        if n == 0 {
            break;
        }
        let mut programs = tabulate(n, &mut |_i| {
            let title = input.read();
            let day = input.read_int();
            let start = input.read_int();
            Program::new(title, day, start)
        });
        programs.sort_by_key(|p|p.date);
        let mut favorite: HashSet<String> = HashSet::new();
        let p = input.read_usize();
        for _ in 0 .. p {
            favorite.insert(input.read());
        }
        let mut time = vec![false; 7 * 24 * 60];
        let len: usize = 7 * 24 * 60;
        let mut count = 0;
        let mut favorite_i = 0;
        'outer: for (i, p) in programs.iter().enumerate() {
            if count >= 0 && favorite.contains(&p.title) {
                favorite_i = i;
                count += 1;
                let minute = p.date.to_minute() as usize;
                for i in 0 .. 30 {
                    if time[(minute + i) % len] {
                        count = -1;
                        break 'outer;
                    }else {
                        time[(minute + i) % len] = true;
                    }
                }
            }
        }
        if count >= 0 {
            'outer: for p in 0..n {
                let minute = programs[(p + favorite_i) % n].date.to_minute() as usize;
                let mut is_overlapped = false;
                for i in 0..30 {
                    is_overlapped |= time[(minute + i) % len];
                }
                if !is_overlapped {
                    count += 1;
                    for i in 0 .. 30 {
                        time[(minute + i) % len] = true;
                    }
                }
            }
        }
        println!("{}", count)
    }
}


Measured memory_bytes: 5056