#include <bits/stdc++.h>
using namespace std;
struct node {
  int u, v, next;
  int w, c;
} edge[800010];
int head[8000], add, n, maxflow;
inline int min(int a, int b) { return a > b ? b : a; }
struct MinCostMaxFlow {
  int q[8000], front, rear, add, vn;
  bool vis[8000];
  int cost[8000], in[8000], pre[8000];
  void init(int n) {
    add = 0;
    memset(head, -1, sizeof(head));
    vn = n + 26 + 1;
  }
  void insert(int u, int v, int w, int c) {
    edge[add].u = u;
    edge[add].v = v;
    edge[add].w = w;
    edge[add].c = c;
    edge[add].next = head[u];
    head[u] = add++;
    edge[add].u = v;
    edge[add].v = u;
    edge[add].w = 0;
    edge[add].c = -c;
    edge[add].next = head[v];
    head[v] = add++;
  }
  bool spfa(int s, int e) {
    int i, j;
    for (i = 0; i <= vn; i++) cost[i] = 0x7fffffff;
    memset(vis, 0, sizeof(vis));
    memset(in, 0, sizeof(in));
    cost[s] = 0;
    pre[s] = -1;
    front = 0, rear = 1;
    q[front] = s;
    vis[s] = true;
    in[s]++;
    while (front != rear) {
      int u = q[front++], v;
      front %= 8000;
      vis[u] = false;
      for (i = head[u]; i != -1; i = edge[i].next) {
        v = edge[i].v;
        if (edge[i].w > 0 && cost[v] > cost[u] + edge[i].c) {
          cost[v] = cost[u] + edge[i].c;
          pre[v] = i;
          if (!vis[v]) {
            q[rear++] = v;
            rear %= 8000;
            vis[v] = true;
            in[v]++;
            if (in[v] > vn) return false;
          }
        }
      }
    }
    if (cost[e] < 0x7fffffff)
      return true;
    else
      return false;
  }
  int mincostmaxflow(int s, int e) {
    int mincost = 0, i, j;
    maxflow = 0;
    while (spfa(s, e)) {
      int flow = 0x7fffffff;
      for (i = pre[e]; i != -1; i = pre[edge[i].u]) flow = min(flow, edge[i].w);
      maxflow += flow;
      for (i = pre[e]; i != -1; i = pre[edge[i].u]) {
        edge[i].w -= flow;
        edge[i ^ 1].w += flow;
      }
      mincost += cost[e] * flow;
    }
    return mincost;
  }
} net;
char str[8000];
char ss[8000];
int cishu[50], val;
void gao() {
  int i, j, u, v, len, s, e;
  scanf("%d", &n);
  net.init(n * 2);
  len = strlen(str);
  memset(cishu, 0, sizeof(cishu));
  for (i = 0; i < len; i++) cishu[str[i] - 'a']++;
  s = 0;
  e = n + 26 + 1;
  for (i = 0; i < 26; i++) net.insert(i + n + 1, e, cishu[i], 0);
  for (i = 1; i <= n; i++) {
    scanf("%s %d", ss, &val);
    len = strlen(ss);
    memset(cishu, 0, sizeof(cishu));
    for (j = 0; j < len; j++) cishu[ss[j] - 'a']++;
    net.insert(s, i, val, 0);
    for (j = 0; j < 26; j++) net.insert(i, j + n + 1, cishu[j], i);
  }
  int mf = net.mincostmaxflow(s, e);
  if (maxflow < strlen(str)) {
    puts("-1");
    return;
  }
  printf("%d\n", mf);
}
int main() {
  while (scanf("%s", str) != EOF) {
    gao();
  }
  return 0;
}
