//***** author: attacker *****//
//***** created: 10.04.2026 18:13:01 *****//
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 1
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define bpc __builtin_popcount
#define size(v) (int) (v.size())
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
string s;
cin >> s;
int n = size(s);
int q;
cin >> q;
vector<pair<int, int>> segs(q);
for (auto& [x, y] : segs) {
cin >> x >> y;
--x, --y;
}
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
--a[i];
}
vector<set<int>> id(26);
for (int i = 0; i < n; i++) {
id[s[i] - 'a'].insert(i);
}
auto Bad = [&](int qlow, int qhigh) {
for (int i = 0; i < 26; i++) {
if (size(id[i]) == 0) {
continue;
}
auto low = id[i].lower_bound(qlow);
auto high = id[i].lower_bound(qhigh);
if (low == id[i].end() || *low > qhigh) --low;
if (high == id[i].end() || *high > qhigh) --high;
if (qlow <= *low && qhigh >= *high && *low != *high) {
return true;
}
}
return false;
};
int res = 0, i = 0;
for (auto [qlow, qhigh] : segs) {
while (i < n && Bad(qlow, qhigh)) {
id[s[a[i]] - 'a'].erase(a[i]);
s[a[i]] = '*';
res++, i++;
}
}
cout << res << '\n';
return 0;
}