#include <bits/stdc++.h>
using namespace std;
inline int in() {
  int32_t x;
  scanf("%d", &x);
  return x;
}
inline string get() {
  char ch[1000010];
  scanf("%s", ch);
  return ch;
}
const int MAX_LG = 21;
const long long maxn = 1e6 + 100;
const long long base = 29;
const long long mod = 1e9 + 7;
const long long INF = 2e9 + 100;
long long cnt[maxn], source, sink, cur, id[maxn], n, res, di[maxn];
bool mark[maxn];
pair<long long, long long> pre[maxn];
vector<long long> g[maxn];
struct edge {
  long long from, to, cap, flow, cst;
  edge() {}
  edge(long long from, long long to, long long cap, long long flow,
       long long cst)
      : from(from), to(to), cap(cap), flow(flow), cst(cst) {}
};
vector<edge> e;
inline void addEdge(long long from, long long to, long long cap,
                    long long cst) {
  g[from].push_back(e.size());
  e.push_back({from, to, cap, 0, cst});
  g[to].push_back(e.size());
  e.push_back({to, from, 0, 0, -cst});
}
inline long long spfa(long long source) {
  for (long long i = 0; i <= n + 30; i++)
    di[i] = INF, mark[i] = 0, pre[i] = {-1, -1};
  di[source] = 0;
  mark[source] = true;
  queue<long long> q;
  q.push(source);
  while (q.empty() == false) {
    long long v = q.front();
    mark[v] = false;
    q.pop();
    for (long long i = 0; i < g[v].size(); i++) {
      long long u = e[g[v][i]].to;
      long long cst = e[g[v][i]].cst;
      if (e[g[v][i]].cap - e[g[v][i]].flow > 0 && di[v] + cst < di[u]) {
        di[u] = di[v] + cst;
        pre[u] = {v, g[v][i]};
        if (!mark[u]) q.push(u);
        mark[u] = true;
      }
    }
  }
  if (di[sink] == INF) return 0;
  long long v = sink;
  while (pre[v].first != -1) {
    long long ide = pre[v].second;
    long long cst = e[ide].cst;
    res += cst;
    e[ide].flow++;
    e[ide ^ 1].flow--;
    v = pre[v].first;
  }
  return 1;
}
inline long long max_flow() {
  long long ret = 0;
  while (long long x = spfa(source)) ret += x;
  return ret;
}
int32_t main() {
  string t = get();
  n = in();
  long long sz = t.size();
  source = cur++, sink = cur++;
  for (long long i = 0; i < t.size(); i++) cnt[t[i] - 'a']++;
  for (long long i = 0; i < 26; i++) {
    id[i] = cur++;
    addEdge(id[i], sink, cnt[i], 0);
  }
  for (long long i = 0; i < n; i++) {
    string s = get();
    long long lim = in();
    addEdge(source, cur, lim, 0);
    for (long long j = 0; j < 26; j++) cnt[j] = 0;
    for (long long j = 0; j < s.size(); j++) cnt[s[j] - 'a']++;
    for (long long j = 0; j < 26; j++) addEdge(cur, id[j], cnt[j], i + 1);
    cur++;
  }
  long long flow = max_flow();
  if (flow - sz) return cout << -1 << "\n", 0;
  cout << res << "\n";
}
