#include <bits/stdc++.h>
using namespace std;
string s;
string t[505];
vector<int> pi[505];
int mat[505];
int dp[100005];
int n;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> s >> n;
for (int i = 0; i < n; ++i) {
cin >> t[i];
int m = 0;
pi[i].assign(t[i].size(), 0);
for (int j = 1; j < t[i].size(); ++j) {
while (t[i][j] != t[i][m] && m > 0) m = pi[i][m - 1];
if (t[i][j] == t[i][m]) ++m;
pi[i][j] = m;
}
}
for (int i = 0; i < s.size(); ++i) {
dp[i+1] = max(dp[i+1], dp[i]);
for (int j = 0; j < n; ++j) {
while (s[i] != t[j][mat[j]] && mat[j] > 0) mat[j] = pi[j][mat[j] - 1];
if (s[i] == t[j][mat[j]]) ++mat[j];
if (mat[j] == t[j].size()) {
dp[i + 1] = max(dp[i + 1], dp[i - (int)t[j].size() + 1] + mat[j]);
mat[j] = pi[j][mat[j] - 1];
}
}
}
cout << dp[s.size()] << '\n';
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpzdHJpbmcgczsKc3RyaW5nIHRbNTA1XTsKdmVjdG9yPGludD4gcGlbNTA1XTsKaW50IG1hdFs1MDVdOwppbnQgZHBbMTAwMDA1XTsKaW50IG47CmludCBtYWluKCkgewogIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogIGNpbi50aWUoMCk7CiAgY2luID4+IHMgPj4gbjsKICBmb3IgKGludCBpID0gMDsgaSA8IG47ICsraSkgewogICAgY2luID4+IHRbaV07CiAgICBpbnQgbSA9IDA7CiAgICBwaVtpXS5hc3NpZ24odFtpXS5zaXplKCksIDApOwogICAgZm9yIChpbnQgaiA9IDE7IGogPCB0W2ldLnNpemUoKTsgKytqKSB7CiAgICAgIHdoaWxlICh0W2ldW2pdICE9IHRbaV1bbV0gJiYgbSA+IDApIG0gPSBwaVtpXVttIC0gMV07CiAgICAgIGlmICh0W2ldW2pdID09IHRbaV1bbV0pICsrbTsKICAgICAgcGlbaV1bal0gPSBtOwogICAgfQogIH0KCiAgZm9yIChpbnQgaSA9IDA7IGkgPCBzLnNpemUoKTsgKytpKSB7CiAgICBkcFtpKzFdID0gbWF4KGRwW2krMV0sIGRwW2ldKTsKICAgIGZvciAoaW50IGogPSAwOyBqIDwgbjsgKytqKSB7CiAgICAgIHdoaWxlIChzW2ldICE9IHRbal1bbWF0W2pdXSAmJiBtYXRbal0gPiAwKSBtYXRbal0gPSBwaVtqXVttYXRbal0gLSAxXTsKICAgICAgaWYgKHNbaV0gPT0gdFtqXVttYXRbal1dKSArK21hdFtqXTsKICAgICAgaWYgKG1hdFtqXSA9PSB0W2pdLnNpemUoKSkgewogICAgICAgIGRwW2kgKyAxXSA9IG1heChkcFtpICsgMV0sIGRwW2kgLSAoaW50KXRbal0uc2l6ZSgpICsgMV0gKyBtYXRbal0pOwogICAgICAgIG1hdFtqXSA9IHBpW2pdW21hdFtqXSAtIDFdOwogICAgICB9CiAgICB9CiAgfQogIGNvdXQgPDwgZHBbcy5zaXplKCldIDw8ICdcbic7Cn0K