#include <bits/stdc++.h>
using namespace std;
const int M = 1000000007;
const int MM = 998244353;
const long double PI = acos(-1);
template <typename T, typename U>
static inline void amin(T &x, U y) {
  if (y < x) x = y;
}
template <typename T, typename U>
static inline void amax(T &x, U y) {
  if (x < y) x = y;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << '(' << p.first << "," << p.second << ')';
}
const int N = 200005;
const int LEN = 400005;
vector<int> g[LEN];
struct SA {
  struct state {
    int len, link;
    map<char, int> next;
  };
  state st[LEN];
  int sz, last;
  int cnt[LEN];
  bitset<LEN> vis;
  int idx[LEN];
  void SA_make() {
    sz = 0;
    st[0].len = 0, st[0].link = -1;
    ++sz;
    last = 0;
  }
  SA() { SA_make(); }
  void add(char c) {
    int new_n = sz++;
    st[new_n].len = st[last].len + 1;
    int pt = last;
    while (pt != -1 && !st[pt].next.count(c)) {
      st[pt].next[c] = new_n;
      pt = st[pt].link;
    }
    if (pt == -1) {
      st[new_n].link = 0;
    } else {
      int q_st = st[pt].next[c];
      if (st[q_st].len == st[pt].len + 1)
        st[new_n].link = q_st;
      else {
        int clone = sz++;
        st[clone] = st[q_st];
        st[clone].len = st[pt].len + 1;
        st[q_st].link = st[new_n].link = clone;
        while (pt != -1 && st[pt].next[c] == q_st) {
          st[pt].next[c] = clone;
          pt = st[pt].link;
        }
      }
    }
    last = new_n;
  }
  int minlen(int u) {
    if (u == 0) return 0;
    return st[st[u].link].len + 1;
  }
  void build(string &s) {
    int id = 1;
    for (char c : s) {
      add(c);
      idx[last] = id;
      ++id;
    }
    for (int i = 1; i < sz; ++i) g[st[i].link].push_back(i);
  }
  void go_(string &s) {
    int cur = 0;
    int cntr = 0;
    for (auto c : s) {
      if (st[cur].next.count(c))
        cur = st[cur].next[c];
      else
        break;
      ++cntr;
    }
    if (cntr == (long long)s.size()) ++cnt[cur];
  }
  void clear() {
    last = 0;
    for (int i = 0; i < sz; ++i) {
      st[i].next.clear();
      g[i].clear();
      cnt[i] = idx[i] = 0;
    }
    sz = 1;
    vis.reset();
  }
} sa;
long long st[N][2];
int cnt = 0;
void dfs(int u, long long val, int id) {
  if (sa.vis[u]) return;
  sa.vis[u] = 1;
  val += sa.cnt[u];
  if (sa.idx[u]) st[sa.idx[u]][id] += val;
  cnt += sa.idx[u] > 0;
  for (auto j : g[u]) {
    dfs(j, val, id);
  }
}
int _runtimeTerror_() {
  string t;
  cin >> t;
  int n;
  cin >> n;
  sa.build(t);
  vector<string> v;
  for (int i = 1; i <= n; ++i) {
    string s;
    cin >> s;
    v.push_back(s);
    sa.go_(s);
  }
  dfs(0, 0, 1);
  sa.clear();
  reverse(t.begin(), t.end());
  sa.build(t);
  for (auto &j : v) {
    reverse(j.begin(), j.end());
    sa.go_(j);
  }
  assert(cnt == (long long)t.size());
  dfs(0, 0, 0);
  for (int i = 1; i <= (long long)t.size() / 2; ++i)
    swap(st[i][0], st[(long long)t.size() + 1 - i][0]);
  long long ans = 0;
  for (int i = 1; i <= (long long)t.size() - 1; ++i) {
    ans += st[i][1] * st[i + 1][0];
  }
  assert(cnt == 2 * (long long)t.size());
  cout << ans << "\n";
  return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  while (TESTS--) _runtimeTerror_();
  return 0;
}

