#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T MAX(T a, T b) {
  if (a > b) return a;
  return b;
}
template <class T>
inline T MIN(T a, T b) {
  if (a < b) return a;
  return b;
}
inline void OPEN(const string &s) {
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}
string s;
int nA, nB;
int kiri[300 + 5], kanan[300 + 5];
int G[300 + 5][300 + 5];
int rG[300 + 5][300 + 5];
int N;
bool found[300 + 5][2];
inline bool dfs(int v, int sisi) {
  if (found[v][sisi]) return false;
  found[v][sisi] = true;
  if (sisi == 0) {
    for (int i = 0, _n = (nB); i < _n; i++) {
      if (G[v][i] && dfs(i, !sisi)) {
        G[v][i]--;
        rG[i][v]++;
        return true;
      }
    }
  } else {
    if (kanan[v] > 0) {
      kanan[v]--;
      return true;
    }
    for (int i = 0, _n = (nA); i < _n; i++) {
      if (rG[v][i] && dfs(i, !sisi)) {
        rG[v][i]--;
        G[i][v]++;
        return true;
      }
    }
  }
  return false;
}
int main() {
  cin >> s;
  for (int i = 0, _n = ((s).size()); i < _n; i++) kanan[s[i] - 'a']++;
  cin >> N;
  nA = N;
  nB = 26;
  for (int i = 0, _n = (N); i < _n; i++) {
    cin >> s >> kiri[i];
    for (int j = 0, _n = ((s).size()); j < _n; j++) G[i][s[j] - 'a']++;
  }
  int ret = 0;
  for (int i = 0, _n = (N); i < _n; i++) {
    memset(found, 0, sizeof(found));
    while (kiri[i] > 0 && dfs(i, 0)) {
      ret += i + 1;
      kiri[i]--;
      memset(found, 0, sizeof(found));
    }
  }
  bool flag = true;
  for (int i = 0, _n = (26); i < _n; i++)
    if (kanan[i] != 0) flag = false;
  if (!flag) ret = -1;
  cout << ret << endl;
  return 0;
}
