Permission Coverage Report

hard bitmask dp 1000 ms · 256 MB

Permission Coverage Report

An access-control system defines exactly 20 permissions, named by the lowercase letters a through t. The company has n roles. Each role requires a fixed subset of the 20 permissions — a role is satisfied by a user only if the user holds every permission the role requires.

You are auditing q users. For each user you know the subset of the 20 permissions they have been granted. For every user, report two numbers:

  1. Satisfied roles — the number of roles whose entire required permission set is contained in the user's granted permissions (the user could perform that role).
  2. Unrelated roles — the number of roles whose required permission set shares no permission at all with the user's granted permissions (the role needs nothing the user has).

A role with an empty requirement counts as both satisfied and unrelated for every user, since the empty set is a subset of any set and disjoint from any set. Different roles may require the same set of permissions, and each is counted separately.

Input

The first line contains two integers n and q (1 ≤ n, q ≤ 10^5).

Each of the next n lines describes one role's required permissions, and each of the next q lines describes one user's granted permissions. Each such line is either:

  • -, meaning the empty set, or
  • a comma-separated list of distinct letters from a to t (in any order), each appearing at most once, meaning the set of those permissions.

A line never contains spaces, and every letter on it lies between a and t.

Output

Print q lines. The i-th line contains two integers: the number of roles satisfied by the i-th user, followed by the number of roles unrelated to the i-th user.

Notes

In the first example, permissions are letters among a..t. A role requiring a,b is satisfied by a user granted a,b,c (superset), and is unrelated to a user granted only d (no overlap), but is neither satisfied nor unrelated to a user granted only a (partial overlap).

Example 1

Input

3 3
a,b
c
-
a,b,c
d
a

Output

3 1
1 3
1 2

Example 2

Input

4 2
-
-
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t
t
-
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t

Output

2 4
4 2

Hints · reveal one at a time if you're stuck

Hint 1

Each permission set is a 20-bit mask, and there are only about a million possible masks. Checking every role against every user is too slow, so think about what you could precompute once for all masks.

Hint 2

Both answers are the same kind of query: how many roles require a subset of a given mask? For the unrelated roles, consider the complement of the user's mask.

Hint 3

Count roles by their exact mask, then use a sum-over-subsets DP that processes one bit at a time across all 2^20 masks so each entry holds the count of roles that are submasks of it. Each user then needs just two table lookups.

report an issue Prefer your own editor? sooks prep 68 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.