#include <bits/stdc++.h>
using namespace std;
int q[150], c[150][150], u[150][150], f[150][150], p[150], d[150], fin;
vector<pair<int, int> > e;
char t[150];
int mcmflow(int flow) {
  int cost = 0;
  while (1) {
    memset((p), (-1), sizeof((p)));
    memset((d), (-1), sizeof((d)));
    d[0] = 0;
    while (1) {
      bool any = false;
      for (int i = 0; i < (e.size()); i++) {
        int a = e[i].first, b = e[i].second;
        if (d[a] == -1) continue;
        if (f[a][b] < u[a][b] && (d[b] == -1 || d[a] + c[a][b] < d[b])) {
          d[b] = d[a] + c[a][b];
          p[b] = a;
          any = true;
        }
      }
      if (!any) break;
    }
    if (d[fin] == -1) break;
    int af = 2147483647, cur = fin;
    while (cur) {
      af = min(af, u[p[cur]][cur] - f[p[cur]][cur]);
      cur = p[cur];
    }
    cur = fin;
    while (cur) {
      f[p[cur]][cur] += af;
      f[cur][p[cur]] -= af;
      cost += af * c[p[cur]][cur];
      cur = p[cur];
    }
    flow -= af;
  }
  return flow ? -1 : cost;
}
void add(int a, int b) {
  e.push_back(pair<int, int>(a, b));
  e.push_back(pair<int, int>(b, a));
}
int main() {
  gets(t);
  memset((u), (0), sizeof((u)));
  memset((c), (0), sizeof((c)));
  memset((f), (0), sizeof((f)));
  int len = strlen(t);
  for (int i = 0; i < (len); i++) u[0][t[i] - 'a' + 1]++;
  int n;
  scanf("%d\n", &n);
  fin = n + 27;
  for (int i = 0; i < (n); i++) {
    scanf("%s%d", t, &u[i + 27][fin]);
    c[i + 27][fin] = i + 1;
    int len = strlen(t);
    for (int j = 0; j < (len); j++) u[t[j] - 'a' + 1][i + 27]++;
    add(i + 27, fin);
  }
  for (int i = 0; i < (26); i++) {
    add(0, i + 1);
    for (int j = 0; j < (n); j++) add(i + 1, j + 27);
  }
  printf("%d", mcmflow(len));
}
