#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6;
int n;
string t, s[MAXN];
int numPref[MAXN];
long long ans = 0;
struct AhoCorasick {
  const static char START = 'a';
  const static int NUM_CHAR = 26;
  struct Node {
    char ch;
    int p;
    int trie[NUM_CHAR], nex[NUM_CHAR];
    int link;
    vector<int> revLink;
    int leaf = 0;
    Node(char _ch = '$', int _p = -1) : ch(_ch), link(-1), p(_p) {
      memset(trie, -1, sizeof trie);
      memset(nex, -1, sizeof nex);
    }
  };

 public:
  vector<Node> t;
  AhoCorasick() { t.push_back(Node()); }
  void addString(string s) {
    int cur = 0;
    for (char c : s) {
      int idx = c - START;
      if (t[cur].trie[idx] == -1) {
        t[cur].trie[idx] = t.size();
        t.push_back(Node(c, cur));
      }
      cur = t[cur].trie[idx];
    }
    ++t[cur].leaf;
  }
  int getLink(int state);
  int go(int state, char ch) {
    int idx = ch - START;
    if (t[state].nex[idx] == -1) {
      if (t[state].trie[idx] == -1) {
        t[state].nex[idx] = (state == 0) ? 0 : go(getLink(state), ch);
      } else {
        t[state].nex[idx] = t[state].trie[idx];
      }
    }
    return t[state].nex[idx];
  }
  void traverse() {
    for (int i = 0; i < t.size(); ++i) {
      int link = getLink(i);
      if (link >= 0 && link != i) {
        t[link].revLink.push_back(i);
      }
    }
    queue<int> q;
    q.push(0);
    while (!q.empty()) {
      int front = q.front();
      q.pop();
      for (int i : t[front].revLink) {
        t[i].leaf += t[front].leaf;
        q.push(i);
      }
    }
  }
  int getLeaf(int state) { return t[state].leaf; }
} f, r;
int AhoCorasick::getLink(int state) {
  if (t[state].link == -1) {
    if (state == 0 || t[state].p == 0) {
      t[state].link = 0;
    } else {
      t[state].link = go(getLink(t[state].p), t[state].ch);
    }
  }
  return t[state].link;
};
int main() {
  cin >> t;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> s[i];
    f.addString(s[i]);
    reverse(s[i].begin(), s[i].end());
    r.addString(s[i]);
  }
  f.traverse();
  r.traverse();
  int state = 0;
  for (int i = 0; i < t.size(); ++i) {
    state = f.go(state, t[i]);
    numPref[i] = f.getLeaf(state);
  }
  state = 0;
  for (int i = t.size() - 1; i >= 0; --i) {
    state = r.go(state, t[i]);
    if (i > 0) {
      ans += 1LL * numPref[i - 1] * r.getLeaf(state);
    }
  }
  printf("%lld\n", ans);
}

### Prompt

Generate a Cpp solution to 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;
const int MAXN = 1e6;
int n;
string t, s[MAXN];
int numPref[MAXN];
long long ans = 0;
struct AhoCorasick {
  const static char START = 'a';
  const static int NUM_CHAR = 26;
  struct Node {
    char ch;
    int p;
    int trie[NUM_CHAR], nex[NUM_CHAR];
    int link;
    vector<int> revLink;
    int leaf = 0;
    Node(char _ch = '$', int _p = -1) : ch(_ch), link(-1), p(_p) {
      memset(trie, -1, sizeof trie);
      memset(nex, -1, sizeof nex);
    }
  };

 public:
  vector<Node> t;
  AhoCorasick() { t.push_back(Node()); }
  void addString(string s) {
    int cur = 0;
    for (char c : s) {
      int idx = c - START;
      if (t[cur].trie[idx] == -1) {
        t[cur].trie[idx] = t.size();
        t.push_back(Node(c, cur));
      }
      cur = t[cur].trie[idx];
    }
    ++t[cur].leaf;
  }
  int getLink(int state);
  int go(int state, char ch) {
    int idx = ch - START;
    if (t[state].nex[idx] == -1) {
      if (t[state].trie[idx] == -1) {
        t[state].nex[idx] = (state == 0) ? 0 : go(getLink(state), ch);
      } else {
        t[state].nex[idx] = t[state].trie[idx];
      }
    }
    return t[state].nex[idx];
  }
  void traverse() {
    for (int i = 0; i < t.size(); ++i) {
      int link = getLink(i);
      if (link >= 0 && link != i) {
        t[link].revLink.push_back(i);
      }
    }
    queue<int> q;
    q.push(0);
    while (!q.empty()) {
      int front = q.front();
      q.pop();
      for (int i : t[front].revLink) {
        t[i].leaf += t[front].leaf;
        q.push(i);
      }
    }
  }
  int getLeaf(int state) { return t[state].leaf; }
} f, r;
int AhoCorasick::getLink(int state) {
  if (t[state].link == -1) {
    if (state == 0 || t[state].p == 0) {
      t[state].link = 0;
    } else {
      t[state].link = go(getLink(t[state].p), t[state].ch);
    }
  }
  return t[state].link;
};
int main() {
  cin >> t;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> s[i];
    f.addString(s[i]);
    reverse(s[i].begin(), s[i].end());
    r.addString(s[i]);
  }
  f.traverse();
  r.traverse();
  int state = 0;
  for (int i = 0; i < t.size(); ++i) {
    state = f.go(state, t[i]);
    numPref[i] = f.getLeaf(state);
  }
  state = 0;
  for (int i = t.size() - 1; i >= 0; --i) {
    state = r.go(state, t[i]);
    if (i > 0) {
      ans += 1LL * numPref[i - 1] * r.getLeaf(state);
    }
  }
  printf("%lld\n", ans);
}

```