#include <bits/stdc++.h>
using namespace std;
int N, n, m, t, j, k, x, y, mm, z, f, ans;
bool inq[300005];
char s[150];
int c[150][150];
int vis[150];
int Augment(int x, int a) {
  if (x == N - 1) return a;
  vis[x] = 1;
  for (int i = 0; i < N; i++)
    if (!vis[i] && c[x][i]) {
      int ff = Augment(i, min(a, c[x][i]));
      if (ff) {
        c[x][i] -= ff;
        c[i][x] += ff;
        return ff;
      }
    }
  return 0;
}
int main() {
  scanf("%s%d", s, &n);
  N = n + 28;
  for (int i = 0; s[i]; i++) c[s[i] - 'a' + 1 + n][N - 1]++;
  for (int i = 0; i < n; i++) {
    scanf("%s%d", s, &c[0][i + 1]);
    for (int j = 0; s[j]; j++) c[i + 1][s[j] - 'a' + 1 + n]++;
  }
  while (1) {
    fill(vis, vis + N, 0);
    if (!Augment(0, 999999999)) break;
  }
  ans = 0;
  for (int i = 1; i <= n; i++)
    for (j = 1; j <= 26; j++) ans += c[n + j][i] * i;
  for (int i = n + 1; i <= n + 26; i++)
    if (c[i][N - 1]) {
      printf("-1");
      return 0;
    }
  printf("%d", ans);
  return 0;
}
