#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 20000, E = 4 * N;
int from[E], to[E], cap[E], w[E], ant[E], adj[N], inde;
void add(int a, int b, int c, int custo) {
  int& z = inde;
  from[z] = a;
  to[z] = b, cap[z] = c, w[z] = custo, ant[z] = adj[a], adj[a] = z++;
  from[z] = b;
  to[z] = a, cap[z] = 0, w[z] = -custo, ant[z] = adj[b], adj[b] = z++;
}
int dist[N], pot[N], pai[N];
set<pair<int, int> > heap;
void update(int no, int ndist, int p) {
  if (ndist >= dist[no]) return;
  if (dist[no] < inf) heap.erase(pair<int, int>(dist[no], no));
  dist[no] = ndist, pai[no] = p;
  heap.insert(pair<int, int>(dist[no], no));
}
pair<int, int> top() {
  pair<int, int> ret = *heap.begin();
  heap.erase(heap.begin());
  return ret;
}
int djikstra(int source, int sink) {
  heap.clear(), memset(dist, inf, sizeof dist);
  update(source, 0, -1);
  while (heap.size()) {
    pair<int, int> p = top();
    for (int i = adj[p.second]; i >= 0; i = ant[i])
      if (cap[i]) update(to[i], p.first + w[i] + pot[p.second] - pot[to[i]], i);
  }
  return dist[sink] < inf;
}
pair<int, int> mcmf(int source, int sink) {
  memset(pot, 0, sizeof pot);
  pair<int, int> p(0, 0);
  while (djikstra(source, sink)) {
    int cost = 0, flow = inf;
    for (int x = sink; x != source; x = from[pai[x]])
      if (cap[pai[x]] < flow) flow = cap[pai[x]];
    for (int x = sink; x != source; x = from[pai[x]])
      cap[pai[x]] -= flow, cap[pai[x] ^ 1] += flow, cost += w[pai[x]] * flow;
    for (int x = 0; x <= sink; x++) pot[x] += dist[x];
    p.first += cost, p.second += flow;
  }
  return p;
}
char t[105], s[105];
int main() {
  memset(adj, -1, sizeof adj);
  inde = 0;
  int source = 26, sink = 27, node_cnt = sink + 1;
  scanf("%s", t);
  for (int i = 0; t[i]; ++i) {
    add(t[i] - 'a', node_cnt, 1, 0);
    add(node_cnt, sink, inf, 0);
    node_cnt++;
  }
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    int a;
    scanf("%s %d", s, &a);
    for (int j = 0; s[j]; ++j) {
      add(node_cnt, s[j] - 'a', 1, i + 1);
    }
    add(source, node_cnt, a, 0);
    node_cnt++;
  }
  pair<int, int> ans = mcmf(source, sink);
  if (ans.second == strlen(t)) {
    printf("%d\n", ans.first);
  } else {
    puts("-1");
  }
  return 0;
}
