#include <bits/stdc++.h>
using namespace std;
const pair<long long, long long> pb(29, 37);
const long long mod = 1e9 + 7;
const int N = 6e5 + 9;
string t, s[N];
char S[N];
vector<int> V;
pair<long long, long long> hashh[N];
pair<long long, long long> P[N];
int n, m, str[N], pos[N], sa[N], tmp[N], gap;
long long ans[N], ansrev[N];
pair<long long, long long> gethash(int l, int r) {
  return {
      (hashh[r].first - (hashh[l - 1].first * P[r - l + 1].first) % mod + mod) %
          mod,
      (hashh[r].second - (hashh[l - 1].second * P[r - l + 1].second) % mod +
       mod) %
          mod};
}
bool cmp(int i, int j) {
  if (pos[i] != pos[j]) return pos[i] < pos[j];
  i += gap;
  j += gap;
  return (i <= n && j <= n ? pos[i] < pos[j] : i > j);
}
void constructSA() {
  for (int i = 1; i <= n; i++) {
    sa[i] = i;
    pos[i] = S[i];
  }
  for (gap = 1; gap <= n; gap <<= 1) {
    sort(sa + 1, sa + 1 + n, cmp);
    for (int i = 1; i <= n; i++) tmp[i] = tmp[i - 1] + cmp(sa[i - 1], sa[i]);
    for (int i = 1; i <= n; i++) pos[sa[i]] = tmp[i];
  }
}
bool check_pop(int x, int j) {
  int len = s[str[x]].size();
  if (j + len - 1 > n) return 1;
  pair<long long, long long> h1 = gethash(x, x + len - 1),
                             h2 = gethash(j, j + len - 1);
  return h1.first != h2.first || h1.second != h2.second;
}
void re() {
  V.clear();
  for (int i = 1; i <= n; i++) {
    str[i] = 0;
  }
}
void comp() {
  constructSA();
  for (int i = 1; i <= n; i++) {
    hashh[i].first = (hashh[i - 1].first * pb.first + S[i]) % mod;
    hashh[i].second = (hashh[i - 1].second * pb.second + S[i]) % mod;
  }
  for (int i = 1; i <= n; i++) {
    while (!V.empty() && check_pop(V.back(), sa[i])) {
      V.pop_back();
    }
    if (sa[i] <= t.size()) {
      ans[sa[i]] = V.size();
    }
    if (str[sa[i]] > 0) {
      V.push_back(sa[i]);
    }
  }
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int idx = 0;
  cin >> t;
  cin >> m;
  S[0] = '#';
  int sh = t.size();
  for (int j = 0; j < sh; j++) {
    S[++idx] = t[j];
  }
  S[++idx] = 'z' + 1;
  P[0] = {1, 1};
  for (int i = 1; i < N; i++)
    P[i] = {P[i - 1].first * pb.first % mod, P[i - 1].second * pb.second % mod};
  for (int i = 1; i <= m; i++) {
    S[++idx] = '#';
    str[idx + 1] = i;
    cin >> s[i];
    int pp = s[i].size();
    for (int j = 0; j < pp; j++) S[++idx] = s[i][j];
  }
  n = idx;
  idx = 0;
  comp();
  for (int i = 1; i <= n; i++) ansrev[i] = ans[i];
  re();
  reverse(t.begin(), t.end());
  for (int i = 1; i <= m; i++) reverse(s[i].begin(), s[i].end());
  for (int j = 0; j < int(t.size()); j++) {
    S[++idx] = t[j];
  }
  S[++idx] = 'z' + 1;
  for (int i = 1; i <= m; i++) {
    S[++idx] += '#';
    str[idx + 1] = i;
    int pp = s[i].size();
    for (int j = 0; j < pp; j++) S[++idx] = s[i][j];
  }
  comp();
  int len = t.size();
  long long pr = 0;
  for (int i = 1; i <= len; i++) {
    pr += ansrev[i] * ans[len - i + 2];
  }
  cout << pr << '\n';
  return 0;
}

### Prompt

Your challenge is to write a cpp solution to the following problem:

You are given a string t and n strings s_1, s_2, ..., s_n. All strings consist of lowercase Latin letters.

Let f(t, s) be the number of occurences of string s in string t. For example, f('aaabacaa', 'aa') = 3, and f('ababa', 'aba') = 2.

Calculate the value of ∑_{i=1}^{n} ∑_{j=1}^{n} f(t, s_i + s_j), where s + t is the concatenation of strings s and t. Note that if there are two pairs i_1, j_1 and i_2, j_2 such that s_{i_1} + s_{j_1} = s_{i_2} + s_{j_2}, you should include both f(t, s_{i_1} + s_{j_1}) and f(t, s_{i_2} + s_{j_2}) in answer.

