Language: C++

#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,abm,mmx,popcnt,avx,avx2,tune=native")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

/*
    zet:
        find_by_order(k): k-stat [k > 0]
        order_of_key(k) : {el < k}

    gp_hash_table<key, val, custom_hash>

    rope<T>:
        iterators: mutable_begin, ..
        methods  : erase, insert, substr, ..
*/

#define all(x) (x).begin(), (x).end()
#define size(x) (int)((x).size())
#define em_back emplace_back

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template<typename T>
using zet = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

mt19937 eng(chrono::steady_clock::now().time_since_epoch().count());

const int N = 1e5 + 4;
const int S = 4e5 + 4;
const int ABC = 'z' - 'a' + 1;

struct Node {
    vector<pii> vto;
    int to[ABC];
    int link;
    int leaf;
    int cnt;
    int soon;

    Node() {
        memset(to, -1, sizeof to);
        link = -1;
        leaf = -1;
        cnt = 0;
        soon = 0;
    }
};

int n, it;
Node bor[S];
string b[N];

void add(string& s, int num) {
    int u = 0;
    for (char c : s) {
        int& to = bor[u].to[c - 'a'];
        if (to == -1) {
            to = ++it;
        }
        u = to;
    }
    bor[u].leaf = num;
}

void read() {
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> b[i];
        add(b[i], i + 1);
    }
}

int h[S];

void dfs(int u, int hu) {
    int& res = bor[u].cnt;
    h[u] = hu;
    res += (bor[u].leaf != -1);
    for (int to = 0; to < ABC; ++to) {
        int v = bor[u].to[to];
        if (v != -1) {
            bor[u].vto.em_back(to, v);
            dfs(v, hu + 1);
        }
        res += bor[v].cnt;
        bor[u].soon++;
    }

    if (bor[u].soon == 1) {
        for (int to = 0; to < ABC; ++to) {
            int v = bor[u].to[to];
            if (v != -1) {
                if (bor[v].leaf != -1 || bor[v].soon > 1) {
                    bor[u].link = v;
                } else {
                    bor[u].link = bor[v].link;
                }
                break;
            }
        }
    }
}

int pos['z' + 4];

int calc(int k, string& p) {
    --k;
    int res = 0, u = 0;

    for (int i = 0; i < ABC; ++i) {
        pos[p[i]] = i;
    }

    for (int i = 0; i < size(b[k]);) {
        res += (bor[u].leaf != -1);
        if (bor[u].soon == 1) {
            int v = bor[u].link;
            i += h[v] - h[u];
            u = v;
            continue;
        }

        int board = pos[b[k][i]];
        for (pii to : bor[u].vto) {
            if (pos[to.first + 'a'] < board) {
                res += bor[to.second].cnt;
            }
        }
        u = bor[u].to[b[k][i] - 'a'];
        ++i;
    }
    return res + 1;
}

void solve() {
    read();
    dfs(0, 0);

    int q;
    cin >> q;
    while (q--) {
        int k;
        string p;
        cin >> k >> p;
        cout << calc(k, p) << "\n";
    }
}

int main() {
    ios::sync_with_stdio(0);
    cout.tie(0), cin.tie(0);

    int z = 1;
    /// cin >> z;

    while (z--) {
        solve();
    }

    return 0;
}


Compiler output:
In file included from /usr/include/c++/14/string:43,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:4:
/usr/include/c++/14/bits/allocator.h: In destructor 'std::__cxx11::basic_string<char>::_Alloc_hider::~_Alloc_hider()':
/usr/include/c++/14/bits/allocator.h:182:7: error: inlining failed in call to 'always_inline' 'std::allocator< <template-parameter-1-1> >::~allocator() noexcept [with _Tp = char]': target specific option mismatch
  182 |       ~allocator() _GLIBCXX_NOTHROW { }
      |       ^
In file included from /usr/include/c++/14/string:54:
/usr/include/c++/14/bits/basic_string.h:186:14: note: called from here
  186 |       struct _Alloc_hider : allocator_type // TODO check __is_final
      |              ^~~~~~~~~~~~