#include <bits/stdc++.h>
using namespace std;
void count_sort(vector<long long> &pos, vector<long long> &rank) {
  long long n = pos.size();
  vector<vector<long long>> cnt(n);
  for (long long i : pos) cnt[rank[i]].push_back(i);
  for (long long i = 0, idx = 0; i < n; i++)
    for (auto x : cnt[i]) pos[idx++] = x;
}
void get_suffix(string s, vector<long long> &pos) {
  s += '$';
  long long n = s.size();
  vector<long long> rank(n);
  iota(pos.begin(), pos.end(), 0);
  sort(pos.begin(), pos.end(),
       [&](long long a, long long b) { return s[a] < s[b]; });
  for (long long i = 0; i < n; i++) {
    if (i == 0) {
      rank[pos[i]] = 0;
    } else {
      rank[pos[i]] = rank[pos[i - 1]] + (s[pos[i]] != s[pos[i - 1]]);
    }
  }
  vector<long long> new_rank(n);
  for (long long k = 0; (1 << k) <= n; k++) {
    for (long long i = 0; i < n; i++) pos[i] = (pos[i] - (1 << k) % n + n) % n;
    count_sort(pos, rank);
    new_rank[pos[0]] = 0;
    for (long long i = 1; i < n; i++) {
      pair<long long, long long> prev = {rank[pos[i - 1]],
                                         rank[(pos[i - 1] + (1 << k)) % n]};
      pair<long long, long long> now = {rank[pos[i]],
                                        rank[(pos[i] + (1 << k)) % n]};
      new_rank[pos[i]] = new_rank[pos[i - 1]] + (prev != now);
    }
    rank = new_rank;
  }
}
const long long N = 3e5 + 5;
long long bit[2][N];
void update(long long idx, long long pos, long long val) {
  pos++;
  while (pos <= N) {
    bit[idx][pos] += val;
    pos += pos & -pos;
  }
}
long long query(long long idx, long long pos) {
  pos++;
  long long res = 0;
  while (pos) {
    res += bit[idx][pos];
    pos -= pos & -pos;
  }
  return res;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  string s;
  cin >> s;
  vector<long long> SA1(s.size() + 1);
  get_suffix(s, SA1);
  string s2 = s;
  reverse(s2.begin(), s2.end());
  vector<long long> SA2(s.size() + 1);
  get_suffix(s2, SA2);
  vector<long long> SA1T(s.size() + 1);
  vector<long long> SA2T(s.size() + 1);
  for (long long i = 0; i <= s.size(); i++) {
    SA1T[SA1[i]] = i - 1;
  }
  for (long long i = 0; i <= s.size(); i++) {
    SA2T[SA2[i]] = i - 1;
  }
  long long n;
  cin >> n;
  while (n--) {
    string str;
    cin >> str;
    long long l = 1, r = s.size() + 1;
    while (l < r) {
      long long mid = l + r >> 1;
      string tmp = s.substr(SA1[mid], str.size());
      if (tmp < str) {
        l = mid + 1;
      } else {
        r = mid;
      }
    }
    long long lb1 = l;
    l = 1, r = s.size() + 1;
    while (l < r) {
      long long mid = l + r >> 1;
      string tmp = s.substr(SA1[mid], str.size());
      if (tmp <= str) {
        l = mid + 1;
      } else {
        r = mid;
      }
    }
    long long rb1 = l;
    if (lb1 < rb1) {
      update(0, lb1 - 1, 1);
      update(0, rb1 - 1, -1);
    }
    reverse(str.begin(), str.end());
    l = 1, r = s.size() + 1;
    while (l < r) {
      long long mid = l + r >> 1;
      string tmp = s2.substr(SA2[mid], str.size());
      if (tmp < str) {
        l = mid + 1;
      } else {
        r = mid;
      }
    }
    long long lb2 = l;
    l = 1, r = s.size() + 1;
    while (l < r) {
      long long mid = l + r >> 1;
      string tmp = s2.substr(SA2[mid], str.size());
      if (tmp <= str) {
        l = mid + 1;
      } else {
        r = mid;
      }
    }
    long long rb2 = l;
    if (lb2 < rb2) {
      update(1, lb2 - 1, 1);
      update(1, rb2 - 1, -1);
    }
  }
  long long ans = 0;
  for (long long i = 1; i < s.size(); i++) {
    ans += query(0, SA1T[i]) * query(1, SA2T[s.size() - i]);
  }
  cout << ans << "\n";
}