Input

The first line contains string t (1 ≤ |t| ≤ 2 ⋅ 10^5).

The second line contains integer n (1 ≤ n ≤ 2 ⋅ 10^5).

Each of next n lines contains string s_i (1 ≤ |s_i| ≤ 2 ⋅ 10^5).

It is guaranteed that ∑_{i=1}^{n} |s_i| ≤ 2 ⋅ 10^5. All strings consist of lowercase English letters.

Output

Print one integer — the value of ∑_{i=1}^{n} ∑_{j=1}^{n} f(t, s_i + s_j).

Examples

Input


aaabacaa
2
a
aa


Output


5


Input


aaabacaa
4
a
a
a
b


Output


33

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const pair<long long, long long> pb(29, 37);
const long long mod = 1e9 + 7;
const int N = 6e5 + 9;
string t, s[N];
char S[N];
vector<int> V;
pair<long long, long long> hashh[N];
pair<long long, long long> P[N];
int n, m, str[N], pos[N], sa[N], tmp[N], gap;
long long ans[N], ansrev[N];
pair<long long, long long> gethash(int l, int r) {
  return {
      (hashh[r].first - (hashh[l - 1].first * P[r - l + 1].first) % mod + mod) %
          mod,
      (hashh[r].second - (hashh[l - 1].second * P[r - l + 1].second) % mod +
       mod) %
          mod};
}
bool cmp(int i, int j) {
  if (pos[i] != pos[j]) return pos[i] < pos[j];
  i += gap;
  j += gap;
  return (i <= n && j <= n ? pos[i] < pos[j] : i > j);
}
void constructSA() {
  for (int i = 1; i <= n; i++) {
    sa[i] = i;
    pos[i] = S[i];
  }
  for (gap = 1; gap <= n; gap <<= 1) {
    sort(sa + 1, sa + 1 + n, cmp);
    for (int i = 1; i <= n; i++) tmp[i] = tmp[i - 1] + cmp(sa[i - 1], sa[i]);
    for (int i = 1; i <= n; i++) pos[sa[i]] = tmp[i];
  }
}
bool check_pop(int x, int j) {
  int len = s[str[x]].size();
  if (j + len - 1 > n) return 1;
  pair<long long, long long> h1 = gethash(x, x + len - 1),
                             h2 = gethash(j, j + len - 1);
  return h1.first != h2.first || h1.second != h2.second;
}
void re() {
  V.clear();
  for (int i = 1; i <= n; i++) {
    str[i] = 0;
  }
}
void comp() {
  constructSA();
  for (int i = 1; i <= n; i++) {
    hashh[i].first = (hashh[i - 1].first * pb.first + S[i]) % mod;
    hashh[i].second = (hashh[i - 1].second * pb.second + S[i]) % mod;
  }
  for (int i = 1; i <= n; i++) {
    while (!V.empty() && check_pop(V.back(), sa[i])) {
      V.pop_back();
    }
    if (sa[i] <= t.size()) {
      ans[sa[i]] = V.size();
    }
    if (str[sa[i]] > 0) {
      V.push_back(sa[i]);
    }
  }
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int idx = 0;
  cin >> t;
  cin >> m;
  S[0] = '#';
  int sh = t.size();
  for (int j = 0; j < sh; j++) {
    S[++idx] = t[j];
  }
  S[++idx] = 'z' + 1;
  P[0] = {1, 1};
  for (int i = 1; i < N; i++)
    P[i] = {P[i - 1].first * pb.first % mod, P[i - 1].second * pb.second % mod};
  for (int i = 1; i <= m; i++) {
    S[++idx] = '#';
    str[idx + 1] = i;
    cin >> s[i];
    int pp = s[i].size();
    for (int j = 0; j < pp; j++) S[++idx] = s[i][j];
  }
  n = idx;
  idx = 0;
  comp();
  for (int i = 1; i <= n; i++) ansrev[i] = ans[i];
  re();
  reverse(t.begin(), t.end());
  for (int i = 1; i <= m; i++) reverse(s[i].begin(), s[i].end());
  for (int j = 0; j < int(t.size()); j++) {
    S[++idx] = t[j];
  }
  S[++idx] = 'z' + 1;
  for (int i = 1; i <= m; i++) {
    S[++idx] += '#';
    str[idx + 1] = i;
    int pp = s[i].size();
    for (int j = 0; j < pp; j++) S[++idx] = s[i][j];
  }
  comp();
  int len = t.size();
  long long pr = 0;
  for (int i = 1; i <= len; i++) {
    pr += ansrev[i] * ans[len - i + 2];
  }
  cout << pr << '\n';
  return 0;
}

```