// Made By Haireden Aibyn #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define fname "" #define INF 2147483647 #define MOD 1000000007 #define mp make_pair #define F first #define S second #define sc scanf #define all(x) x.begin(), x.end() #define size(x) int(x.size()) #define pr printf #define deb(x) cerr << " | " << #x << " = " << x #define pb push_back #define ex exit(0) #define tim printf("%.4lf\n", (clock() * 1.) / CLOCKS_PER_SEC) #define cas printf(""), ex; #define y1 y4 typedef long long ll; typedef unsigned long long ull; const int N = 2005000; bool fin[N]; int t[N][26]; int suf[N], super[N], last[N], par[N], in[N]; string req; int lq, rq; int h[N]; ll ans = 0; int sz = 1; vector posit[N]; inline void init() { suf[1] = 1; super[1] = 1; for (int i = 0; i < 26; i++) { t[1][i] = 1; } } inline void add(string s, int index) { int root = 1; for (auto ch : s) { int crs = ch - 'a'; if (t[root][crs] <= 1) { t[root][crs] = ++sz; par[sz] = root; in[sz] = crs; } root = t[root][crs]; } fin[root] = 1; posit[root].pb(index); last[index] = root; } inline void bfs() { int l = 0; vector q; q.pb(1); while (l < size(q)) { int v = q[l++]; if (v != 1) { if (par[v] == 1) { suf[v] = 1; } else { suf[v] = t[suf[par[v]]][in[v]]; } if (suf[v] != 1) { if (fin[suf[v]]) { super[v] = suf[v]; } else { super[v] = super[suf[v]]; } } else { super[v] = 1; } } for (int i = 0; i < 26; i++) { if (t[v][i] > 1) { q.pb(t[v][i]); } if (t[v][i] == 0) { t[v][i] = t[suf[v]][i]; } } } } void dfs(int v, int pos) { if (pos > size(req)) return; int to = super[v]; while (to != 1) { if (fin[to]) { for (auto id : posit[to]) { if (lq <= id && id <= rq) ans += h[id]; } } to = super[to]; } if (fin[v]) { for (auto id : posit[v]) { if (lq <= id && id <= rq) ans += h[id]; } } if (pos == size(req)) return; v = t[v][req[pos] - 'a']; dfs(v, pos + 1); } int main() { ios_base::sync_with_stdio(0); init(); int n; cin >> n; for (int i = 1; i <= n; i++) { string s; cin >> s; add(s, i); } for (int i = 1; i <= n; i++) cin >> h[i]; bfs(); int m; cin >> m; ll mn = 1e18; ll mx = -1e18; for (int i = 1; i <= m; i++) { cin >> lq >> rq >> req; ans = 0; lq++, rq++; dfs(1, 0); mn = min(ans, mn); mx = max(ans, mx); } cout << mn << " " << mx; return 0; }