Little Bob studied Trie in Data Structure class. He learned that Trie is a rooted tree to store some strings. There is a character on each edge of a Trie. (In this problem, the valid characters are lowercase Latin letters.)
Bob draws a Trie with n nodes, and the nodes are numbered with integers from 1 to n. The number of the root node is always 1.
He uses S
i to denote the string concatenated with characters on the path from the root to node i. It can be seen that S1 is the empty string. Furthermore, for a set P
{1, 2, . . . , n}, S
P = {S
i | i
P}.
Bob defines a kind of trait on this Trie. The trait can be described with a set P
{1, 2, . . . , n}. We say a string a has this trait, if and only if there exists a string b
S
P such that a ends with b. Here string a ends with string b means the last |b| characters of a is exactly b. (Note that every string ends with the empty string).
Bob also defines a mapping f on P
{1, 2, ..., n}, and f (P) is also a subset of {1, 2, . . . , n}. i
f (P) if and only if there exists j
P such that S
j ends with S
i.
Initially, there is no traits been defined. Bob will add traits whenever he wants. Let D
i denote the number of traits that S
i has until now. Note that D
i may change when a new trait is added.
Besides adding new traits, Bob may also ask you something about his Trie. In each query, Bob will give you a set P
{1, 2, . . . , n}, and you need to compute
. Are you willing to do this for Bob?