Under construction

Still being built. Things may change

Exciting Menus

Time limit4sMemory limit512 MB

Summary
Given N strings with a joy value per position, maximize over all substrings the product of its length, the joy at its end, and the number of strings having it as a prefix.
Level

Hard8 of 10

Topics
Trie, String, Array, Implementation
Solved
No attempts yet

Problem

A restaurant has NN menus, given as the strings S1,…,SNS^1, \dots , S^N. Each string SiS^i comes with an array AiA^i of joy levels of the same length. In this problem you choose a submenu defined by three integers (i,j,k)(i, j, k), where ii is the index of a menu and j,kj, k specify a substring of that menu.

Find the submenu of the highest quality, where the quality QQ is defined by

Q(i,j,k)=popularity(Sj,ki)⋅Aki⋅∣Sj,ki∣Q(i, j, k) = \text{popularity}(S^i_{j,k}) \cdot A^i_k \cdot |S^i_{j,k}|

  • Sj,kiS^i_{j,k} = the substring [j,k][j, k] of the string SiS^i (with j,kj, k one-based and inclusive).
  • popularity(Sj,kiS^i_{j,k}) = the number of menus among S1,…,SNS^1, \dots , S^N that contain the submenu string Sj,kiS^i_{j,k} as a prefix.
  • ∣Sj,ki∣|S^i_{j,k}| is the size of the submenu, the length of the substring that represents it.
  • AkiA^i_k is the joy level of the submenu (the kk-th entry of the ii-th array). Note that the index jj is not used here.

Can you find the submenu of the highest quality? Output the maximum value of the function QQ.

Input

The first line of the input contains a single integer TT, the number of test cases.

Each test case begins with a line containing an integer NN (1≤N≤1051 \le N \le 10^5), the number of menus in the restaurant.

Then NN lines follow. The ii-th line contains a string SiS^i representing the ii-th menu. All menus are nonempty strings of lowercase English letters, and the sum of their lengths does not exceed 10510^5 per test case.

Then NN lines follow. The ii-th line contains the array AiA^i, which has the same length as the string SiS^i, with 0≤Aki≤1090 \le A^i_k \le 10^9. The values of each array are space-separated.

Output

For each test case, print a single line containing an integer QQ, the maximum quality over the best submenu (i,j,k)(i, j, k).

Hint

The triples corresponding to the answers of the two given cases are (1,1,5)(1, 1, 5) and (1,4,5)(1, 4, 5).

Examples1

  1. Example 1

    Input
    2
    2
    aabaa
    aa
    0 0 0 0 100
    0 1
    4
    bbbaa
    aa
    aab
    aax
    0 0 0 0 100
    0 0
    0 0 0
    0 0 0
    
    Expected output
    500
    600