← Home
#include <bits/stdc++.h>
using namespace std;
 
const int INF = 1e9;
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
 
    string s;
    cin >> s;
    int n = s.size();
    vector<int> str(n + 1);
    for (int i = 1; i <= n; ++i) {
        str[i] = s[i - 1] - 'a';
    }
 
    // Precompute next occurrence for each character at each position
    vector<vector<int>> nxt(26, vector<int>(n + 2, n + 1));
    for (int c = 0; c < 26; ++c) {
        nxt[c][n + 1] = n + 1;
    }
    for (int i = n; i >= 1; --i) {
        for (int c = 0; c < 26; ++c) {
            nxt[c][i] = nxt[c][i + 1];
        }
        nxt[str[i]][i] = i;
    }
 
    int m;
    cin >> m;
    unordered_map<int, vector<int>> mask_to_queries;
    vector<int> ans(m, 0);
    for (int i = 0; i < m; ++i) {
        string c;
        cin >> c;
        int mask = 0;
        for (char ch : c) {
            mask |= 1 << (ch - 'a');
        }
        mask_to_queries[mask].push_back(i);
    }
 
    // Enumerate all maximal segments
    for (int l = 1; l <= n; ++l) {
        int S = 1 << str[l];
        int start = l;
        while (start <= n) {
            // Find the first position > start where a character not in S appears
            int p = n + 1;
            for (int c = 0; c < 26; ++c) {
                if (!(S & (1 << c))) {
                    int cand = nxt[c][start + 1];
                    if (cand == start + 1) {
                        p = start + 1;
                        break;
                    }
                    if (cand < p) p = cand;
                }
            }
            int segment_end = (p == n + 1) ? n : (p - 1);
            // Check left boundary condition
            if (l == 1 || !(S & (1 << str[l - 1]))) {
                auto it = mask_to_queries.find(S);
                if (it != mask_to_queries.end()) {
                    for (int idx : it->second) {
                        ans[idx]++;
                    }
                }
            }
            if (p == n + 1) break;
            // Extend the segment by including the character at position p
            S |= (1 << str[p]);
            start = p;
        }
    }
 
    for (int i = 0; i < m; ++i) {
        cout << ans[i] << "\n";
    }
 
    return 0;
}