#include <bits/stdc++.h>
using namespace std;
const int MAXV = 140;
const int oo = (1 << 30);
char reach[105], word[105];
int tot;
int s, t, V;
vector<int> G[MAXV];
int cap[MAXV][MAXV], flow[MAXV][MAXV], cost[MAXV][MAXV];
inline int ecap(int u, int v) {
  if (flow[v][u]) return flow[v][u];
  return cap[u][v] - flow[u][v];
}
inline int ecost(int u, int v) {
  if (flow[v][u]) return -cost[v][u];
  return cost[u][v];
}
int par[MAXV], dist[MAXV];
bool augment() {
  for (int i = 0; i < V; ++i) par[i] = -1, dist[i] = oo;
  dist[s] = 0;
  bool change = true;
  while (change) {
    change = false;
    for (int u = 0; u < V; ++u)
      for (int i = 0; i < G[u].size(); ++i) {
        int v = G[u][i];
        if (ecap(u, v) && dist[u] + ecost(u, v) < dist[v]) {
          par[v] = u;
          change = true;
          dist[v] = dist[u] + ecost(u, v);
        }
      }
  }
  return par[t] != -1;
}
int mcmf() {
  int flujo = 0, mincost = 0;
  while (augment()) {
    flujo++;
    for (int u = par[t], v = t; v != s; u = par[v = u]) {
      if (flow[v][u])
        flow[v][u]--, mincost -= cost[v][u];
      else
        flow[u][v]++, mincost += cost[u][v];
    }
  }
  if (flujo != tot) return -1;
  return mincost;
}
void add_edge(int u, int v, int _cap, int _cost) {
  G[u].push_back(v);
  G[v].push_back(u);
  cap[u][v] = _cap;
  cap[v][u] = 0;
  flow[u][v] = 0;
  flow[v][u] = 0;
  cost[u][v] = _cost;
  cost[v][u] = 0;
}
int main() {
  int n;
  scanf("%s", reach);
  tot = strlen(reach);
  scanf("%d", &n);
  s = n + 26;
  t = n + 27;
  V = n + 28;
  for (int i = 0; i < V; ++i) G[i].clear();
  int peso, frec[30];
  for (int i = 0; i < n; ++i) {
    scanf("%s %d", word, &peso);
    add_edge(s, i, peso, 0);
    int l = strlen(word);
    memset(frec, 0, sizeof(frec));
    for (int j = 0; j < l; ++j) {
      frec[word[j] - 'a']++;
    }
    for (int j = 0; j < 26; ++j) add_edge(i, n + j, frec[j], i + 1);
  }
  memset(frec, 0, sizeof(frec));
  for (int i = 0; i < strlen(reach); ++i) frec[reach[i] - 'a']++;
  for (int i = 0; i < 26; ++i) add_edge(n + i, t, frec[i], 0);
  printf("%d\n", mcmf());
  return 0;
}
