#include <bits/stdc++.h>
using namespace std;
static const int maxn = 3e5 + 10;
int Next[27][maxn];
int End[maxn];
int sz;
int Num[maxn];
int backPoint[maxn];
bool created[maxn];
vector<int> out[maxn];
vector<int> patternIndices[maxn];
int start[maxn], endd[maxn];
string text;
string pattern[maxn];
map<string, int> mapper;
int Counter[maxn];
void insertPattern(const string &pat, const int id) {
  int v = 0;
  for (char ch : pat) {
    int val = ch - 'a';
    if (!created[Next[val][v]]) {
      ++sz;
      Next[val][v] = sz;
      created[sz] = 1;
    }
    v = Next[val][v];
  }
  patternIndices[v].emplace_back(id);
}
void findBackPointer() {
  queue<int> pq;
  int v = 0;
  for (int i = 0; i < 26; i++) {
    if (Next[i][v]) {
      pq.emplace(Next[i][v]);
      backPoint[Next[i][v]] = v;
      out[v].emplace_back(Next[i][v]);
    }
  }
  while (!pq.empty()) {
    int u = pq.front();
    pq.pop();
    for (int i = 0; i < 26; i++) {
      int v = Next[i][u];
      if (!v) continue;
      int w = backPoint[u];
      while (w && Next[i][w] == 0) w = backPoint[w];
      backPoint[v] = Next[i][w];
      out[Next[i][w]].push_back(v);
      for (int p : patternIndices[Next[i][w]]) patternIndices[v].push_back(p);
      pq.emplace(v);
    }
  }
}
void insertText(const string &text) {
  int v = 0;
  for (int i = 0; i < text.size(); i++) {
    int val = text[i] - 'a';
    while (v && Next[val][v] == 0) v = backPoint[v];
    v = Next[val][v];
    for (int p : patternIndices[v]) {
      int st = i - pattern[p].size() + 1;
      start[st] += Counter[p];
      endd[i] += Counter[p];
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout << fixed << setprecision(12);
  cin >> text;
  int q;
  cin >> q;
  int ptr = 0;
  for (int i = 1; i <= q; i++) {
    string pat;
    cin >> pat;
    if (mapper.find(pat) == mapper.end()) {
      mapper[pat] = ptr;
      pattern[ptr] = pat;
      insertPattern(pat, ptr);
      ++ptr;
    }
    int id = mapper[pat];
    Counter[id]++;
  }
  findBackPointer();
  insertText(text);
  long long int ans = 0;
  int lenText = text.size();
  for (int i = 0; i < lenText - 1; i++) {
    ans += 1LL * endd[i] * start[i + 1];
  }
  cout << ans << '\n';
}

### Prompt

Create a solution in CPP for 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;
static const int maxn = 3e5 + 10;
int Next[27][maxn];
int End[maxn];
int sz;
int Num[maxn];
int backPoint[maxn];
bool created[maxn];
vector<int> out[maxn];
vector<int> patternIndices[maxn];
int start[maxn], endd[maxn];
string text;
string pattern[maxn];
map<string, int> mapper;
int Counter[maxn];
void insertPattern(const string &pat, const int id) {
  int v = 0;
  for (char ch : pat) {
    int val = ch - 'a';
    if (!created[Next[val][v]]) {
      ++sz;
      Next[val][v] = sz;
      created[sz] = 1;
    }
    v = Next[val][v];
  }
  patternIndices[v].emplace_back(id);
}
void findBackPointer() {
  queue<int> pq;
  int v = 0;
  for (int i = 0; i < 26; i++) {
    if (Next[i][v]) {
      pq.emplace(Next[i][v]);
      backPoint[Next[i][v]] = v;
      out[v].emplace_back(Next[i][v]);
    }
  }
  while (!pq.empty()) {
    int u = pq.front();
    pq.pop();
    for (int i = 0; i < 26; i++) {
      int v = Next[i][u];
      if (!v) continue;
      int w = backPoint[u];
      while (w && Next[i][w] == 0) w = backPoint[w];
      backPoint[v] = Next[i][w];
      out[Next[i][w]].push_back(v);
      for (int p : patternIndices[Next[i][w]]) patternIndices[v].push_back(p);
      pq.emplace(v);
    }
  }
}
void insertText(const string &text) {
  int v = 0;
  for (int i = 0; i < text.size(); i++) {
    int val = text[i] - 'a';
    while (v && Next[val][v] == 0) v = backPoint[v];
    v = Next[val][v];
    for (int p : patternIndices[v]) {
      int st = i - pattern[p].size() + 1;
      start[st] += Counter[p];
      endd[i] += Counter[p];
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout << fixed << setprecision(12);
  cin >> text;
  int q;
  cin >> q;
  int ptr = 0;
  for (int i = 1; i <= q; i++) {
    string pat;
    cin >> pat;
    if (mapper.find(pat) == mapper.end()) {
      mapper[pat] = ptr;
      pattern[ptr] = pat;
      insertPattern(pat, ptr);
      ++ptr;
    }
    int id = mapper[pat];
    Counter[id]++;
  }
  findBackPointer();
  insertText(text);
  long long int ans = 0;
  int lenText = text.size();
  for (int i = 0; i < lenText - 1; i++) {
    ans += 1LL * endd[i] * start[i + 1];
  }
  cout << ans << '\n';
}

```