1843: Code a Trie

内存限制:64 MB 时间限制:1 S
题面:传统 评测方式:文本比较 上传者:
提交:0 通过:0

题目描述

  https://codeforces.com/gym/102822/problem/C

A Trie is a tree data structure used to store strings. Here is Little Rabbit's C++ code for inserting and querying a string in a Trie. The character set is lowercase letters.

struct Trie {  
int e[N][26], val[N], tot; // N is a constant  
Trie() {  
    tot = 0;  
    val[0] = Rand(1, 1000000000);  
    memset(e, 0, sizeof e);  
}  
void insert(const char *s, int n) { // n is the length of string s  
    for (int i = 0, u = 0; i < n; u = e[u][s[i] - 'a'], i++)  
    if (!e[u][s[i] - 'a']) {  
    e[u][s[i] - 'a'] = ++tot;  
    val[tot] = Rand(1, 1000000000);  
    // Rand(l, r) can generate  
    // distinct random numbers in the range of [l, r]  
    }  
    }  
    int query(const char *s, int n) { 
    // n is the length of string s  
    int u = 0;  
    for (int i = 0; i < n; u = e[u][s[i] - 'a'], i++)  
    if (!e[u][s[i] - 'a'])  
    break;  
    return val[u];  
} 
}; 
Please note that the integers generated by Rand function are all distinct.
Little Rabbit inserts some (possibly zero) strings in the Trie by using the insert function. But after a while, he totally forgets those strings he has inserted. So he uses the query function nn times to query some strings and gets nn return values. According to these values, you need to tell Little Rabbit how many nodes the Trie has at least. Please note that the root of the Trie should also be counted as a node. In the code above, the number of nodes is tot+1.


输入格式

Input

The first line of the input contains an integer T (1≤T≤5000) — the number of test cases.

The first line of each test case contains an integer n (1≤n≤105) — the number of times Little Rabbit uses the query function.

Then in the next n lines, each line contains a string s containing only lowercase letters and an integer v (1≤v≤109), which means Little Rabbit queries the string s, and the return value is v. The sum of the lengths of the strings in each test case will not exceed 105, and the sum of the lengths of the strings in all test cases will not exceed 5×105


输出格式


For the x-th test case, if the answer is yy, output Case #xy in a single line. If no Trie can meet the conditions, output Case #x: -1 in a single line.

输入样例 复制

6
2
aa 1
a 2
1
a 1
3
aa 1
ab 1
ac 1
2
aa 1
ac 2
3
aaa 1
ac 1
aa 3
3
aa 1
ac 1
a 3

输出样例 复制

Case #1: 3
Case #2: 1
Case #3: 1
Case #4: 3
Case #5: -1
Case #6: -1