#include<bits/stdc++.h>
using namespace std;
typedef int ll;
typedef long double ld;
#define lp(var,start,end) for (ll var = start; var <end ; ++var)
#define rlp(var,start,end) for(ll var = start; var>=end ; var--)
#define pb push_back
#define mp make_pair
#define pf push_front
#define ff first
#define ss second
#define vll vector<ll>
#define vld vector<ld>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define vpll vector<pll>
#define vpld vector<pld>
#define all(X) X.begin(),X.end()
#define endl "\n"
#define sz(x) ((int)(x).size())
const ll N=1e6+5,inf=1e9,k=27;
ll dp[N],ans;
struct node{
ll nxt[k],go[k];
ll p,link,nxtleaf;
ll cnt;
ll pch;
ll leaf;
ll len;
};
node init()
{
node tp;
lp(i,0,k)
{
tp.nxt[i]=-1;
tp.go[i]=-1;
}
tp.nxtleaf=0;
tp.link=-1;
tp.leaf=0;
tp.cnt=0;
return tp;
}
vector<node> v;
void insert(string s)
{
ll cur=0;
lp(i,0,sz(s))
{
v[cur].cnt++;
ll a=s[i]-'a';
if(v[cur].nxt[a]==-1)
{
node tp = init();
tp.p=cur;
tp.pch=a;
v.pb(tp);
v[cur].nxt[a]=sz(v)-1;
}
v[cur].len=i;
cur=v[cur].nxt[a];
}
v[cur].len=sz(s);
v[cur].cnt++;
v[cur].leaf=1;
}
ll go(ll cur, ll c);
ll get_link(ll cur)
{
if(v[cur].link==-1)
{
if(cur==0 || v[cur].p==0 )
{
v[cur].link = 0;
}
else
{
v[cur].link = go(get_link(v[cur].p),v[cur].pch);
}
}
return v[cur].link;
}
ll go(ll cur, ll c)
{
if(v[cur].go[c]==-1)
{
if(v[cur].nxt[c]!=-1)
{
v[cur].go[c]=v[cur].nxt[c];
}
else
{
v[cur].go[c] = cur == 0 ? 0 : go(get_link(cur), c);
}
}
return v[cur].go[c];
}
ll bfs()
{
ll cur=0;
queue<ll> q;
q.push(cur);
while(!q.empty())
{
cur=q.front();
q.pop();
get_link(cur);
lp(i,0,k)
{
if(v[cur].nxt[i]>0)
{
q.push(v[cur].nxt[i]);
}
}
}
return 0;
}
ll chk(ll cur,ll val)
{
if(cur==0)return 0;
if(dp[cur]==-1)
{
dp[cur]=v[cur].len*v[cur].cnt;
dp[cur]=max(dp[cur],chk(v[cur].link,val));
}
ans=max(ans,dp[cur]*val);
return dp[cur];
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
freopen("exciting.in", "r", stdin);
ll t;
cin >> t;
while(t--)
{
ans=0;
v.clear();
ll n;
cin >> n;
// if(n<=0)
// {
// while(1);
// }
// assert(n>0);
string s[n];
node root =init();
v.pb(root);
vector<ll> a[n];
lp(i,0,n)
{
cin >> s[i];
insert(s[i]);
}
lp(i,0,n)
{
lp(j,0,sz(s[i]))
{
ll x;
cin >> x;
a[i].pb(x);
}
}
lp(i,0,sz(v)+2)
{
dp[i]=-1;
}
bfs();
ll cur=0;
lp(i,0,n)
{
cur=0;
lp(j,0,sz(s[i]))
{
cur=go(cur,s[i][j]-'a');
chk(cur,a[i][j]);
}
}
cout<<ans<<endl;
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdHlwZWRlZiBpbnQgbGw7CnR5cGVkZWYgbG9uZyBkb3VibGUgbGQ7CiAKI2RlZmluZSBscCh2YXIsc3RhcnQsZW5kKSBmb3IgKGxsIHZhciA9IHN0YXJ0OyB2YXIgPGVuZCA7ICsrdmFyKQojZGVmaW5lIHJscCh2YXIsc3RhcnQsZW5kKSBmb3IobGwgdmFyID0gc3RhcnQ7IHZhcj49ZW5kIDsgdmFyLS0pCiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgbXAgbWFrZV9wYWlyCiNkZWZpbmUgcGYgcHVzaF9mcm9udAojZGVmaW5lIGZmIGZpcnN0CiNkZWZpbmUgc3Mgc2Vjb25kCiNkZWZpbmUgdmxsIHZlY3RvcjxsbD4KI2RlZmluZSB2bGQgdmVjdG9yPGxkPgojZGVmaW5lIHBsbCBwY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