Link to this code: https://cses.fi/paste/952dbfd1e9b18fab386503/
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pLL pair<LL, LL>
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define rz resize
#define DEBU
#ifndef DEBUG
#define endl '\n'
#endif
void Star_Brust_Stream()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    return;
}
const LL MOD = 1e9 + 7;
const LL INF = 1e18;
vector<LL> en;
vector<LL> sum;
struct node
{
    vector<LL> alph;
    LL m;
    node()
    {
        m = 0;
        alph.resize(26);
    }
};

struct trie
{
    vector<node> v;
    vector<LL> fail;
    trie()
    {
        node temp;
        v.pb(temp);
    }
    void _insert(string s, LL k)
    {
        LL n = s.length();
        LL pos = 0;
        LL i = 0;
        for (i = 0; i < n; i++)
        {
            if (v[pos].alph[s[i] - 'a'] == 0)
            {
                node temp;
                v[pos].alph[s[i] - 'a'] = v.size();
                v.pb(temp);
            }
            pos = v[pos].alph[s[i] - 'a'];
        }
        en[k] = pos;
    }
    void ask(string s)
    {
        sum.rz(v.size());
        LL n = s.length();
        LL pos = 0;
        vector<LL> vis(v.size(), 0);
        for (int i = 0; i < n; i++)
        {
            pos = v[pos].alph[s[i] - 'a'];
            sum[pos]++;
        }
    }
    void build()
    {
        fail.rz(0);
        fail.rz(v.size());
        queue<LL> q;
        for (int i = 0; i < 26; i++)
        {
            if (v[0].alph[i])
            {
                q.push(v[0].alph[i]);
            }
        }
        while (!q.empty())
        {
            LL x = q.front();
            q.pop();
            for (int i = 0; i < 26; i++)
            {
                if (v[x].alph[i])
                {
                    fail[v[x].alph[i]] = v[fail[x]].alph[i];
                    q.push(v[x].alph[i]);
                }
                else
                {
                    v[x].alph[i] = v[fail[x]].alph[i];
                }
            }
        }
    }
};
vector<vector<LL>> p;
vector<LL> vis;
void dfs(LL pos)
{
    vis[pos] = 1;
    for (int i : p[pos])
    {
        if (vis[i])
            continue;
        dfs(i);
        sum[pos] += sum[i];
    }
}
int main()
{
    Star_Brust_Stream();
    string s;
    cin >> s;
    LL n;
    cin >> n;
    en.rz(n);
    trie tr;
    for (int i = 0; i < n; i++)
    {
        string a;
        cin >> a;
        tr._insert(a, i);
    }
    tr.build();
    tr.ask(s);
    vis.rz(tr.v.size());
    p.rz(tr.v.size());
    for (int i = 0; i < tr.v.size(); i++)
    {
        p[i].pb(tr.fail[i]);
        p[tr.fail[i]].pb(i);
    }
    dfs(0);
    for (int i = 0; i < n; i++)
    {
        cout << sum[en[i]] << endl;
    }
#ifdef DEBUG
    system("pause");
#endif
    return 0;
}