#include <bits/stdc++.h>
using namespace std;
char line[200];
int n, ans, len, second, E;
int cap[1024][1024];
int cost[1024][1024];
int fnet[1024][1024], adj[1024][1024], deg[1024];
int par[1024], d[1024];
int pi[1024];
bool dijkstra(int n, int s, int t) {
  for (int i = 0; i < n; i++) d[i] = (INT_MAX / 2), par[i] = -1;
  d[s] = 0;
  par[s] = -n - 1;
  while (1) {
    int u = -1, bestD = (INT_MAX / 2);
    for (int i = 0; i < n; i++)
      if (par[i] < 0 && d[i] < bestD) bestD = d[u = i];
    if (bestD == (INT_MAX / 2)) break;
    par[u] = -par[u] - 1;
    for (int i = 0; i < deg[u]; i++) {
      int v = adj[u][i];
      if (par[v] >= 0) continue;
      if (fnet[v][u] && d[v] > (d[u] + pi[u] - pi[v]) - cost[v][u])
        d[v] = (d[u] + pi[u] - pi[v]) - cost[v][u], par[v] = -u - 1;
      if (fnet[u][v] < cap[u][v] && d[v] > (d[u] + pi[u] - pi[v]) + cost[u][v])
        d[v] = (d[u] + pi[u] - pi[v]) + cost[u][v], par[v] = -u - 1;
    }
  }
  for (int i = 0; i < n; i++)
    if (pi[i] < (INT_MAX / 2)) pi[i] += d[i];
  return par[t] >= 0;
}
int mcmf3(int n, int s, int t, int &fcost) {
  memset(deg, 0, sizeof(deg));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      if (cap[i][j] || cap[j][i]) adj[i][deg[i]++] = j;
  memset(fnet, 0, sizeof(fnet));
  memset(pi, 0, sizeof(pi));
  int flow = fcost = 0;
  while (dijkstra(n, s, t)) {
    int bot = INT_MAX;
    for (int v = t, u = par[v]; v != s; u = par[v = u])
      bot = min(bot, fnet[v][u] ? fnet[v][u] : (cap[u][v] - fnet[u][v]));
    for (int v = t, u = par[v]; v != s; u = par[v = u])
      if (fnet[v][u]) {
        fnet[v][u] -= bot;
        fcost -= bot * cost[v][u];
      } else {
        fnet[u][v] += bot;
        fcost += bot * cost[u][v];
      }
    flow += bot;
  }
  return flow;
}
using namespace std;
int main() {
  scanf("%s%d", line, &n);
  ans = len = strlen(line);
  second = 0, E = 1 + n + 26;
  for (int i = 0; i < len; ++i) {
    cap[line[i] - 'a' + n + 1][E]++;
  }
  for (int i = 1; i <= n; ++i) {
    int a;
    scanf("%s%d", line, &a);
    len = strlen(line);
    cap[second][i] = a, cost[second][i] = i;
    for (int j = 0; j < len; ++j) {
      cap[i][line[j] - 'a' + n + 1]++;
    }
  }
  int fc = 0;
  int fl = mcmf3(n + 2 + 26, second, E, fc);
  if (fl == ans)
    cout << fc;
  else
    cout << -1;
  return 0;
}
