#include <bits/stdc++.h>
using namespace std;
int n, m, now, delta;
int st[100000], lk[100000 * 2];
int a[100000 * 2], b[100000 * 2], c[100000], f[100000], cost[100000];
bool visit[100000];
int q[100000], dis[100000], pre[100000];
bool SPFA(int s, int t) {
  int op = 0, cl = 1, len = n + 10;
  memset(q, 0, sizeof(q));
  memset(visit, 0, sizeof(visit));
  memset(dis, 0x7f, sizeof(dis));
  q[1] = s;
  visit[s] = 1;
  dis[s] = 0;
  dis[t] = 1000000000;
  while (op != cl) {
    op = op % len + 1;
    int x = q[op];
    for (int j = st[x]; j != 0; j = lk[j]) {
      if (j <= m) {
        if (c[j] == f[j]) continue;
      } else {
        if (f[j - m] == 0) continue;
      }
      int y = b[j];
      if (dis[x] + cost[j] < dis[y]) {
        dis[y] = dis[x] + cost[j];
        if (!visit[y]) {
          cl = cl % len + 1;
          q[cl] = y;
          visit[y] = 1;
          pre[y] = j;
        }
      }
    }
    visit[x] = 0;
  }
  if (dis[t] == 1000000000) return 0;
  delta = 1000000000;
  for (int now = t; now != s; now = a[pre[now]]) {
    int j = pre[now];
    if (j <= m)
      delta = min(delta, c[j] - f[j]);
    else
      delta = min(delta, f[j - m]);
  }
  for (int now = t; now != s; now = a[pre[now]]) {
    int j = pre[now];
    if (j <= m)
      f[j] += delta;
    else
      f[j - m] -= delta;
  }
  return 1;
}
int MinCostFlow(int s, int t) {
  memset(f, 0, sizeof(f));
  while (SPFA(s, t))
    ;
  int ans = 0;
  for (int i = 1; i <= m; ++i) ans += cost[i] * f[i];
  return ans;
}
void LinkListInit() {
  memset(st, 0, sizeof(st));
  for (int i = m + m; i; --i) {
    lk[i] = st[a[i]];
    st[a[i]] = i;
  }
}
int FF(int s, int t) {
  for (int i = 1; i <= m; ++i) {
    a[i + m] = b[i];
    b[i + m] = a[i];
    cost[i + m] = -cost[i];
  }
  LinkListInit();
  return MinCostFlow(s, t);
}
void AddEdge(int i, int j, int limit, int Cost) {
  ++m;
  a[m] = i;
  b[m] = j;
  c[m] = limit;
  cost[m] = Cost;
}
char t[110];
char s[110][110];
int limit[110];
int main() {
  scanf("%s", t);
  int k;
  scanf("%d\n", &k);
  for (int i = 0; i < k; ++i) {
    scanf("%s %d\n", s[i], limit + i);
    limit[i] = min(limit[i], (int)strlen(s[i]));
  }
  n = k + 1 + 26 + 1;
  for (int i = 0; i < k; ++i) AddEdge(1, i + 2, limit[i], 0);
  for (int i = 0; i < k; ++i)
    for (int j = 0; j < strlen(s[i]); ++j)
      AddEdge(i + 2, k + 1 + (s[i][j] - 'a' + 1), 1, i + 1);
  for (int j = 0; j < strlen(t); ++j)
    AddEdge(k + 1 + (t[j] - 'a' + 1), k + 1 + 26 + 1, 1, 0);
  int ans = FF(1, k + 1 + 26 + 1);
  for (int i = 1; i <= m; ++i)
    if (b[i] == k + 1 + 26 + 1 && c[i] != f[i]) {
      printf("-1\n");
      return 0;
    }
  printf("%d\n", ans);
}