### Prompt

Develop a solution in cpp to the problem described below:

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 int M = 1000000007;
const int MM = 998244353;
const long double PI = acos(-1);
template <typename T, typename U>
static inline void amin(T &x, U y) {
  if (y < x) x = y;
}
template <typename T, typename U>
static inline void amax(T &x, U y) {
  if (x < y) x = y;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << '(' << p.first << "," << p.second << ')';
}
const int N = 200005;
const int LEN = 400005;
vector<int> g[LEN];
struct SA {
  struct state {
    int len, link;
    map<char, int> next;
  };
  state st[LEN];
  int sz, last;
  int cnt[LEN];
  bitset<LEN> vis;
  int idx[LEN];
  void SA_make() {
    sz = 0;
    st[0].len = 0, st[0].link = -1;
    ++sz;
    last = 0;
  }
  SA() { SA_make(); }
  void add(char c) {
    int new_n = sz++;
    st[new_n].len = st[last].len + 1;
    int pt = last;
    while (pt != -1 && !st[pt].next.count(c)) {
      st[pt].next[c] = new_n;
      pt = st[pt].link;
    }
    if (pt == -1) {
      st[new_n].link = 0;
    } else {
      int q_st = st[pt].next[c];
      if (st[q_st].len == st[pt].len + 1)
        st[new_n].link = q_st;
      else {
        int clone = sz++;
        st[clone] = st[q_st];
        st[clone].len = st[pt].len + 1;
        st[q_st].link = st[new_n].link = clone;
        while (pt != -1 && st[pt].next[c] == q_st) {
          st[pt].next[c] = clone;
          pt = st[pt].link;
        }
      }
    }
    last = new_n;
  }
  int minlen(int u) {
    if (u == 0) return 0;
    return st[st[u].link].len + 1;
  }
  void build(string &s) {
    int id = 1;
    for (char c : s) {
      add(c);
      idx[last] = id;
      ++id;
    }
    for (int i = 1; i < sz; ++i) g[st[i].link].push_back(i);
  }
  void go_(string &s) {
    int cur = 0;
    int cntr = 0;
    for (auto c : s) {
      if (st[cur].next.count(c))
        cur = st[cur].next[c];
      else
        break;
      ++cntr;
    }
    if (cntr == (long long)s.size()) ++cnt[cur];
  }
  void clear() {
    last = 0;
    for (int i = 0; i < sz; ++i) {
      st[i].next.clear();
      g[i].clear();
      cnt[i] = idx[i] = 0;
    }
    sz = 1;
    vis.reset();
  }
} sa;
long long st[N][2];
int cnt = 0;
void dfs(int u, long long val, int id) {
  if (sa.vis[u]) return;
  sa.vis[u] = 1;
  val += sa.cnt[u];
  if (sa.idx[u]) st[sa.idx[u]][id] += val;
  cnt += sa.idx[u] > 0;
  for (auto j : g[u]) {
    dfs(j, val, id);
  }
}
int _runtimeTerror_() {
  string t;
  cin >> t;
  int n;
  cin >> n;
  sa.build(t);
  vector<string> v;
  for (int i = 1; i <= n; ++i) {
    string s;
    cin >> s;
    v.push_back(s);
    sa.go_(s);
  }
  dfs(0, 0, 1);
  sa.clear();
  reverse(t.begin(), t.end());
  sa.build(t);
  for (auto &j : v) {
    reverse(j.begin(), j.end());
    sa.go_(j);
  }
  assert(cnt == (long long)t.size());
  dfs(0, 0, 0);
  for (int i = 1; i <= (long long)t.size() / 2; ++i)
    swap(st[i][0], st[(long long)t.size() + 1 - i][0]);
  long long ans = 0;
  for (int i = 1; i <= (long long)t.size() - 1; ++i) {
    ans += st[i][1] * st[i + 1][0];
  }
  assert(cnt == 2 * (long long)t.size());
  cout << ans << "\n";
  return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  while (TESTS--) _runtimeTerror_();
  return 0;
}

```