#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int start;
int tail;
int head[150500], to[150500], nxt[150500], wei[150500], cost[150500], tot = 1;
int dis[5050], pre[5050], pres[5050];
bool vis[5050];
queue<int> que;
int n, sum, k;
int used, flow;
char str[105];
int numstr[30], numstr1[105][30], num[105];
void add(int x, int y, int z, int w) {
  tot++, to[tot] = y, nxt[tot] = head[x], head[x] = tot, cost[tot] = w,
         wei[tot] = z;
  tot++, to[tot] = x, nxt[tot] = head[y], head[y] = tot, cost[tot] = -w,
         wei[tot] = 0;
}
bool spfa() {
  memset(dis, inf, sizeof(dis));
  memset(vis, 0, sizeof(vis));
  dis[start] = 0;
  que.push(start);
  int now;
  do {
    now = que.front();
    vis[now] = 0;
    que.pop();
    for (int i = head[now]; i; i = nxt[i]) {
      if (dis[to[i]] > dis[now] + cost[i] && wei[i]) {
        dis[to[i]] = dis[now] + cost[i];
        pre[to[i]] = now;
        pres[to[i]] = i;
        if (!vis[to[i]]) {
          que.push(to[i]);
          vis[to[i]] = 1;
        }
      }
    }
  } while (!que.empty());
  return dis[tail] != inf;
}
void f() {
  int now = tail;
  int low = inf;
  while (now != start) {
    low = min(low, wei[pres[now]]);
    now = pre[now];
  }
  used += low * dis[tail];
  flow += low;
  now = tail;
  while (now != start) {
    wei[pres[now]] -= low;
    wei[pres[now] ^ 1] += low;
    now = pre[now];
  }
}
int main() {
  int len;
  cin >> str;
  len = strlen(str);
  for (int i = 0; i < strlen(str); i++) {
    numstr[str[i] - 'a' + 1]++;
  }
  cin >> k;
  start = 0, tail = 30;
  for (int i = 1; i <= k; i++) {
    cin >> str >> num[i];
    for (int j = 0; j < strlen(str); j++) {
      numstr1[i][str[j] - 'a' + 1]++;
    }
  }
  for (int i = 1; i <= 26; i++) {
    if (!numstr[i]) continue;
    add(start, i, numstr[i], 0);
    for (int j = 1; j <= k; j++) {
      if (!numstr1[j][i]) continue;
      add(i, j + 30, numstr1[j][i], j);
    }
  }
  for (int i = 1; i <= k; i++) {
    add(i + 30, tail, num[i], 0);
  }
  while (spfa()) {
    f();
  }
  if (flow != len) {
    cout << -1 << endl;
  } else {
    cout << used << endl;
  }
  return 0;
}
