#include <bits/stdc++.h>
using namespace std;
struct AhoCorasick {
  enum { alpha = 26, first = 'a' };
  struct Node {
    int back, next[alpha], start = -1, end = -1, nmatches = 0;
    Node(int v) { memset(next, v, sizeof(next)); }
  };
  vector<Node> N;
  vector<int> backp;
  void insert(string& s, int j) {
    assert(!s.empty());
    int n = 0;
    for (auto& c : s) {
      int& m = N[n].next[c - first];
      if (m == -1) {
        n = m = (int)(N).size();
        N.emplace_back(-1);
      } else
        n = m;
    }
    if (N[n].end == -1) N[n].start = j;
    backp.push_back(N[n].end);
    N[n].end = j;
    N[n].nmatches++;
  }
  AhoCorasick(vector<string>& pat) {
    N.emplace_back(-1);
    for (int i = 0; i < ((int)(pat).size()); ++i) insert(pat[i], i);
    N[0].back = (int)(N).size();
    N.emplace_back(0);
    queue<int> q;
    for (q.push(0); !q.empty(); q.pop()) {
      int n = q.front(), prev = N[n].back;
      for (int i = 0; i < (alpha); ++i) {
        int &ed = N[n].next[i], y = N[prev].next[i];
        if (ed == -1)
          ed = y;
        else {
          N[ed].back = y;
          (N[ed].end == -1 ? N[ed].end : backp[N[ed].start]) = N[y].end;
          N[ed].nmatches += N[y].nmatches;
          q.push(ed);
        }
      }
    }
  }
  vector<int> find(string word) {
    int n = 0;
    vector<int> res;
    for (auto& c : word) {
      n = N[n].next[c - first];
      res.push_back(N[n].end);
    }
    return res;
  }
  vector<vector<int> > findAll(vector<string>& pat, string word) {
    vector<int> r = find(word);
    vector<vector<int> > res((int)(word).size());
    for (int i = 0; i < ((int)(word).size()); ++i) {
      int ind = r[i];
      while (ind != -1) {
        res[i - (int)(pat[ind]).size() + 1].push_back(ind);
        ind = backp[ind];
      }
    }
    return res;
  }
  void solve(string word, long long* count) {
    int n = 0;
    for (int i = 0; i < word.size(); i++) {
      n = N[n].next[word[i] - first];
      count[i + 1] = N[n].nmatches;
    }
  }
};
const int nax = 2e5 + 10;
long long endsat[nax];
long long startsat[nax];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  string t;
  cin >> t;
  int n;
  cin >> n;
  vector<string> pat(n);
  for (int i = 0; i < n; i++) cin >> pat[i];
  AhoCorasick A(pat);
  A.solve(t, endsat);
  for (int i = 0; i < n; i++) reverse(pat[i].begin(), pat[i].end());
  AhoCorasick B(pat);
  reverse(t.begin(), t.end());
  B.solve(t, startsat);
  int m = t.size();
  long long ans = 0;
  for (int i = 0; i <= m; i++) {
    ans += (long long)startsat[m - i] * endsat[i];
  }
  cout << ans << endl;
}

### 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;
struct AhoCorasick {
  enum { alpha = 26, first = 'a' };
  struct Node {
    int back, next[alpha], start = -1, end = -1, nmatches = 0;
    Node(int v) { memset(next, v, sizeof(next)); }
  };
  vector<Node> N;
  vector<int> backp;
  void insert(string& s, int j) {
    assert(!s.empty());
    int n = 0;
    for (auto& c : s) {
      int& m = N[n].next[c - first];
      if (m == -1) {
        n = m = (int)(N).size();
        N.emplace_back(-1);
      } else
        n = m;
    }
    if (N[n].end == -1) N[n].start = j;
    backp.push_back(N[n].end);
    N[n].end = j;
    N[n].nmatches++;
  }
  AhoCorasick(vector<string>& pat) {
    N.emplace_back(-1);
    for (int i = 0; i < ((int)(pat).size()); ++i) insert(pat[i], i);
    N[0].back = (int)(N).size();
    N.emplace_back(0);
    queue<int> q;
    for (q.push(0); !q.empty(); q.pop()) {
      int n = q.front(), prev = N[n].back;
      for (int i = 0; i < (alpha); ++i) {
        int &ed = N[n].next[i], y = N[prev].next[i];
        if (ed == -1)
          ed = y;
        else {
          N[ed].back = y;
          (N[ed].end == -1 ? N[ed].end : backp[N[ed].start]) = N[y].end;
          N[ed].nmatches += N[y].nmatches;
          q.push(ed);
        }
      }
    }
  }
  vector<int> find(string word) {
    int n = 0;
    vector<int> res;
    for (auto& c : word) {
      n = N[n].next[c - first];
      res.push_back(N[n].end);
    }
    return res;
  }
  vector<vector<int> > findAll(vector<string>& pat, string word) {
    vector<int> r = find(word);
    vector<vector<int> > res((int)(word).size());
    for (int i = 0; i < ((int)(word).size()); ++i) {
      int ind = r[i];
      while (ind != -1) {
        res[i - (int)(pat[ind]).size() + 1].push_back(ind);
        ind = backp[ind];
      }
    }
    return res;
  }
  void solve(string word, long long* count) {
    int n = 0;
    for (int i = 0; i < word.size(); i++) {
      n = N[n].next[word[i] - first];
      count[i + 1] = N[n].nmatches;
    }
  }
};
const int nax = 2e5 + 10;
long long endsat[nax];
long long startsat[nax];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  string t;
  cin >> t;
  int n;
  cin >> n;
  vector<string> pat(n);
  for (int i = 0; i < n; i++) cin >> pat[i];
  AhoCorasick A(pat);
  A.solve(t, endsat);
  for (int i = 0; i < n; i++) reverse(pat[i].begin(), pat[i].end());
  AhoCorasick B(pat);
  reverse(t.begin(), t.end());
  B.solve(t, startsat);
  int m = t.size();
  long long ans = 0;
  for (int i = 0; i <= m; i++) {
    ans += (long long)startsat[m - i] * endsat[i];
  }
  cout << ans << endl;
}

```