#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
  int v = 0, f = 1;
  char c = getchar();
  while (!isdigit(c) && c != '-') c = getchar();
  if (c == '-')
    f = -1;
  else
    v = (c & 15);
  while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15);
  x = v * f;
}
inline void read(long long &x) {
  long long v = 0ll, f = 1ll;
  char c = getchar();
  while (!isdigit(c) && c != '-') c = getchar();
  if (c == '-')
    f = -1;
  else
    v = (c & 15);
  while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15);
  x = v * f;
}
inline void readc(char &x) {
  char c;
  while (((c = getchar()) == ' ') || c == '\n')
    ;
  x = c;
}
inline void writes(string s) { puts(s.c_str()); }
inline void writeln() { writes(""); }
inline void writei(int x) {
  if (x < 0) {
    putchar('-');
    x = abs(x);
  }
  if (!x) putchar('0');
  char a[25];
  int top = 0;
  while (x) {
    a[++top] = (x % 10) + '0';
    x /= 10;
  }
  while (top) {
    putchar(a[top]);
    top--;
  }
}
inline void writell(long long x) {
  if (x < 0) {
    putchar('-');
    x = abs(x);
  }
  if (!x) putchar('0');
  char a[25];
  int top = 0;
  while (x) {
    a[++top] = (x % 10) + '0';
    x /= 10;
  }
  while (top) {
    putchar(a[top]);
    top--;
  }
}
int n, m, i, j, pre[200005], suf[200005], fa[200005], sz[200005],
    ch[200005][26], cnt;
long long ans;
string st, s[200005];
void clear(int x) {
  fa[x] = sz[x] = 0;
  memset((ch[x]), (0), (sizeof((ch[x]))));
}
void ins(string st) {
  int x = 1, i;
  for (i = 0; i < st.length(); i++) {
    if (!ch[x][st[i] - 'a']) {
      ch[x][st[i] - 'a'] = ++cnt;
      clear(cnt);
    }
    x = ch[x][st[i] - 'a'];
  }
  sz[x]++;
}
void rebuild() {
  queue<int> qx;
  int i;
  fa[1] = 1;
  for ((i) = (0); (i) <= (25); (i)++) {
    if (ch[1][i]) {
      qx.push(ch[1][i]);
      fa[ch[1][i]] = 1;
    } else {
      ch[1][i] = 1;
    }
  }
  while (!qx.empty()) {
    int x = qx.front();
    qx.pop();
    for ((i) = (0); (i) <= (25); (i)++)
      if (ch[x][i]) {
        fa[ch[x][i]] = ch[fa[x]][i];
        sz[ch[x][i]] += sz[fa[ch[x][i]]];
        qx.push(ch[x][i]);
      } else
        ch[x][i] = ch[fa[x]][i];
  }
}
void solve(int *a) {
  cnt = 1;
  clear(1);
  int i;
  for (((i)) = (1); ((i)) <= ((n)); ((i))++) {
    ins(s[i]);
  }
  rebuild();
  int x = 1;
  for (i = 0; i < st.length(); i++) {
    x = ch[x][st[i] - 'a'];
    a[i] = sz[x];
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  ;
  cin >> st;
  cin >> n;
  for (((i)) = (1); ((i)) <= ((n)); ((i))++) {
    cin >> s[i];
  }
  solve(pre);
  reverse(st.begin(), st.end());
  for (((i)) = (1); ((i)) <= ((n)); ((i))++) {
    reverse(s[i].begin(), s[i].end());
  }
  solve(suf);
  reverse(suf, suf + st.size());
  for (((i)) = (1); ((i)) <= ((st.size() - 1)); ((i))++) {
    ans += 1ll * pre[i - 1] * suf[i];
  }
  cout << ans << endl;
  return 0;
}

### Prompt

Construct a cpp code solution to the problem outlined:

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;
inline void read(int &x) {
  int v = 0, f = 1;
  char c = getchar();
  while (!isdigit(c) && c != '-') c = getchar();
  if (c == '-')
    f = -1;
  else
    v = (c & 15);
  while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15);
  x = v * f;
}
inline void read(long long &x) {
  long long v = 0ll, f = 1ll;
  char c = getchar();
  while (!isdigit(c) && c != '-') c = getchar();
  if (c == '-')
    f = -1;
  else
    v = (c & 15);
  while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15);
  x = v * f;
}
inline void readc(char &x) {
  char c;
  while (((c = getchar()) == ' ') || c == '\n')
    ;
  x = c;
}
inline void writes(string s) { puts(s.c_str()); }
inline void writeln() { writes(""); }
inline void writei(int x) {
  if (x < 0) {
    putchar('-');
    x = abs(x);
  }
  if (!x) putchar('0');
  char a[25];
  int top = 0;
  while (x) {
    a[++top] = (x % 10) + '0';
    x /= 10;
  }
  while (top) {
    putchar(a[top]);
    top--;
  }
}
inline void writell(long long x) {
  if (x < 0) {
    putchar('-');
    x = abs(x);
  }
  if (!x) putchar('0');
  char a[25];
  int top = 0;
  while (x) {
    a[++top] = (x % 10) + '0';
    x /= 10;
  }
  while (top) {
    putchar(a[top]);
    top--;
  }
}
int n, m, i, j, pre[200005], suf[200005], fa[200005], sz[200005],
    ch[200005][26], cnt;
long long ans;
string st, s[200005];
void clear(int x) {
  fa[x] = sz[x] = 0;
  memset((ch[x]), (0), (sizeof((ch[x]))));
}
void ins(string st) {
  int x = 1, i;
  for (i = 0; i < st.length(); i++) {
    if (!ch[x][st[i] - 'a']) {
      ch[x][st[i] - 'a'] = ++cnt;
      clear(cnt);
    }
    x = ch[x][st[i] - 'a'];
  }
  sz[x]++;
}
void rebuild() {
  queue<int> qx;
  int i;
  fa[1] = 1;
  for ((i) = (0); (i) <= (25); (i)++) {
    if (ch[1][i]) {
      qx.push(ch[1][i]);
      fa[ch[1][i]] = 1;
    } else {
      ch[1][i] = 1;
    }
  }
  while (!qx.empty()) {
    int x = qx.front();
    qx.pop();
    for ((i) = (0); (i) <= (25); (i)++)
      if (ch[x][i]) {
        fa[ch[x][i]] = ch[fa[x]][i];
        sz[ch[x][i]] += sz[fa[ch[x][i]]];
        qx.push(ch[x][i]);
      } else
        ch[x][i] = ch[fa[x]][i];
  }
}
void solve(int *a) {
  cnt = 1;
  clear(1);
  int i;
  for (((i)) = (1); ((i)) <= ((n)); ((i))++) {
    ins(s[i]);
  }
  rebuild();
  int x = 1;
  for (i = 0; i < st.length(); i++) {
    x = ch[x][st[i] - 'a'];
    a[i] = sz[x];
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  ;
  cin >> st;
  cin >> n;
  for (((i)) = (1); ((i)) <= ((n)); ((i))++) {
    cin >> s[i];
  }
  solve(pre);
  reverse(st.begin(), st.end());
  for (((i)) = (1); ((i)) <= ((n)); ((i))++) {
    reverse(s[i].begin(), s[i].end());
  }
  solve(suf);
  reverse(suf, suf + st.size());
  for (((i)) = (1); ((i)) <= ((st.size() - 1)); ((i))++) {
    ans += 1ll * pre[i - 1] * suf[i];
  }
  cout << ans << endl;
  return 0;
}

```