import sequtils,algorithm,strutils,intsets,tables template times*(n:int,body) = (for _ in 0.." .} proc scan(): int = while true: let k = getchar_unlocked() if k < '0': break result = 10 * result + k.ord - '0'.ord let n = scan() let m = scan() let k = scan() # a -> c -> b var P = newSeqWith(n+1,newTable[int,seq[int]]()) m.times: let a = scan() let b = scan() let c = scan() if c notin P[a]: P[a][c] = @[] if c notin P[b]: P[b][c] = @[] P[a][c] &= b P[b][c] &= a let C = newSeqWith(k,scan()) var nows = initIntSet() for a in 1..n: if C[0] notin P[a] : continue for b in P[a][C[0]]: nows.incl b for i in 1..