### Prompt

Please formulate 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;
void count_sort(vector<long long> &pos, vector<long long> &rank) {
  long long n = pos.size();
  vector<vector<long long>> cnt(n);
  for (long long i : pos) cnt[rank[i]].push_back(i);
  for (long long i = 0, idx = 0; i < n; i++)
    for (auto x : cnt[i]) pos[idx++] = x;
}
void get_suffix(string s, vector<long long> &pos) {
  s += '$';
  long long n = s.size();
  vector<long long> rank(n);
  iota(pos.begin(), pos.end(), 0);
  sort(pos.begin(), pos.end(),
       [&](long long a, long long b) { return s[a] < s[b]; });
  for (long long i = 0; i < n; i++) {
    if (i == 0) {
      rank[pos[i]] = 0;
    } else {
      rank[pos[i]] = rank[pos[i - 1]] + (s[pos[i]] != s[pos[i - 1]]);
    }
  }
  vector<long long> new_rank(n);
  for (long long k = 0; (1 << k) <= n; k++) {
    for (long long i = 0; i < n; i++) pos[i] = (pos[i] - (1 << k) % n + n) % n;
    count_sort(pos, rank);
    new_rank[pos[0]] = 0;
    for (long long i = 1; i < n; i++) {
      pair<long long, long long> prev = {rank[pos[i - 1]],
                                         rank[(pos[i - 1] + (1 << k)) % n]};
      pair<long long, long long> now = {rank[pos[i]],
                                        rank[(pos[i] + (1 << k)) % n]};
      new_rank[pos[i]] = new_rank[pos[i - 1]] + (prev != now);
    }
    rank = new_rank;
  }
}
const long long N = 3e5 + 5;
long long bit[2][N];
void update(long long idx, long long pos, long long val) {
  pos++;
  while (pos <= N) {
    bit[idx][pos] += val;
    pos += pos & -pos;
  }
}
long long query(long long idx, long long pos) {
  pos++;
  long long res = 0;
  while (pos) {
    res += bit[idx][pos];
    pos -= pos & -pos;
  }
  return res;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  string s;
  cin >> s;
  vector<long long> SA1(s.size() + 1);
  get_suffix(s, SA1);
  string s2 = s;
  reverse(s2.begin(), s2.end());
  vector<long long> SA2(s.size() + 1);
  get_suffix(s2, SA2);
  vector<long long> SA1T(s.size() + 1);
  vector<long long> SA2T(s.size() + 1);
  for (long long i = 0; i <= s.size(); i++) {
    SA1T[SA1[i]] = i - 1;
  }
  for (long long i = 0; i <= s.size(); i++) {
    SA2T[SA2[i]] = i - 1;
  }
  long long n;
  cin >> n;
  while (n--) {
    string str;
    cin >> str;
    long long l = 1, r = s.size() + 1;
    while (l < r) {
      long long mid = l + r >> 1;
      string tmp = s.substr(SA1[mid], str.size());
      if (tmp < str) {
        l = mid + 1;
      } else {
        r = mid;
      }
    }
    long long lb1 = l;
    l = 1, r = s.size() + 1;
    while (l < r) {
      long long mid = l + r >> 1;
      string tmp = s.substr(SA1[mid], str.size());
      if (tmp <= str) {
        l = mid + 1;
      } else {
        r = mid;
      }
    }
    long long rb1 = l;
    if (lb1 < rb1) {
      update(0, lb1 - 1, 1);
      update(0, rb1 - 1, -1);
    }
    reverse(str.begin(), str.end());
    l = 1, r = s.size() + 1;
    while (l < r) {
      long long mid = l + r >> 1;
      string tmp = s2.substr(SA2[mid], str.size());
      if (tmp < str) {
        l = mid + 1;
      } else {
        r = mid;
      }
    }
    long long lb2 = l;
    l = 1, r = s.size() + 1;
    while (l < r) {
      long long mid = l + r >> 1;
      string tmp = s2.substr(SA2[mid], str.size());
      if (tmp <= str) {
        l = mid + 1;
      } else {
        r = mid;
      }
    }
    long long rb2 = l;
    if (lb2 < rb2) {
      update(1, lb2 - 1, 1);
      update(1, rb2 - 1, -1);
    }
  }
  long long ans = 0;
  for (long long i = 1; i < s.size(); i++) {
    ans += query(0, SA1T[i]) * query(1, SA2T[s.size() - i]);
  }
  cout << ans << "\n";
}

```