#include <bits/stdc++.h>
using namespace std;
int s[130][130], cost[130][130], st, ed, N, in[130], pre[130], d[130], all;
char str[105], tmp[105];
void SPFA() {
  int i, j;
  memset(in, 0, sizeof(in));
  memset(pre, -1, sizeof(pre));
  for (i = 0; i <= ed; i++) d[i] = 1e9;
  d[st] = 0;
  queue<int> q;
  q.push(st);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    in[x] = 0;
    for (i = 0; i <= ed; i++) {
      if (s[x][i]) {
        if (d[x] + cost[x][i] < d[i]) {
          d[i] = d[x] + cost[x][i];
          pre[i] = x;
          if (in[i] == 0) {
            in[i] = 1;
            q.push(i);
          }
        }
      }
    }
  }
}
int flow() {
  int i, j, ret = 0;
  while (1) {
    SPFA();
    if (d[ed] == 1e9) return ret;
    ret += d[ed];
    all++;
    i = ed;
    while (pre[i] != -1) {
      s[pre[i]][i] -= 1;
      s[i][pre[i]] += 1;
      i = pre[i];
    }
  }
}
int main() {
  int i, j, len, n;
  while (scanf(" %s", str) != EOF) {
    len = strlen(str);
    scanf(" %d", &N);
    memset(s, 0, sizeof(s));
    memset(cost, 0, sizeof(cost));
    st = 0;
    ed = N + 27;
    for (i = 0; i < len; i++) s[str[i] - 'a' + N + 1][ed]++;
    for (n = 0; n < N; n++) {
      scanf(" %s %d", tmp, &i);
      int t_len = strlen(tmp);
      s[st][n + 1] = min(t_len, i);
      cost[st][n + 1] = n + 1;
      cost[n + 1][st] = -n - 1;
      for (i = 0; i < t_len; i++) s[n + 1][N + tmp[i] - 'a' + 1]++;
    }
    all = 0;
    int tt = flow();
    if (all == len)
      printf("%d\n", tt);
    else
      printf("-1\n");
  }
  return 0;
}
