#include <bits/stdc++.h>
using namespace std;
int const nax = 2e5 + 10;
struct Aho_Corasick {
  static const int ALPHA = 26;
  int nxt[nax][ALPHA] = {};
  int link[nax] = {};
  int val[nax] = {};
  int top = 0;
  void add_string(string const &s) {
    int v = 0;
    for (char ch : s) {
      int c = ch - 'a';
      if (nxt[v][c]) {
        v = nxt[v][c];
      } else {
        nxt[v][c] = ++top;
        v = top;
      }
    }
    ++val[v];
  }
  void get_link() {
    static int q[nax];
    int topQ = 0;
    q[topQ++] = 0;
    for (int _i = 0; _i < topQ; ++_i) {
      int cur = q[_i];
      val[cur] += val[link[cur]];
      for (int i = 0; i < ALPHA; ++i) {
        if (nxt[cur][i]) {
          link[nxt[cur][i]] = cur ? nxt[link[cur]][i] : 0;
          q[topQ++] = nxt[cur][i];
        } else {
          nxt[cur][i] = nxt[link[cur]][i];
        }
      }
    }
  }
  void get_cnt(int *ct, string const &s) {
    int v = 0;
    for (int i = 0; i < (int)s.size(); ++i) {
      v = nxt[v][s[i] - 'a'];
      ct[i] = val[v];
    }
  }
};
Aho_Corasick pref, suf;
int ct[2][nax];
int main() {
  cin.tie(0)->sync_with_stdio(false);
  string t;
  cin >> t;
  int n;
  string s;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> s;
    pref.add_string(s);
    reverse(s.begin(), s.end());
    suf.add_string(s);
  }
  n = t.size();
  pref.get_link();
  suf.get_link();
  pref.get_cnt(ct[0], t);
  reverse(t.begin(), t.end());
  suf.get_cnt(ct[1], t);
  reverse(ct[1], ct[1] + n);
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    ans += 1LL * ct[0][i] * ct[1][i + 1];
  }
  cout << ans << '\n';
}

### 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;
int const nax = 2e5 + 10;
struct Aho_Corasick {
  static const int ALPHA = 26;
  int nxt[nax][ALPHA] = {};
  int link[nax] = {};
  int val[nax] = {};
  int top = 0;
  void add_string(string const &s) {
    int v = 0;
    for (char ch : s) {
      int c = ch - 'a';
      if (nxt[v][c]) {
        v = nxt[v][c];
      } else {
        nxt[v][c] = ++top;
        v = top;
      }
    }
    ++val[v];
  }
  void get_link() {
    static int q[nax];
    int topQ = 0;
    q[topQ++] = 0;
    for (int _i = 0; _i < topQ; ++_i) {
      int cur = q[_i];
      val[cur] += val[link[cur]];
      for (int i = 0; i < ALPHA; ++i) {
        if (nxt[cur][i]) {
          link[nxt[cur][i]] = cur ? nxt[link[cur]][i] : 0;
          q[topQ++] = nxt[cur][i];
        } else {
          nxt[cur][i] = nxt[link[cur]][i];
        }
      }
    }
  }
  void get_cnt(int *ct, string const &s) {
    int v = 0;
    for (int i = 0; i < (int)s.size(); ++i) {
      v = nxt[v][s[i] - 'a'];
      ct[i] = val[v];
    }
  }
};
Aho_Corasick pref, suf;
int ct[2][nax];
int main() {
  cin.tie(0)->sync_with_stdio(false);
  string t;
  cin >> t;
  int n;
  string s;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> s;
    pref.add_string(s);
    reverse(s.begin(), s.end());
    suf.add_string(s);
  }
  n = t.size();
  pref.get_link();
  suf.get_link();
  pref.get_cnt(ct[0], t);
  reverse(t.begin(), t.end());
  suf.get_cnt(ct[1], t);
  reverse(ct[1], ct[1] + n);
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    ans += 1LL * ct[0][i] * ct[1][i + 1];
  }
  cout << ans << '\n';
}

```