"""
Hi Kaggler!

This is a code that allocates one second for roads entering each intersection.
"""
file_path = '../input/hashcode-2021-oqr-extension/hashcode.in'

f = open(file_path, 'r')

# first line
line = f.readline()
D, I, S, V, F = map(int, line.split())

roadnm_to_path = {}
path_to_roadnm = {}
adj = [[0 for _ in range(I+1)] for _ in range(I+1)]

for i in range(S):
    line = f.readline()
    start, end, rd_nm, weight = line.split()
    start, end, weight = int(start), int(end), int(weight)
    adj[start][end] = weight
    roadnm_to_path[rd_nm] = [start, end]
    path_to_roadnm['[{}, {}]'.format(start, end)] = rd_nm

car = []
for i in range(V):
    line = f.readline()
    line = line.split()
    _, path_nm = line[0], line[1:]
    path = []
    for street in path_nm:
        start, end = roadnm_to_path[street]
        if path:
            path.extend([start, end])
        else:
            path.append(end)
    car.append(path)

f.close()
def solution(D, I, S, V, F, adj, roadnm_to_path, path_to_roadnm, car):
    res = []
    num_intersection = 0
    for _to in range(I+1):
        cnt = 0
        tmp = []
        for _from in range(I+1):
            if adj[_from][_to] != 0:
                road_nm = path_to_roadnm['[{}, {}]'.format(_from, _to)]
                tmp.append(road_nm)
                cnt += 1
        if cnt == 0:
            continue
        res.append([_to])
        res.append([cnt])
        for i in range(cnt):
            res.append([tmp[i], 1])
        num_intersection += 1
        
        if _to % 1000 == 0:
            print("DEBUG {} ..... ".format(_to))
    
    res.insert(0, [num_intersection])
    return res
res = solution(D, I, S, V, F, adj, roadnm_to_path, path_to_roadnm, car)
f = open('submission.csv', 'w')
for one_res in res:
    try:
        data = ' '.join([str(s) for s in one_res]) + '\n'
    except:
        data = str(one_res) + '\n'
    f.write(data)

f.close()