Language: C++

#include <bits/stdc++.h>
using namespace std;
#define fin "qualB_E.inp"
#define fou ""
#define ff(i, a, b) for(int i = a; i <= b; i ++)
#define fd(i, a, b) for(int i = a; i >= b; i --)
#define x first
#define y second
//#define endl '\n'
typedef long long data;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
const int N = 1e5 + 10;
const int M = 5e5;

int n, q, k;
string s[N], p;
int num, next[M][26], endstr[M], f[M];

void read()
{
    cin >> n;
    ff(i, 1, n) cin >> s[i];
    cin >> q;
}
void build()
{
    num = 1;
    int u, v;
    ff(i, 1, n)
    {
        u = 1;
        ff(j, 1, s[i].size())
        {
            if (!next[u][s[i][j - 1] - 'a'])
                next[u][s[i][j - 1] - 'a'] = ++ num;
            u = next[u][s[i][j - 1] - 'a'];
        }
        endstr[u] = i;
    }
}
void visit(int u)
{
    int v;
    if (endstr[u]) f[u] = 1;
    ff(c, 0, 25)
    {
        v = next[u][c];
        if (v) visit(v);
        f[u] += f[v];
    }
}
void solve()
{
    build();
    visit(1);
    int u, v, c, ans;
    while (q --)
    {
        cin >> k >> p;
        u = 1;
        ans = 0;
        ff(i, 1, s[k].size())
        {
            ff(j, 1, p.size())
            {
                c = p[j - 1] - 'a';
                v = next[u][c];
                if (c != s[k][i - 1] - 'a') ans += f[v];
                    else {
                        //cout << (char) (c + 'a') << " " << v << endl;
                        u = v;
                        if (endstr[u]) ++ ans;
                        break;
                    }
            }
        }
        cout << ans << endl;
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    //freopen(fin,"r",stdin), freopen(fou,"w",stdout);

    read();
    solve();
    return 0;
}


Compiler output:
a.cc: In function 'void build()':
a.cc:36:18: error: reference to 'next' is ambiguous
   36 |             if (!next[u][s[i][j - 1] - 'a'])
      |                  ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:19:10: note:                 'int next [500000][26]'
   19 | int num, next[M][26], endstr[M], f[M];
      |          ^~~~
a.cc:37:17: error: reference to 'next' is ambiguous
   37 |                 next[u][s[i][j - 1] - 'a'] = ++ num;
      |                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:19:10: note:                 'int next [500000][26]'
   19 | int num, next[M][26], endstr[M], f[M];
      |          ^~~~
a.cc:38:17: error: reference to 'next' is ambiguous
   38 |             u = next[u][s[i][j - 1] - 'a'];
      |                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:19:10: note:                 'int next [500000][26]'
   19 | int num, next[M][26], endstr[M], f[M];
      |          ^~~~
a.cc: In function 'void visit(int)':
a.cc:49:13: error: reference to 'next' is ambiguous
   49 |         v = next[u][c];
      |             ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:19:10: note:                 'int next [500000][26]'
   19 | int num, next[M][26], endstr[M], f[M];
      |          ^~~~
a.cc: In function 'void solve()':
a.cc:69:21: error: reference to 'next' is ambiguous
   69 |                 v = next[u][c];
      |                     ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:19:10: note:                 'int next [500000][26]'
   19 | int num, next[M][26], endstr[M], f[M];
      |          ^~~~