You desperately need to build some string t. For that you've got n more strings s1, s2, ..., sn. To build string t, you are allowed to perform exactly |t| (|t| is the length of string t) operations on these strings. Each operation looks like that:

  1. choose any non-empty string from strings s1, s2, ..., sn; 
  2. choose an arbitrary character from the chosen string and write it on a piece of paper; 
  3. remove the chosen character from the chosen string. 



Note that after you perform the described operation, the total number of characters in strings s1, s2, ..., sn decreases by 1. We are assumed to build string t, if the characters, written on the piece of paper, in the order of performed operations form string t.

There are other limitations, though. For each string si you know number ai — the maximum number of characters you are allowed to delete from string si. You also know that each operation that results in deleting a character from string si, costs i rubles. That is, an operation on string s1 is the cheapest (it costs 1 ruble), and the operation on string sn is the most expensive one (it costs n rubles).

Your task is to count the minimum amount of money (in rubles) you will need to build string t by the given rules. Consider the cost of building string t to be the sum of prices of the operations you use.

Input

The first line of the input contains string t — the string that you need to build.

The second line contains a single integer n (1 ≤ n ≤ 100) — the number of strings to which you are allowed to apply the described operation. Each of the next n lines contains a string and an integer. The i-th line contains space-separated string si and integer ai (0 ≤ ai ≤ 100). Number ai represents the maximum number of characters that can be deleted from string si.

All strings in the input only consist of lowercase English letters. All strings are non-empty. The lengths of all strings do not exceed 100 characters.

Output

Print a single number — the minimum money (in rubles) you need in order to build string t. If there is no solution, print -1.

Examples

Input

bbaze
3
bzb 2
aeb 3
ba 10


Output

8


Input

abacaba
4
aba 2
bcc 1
caa 2
bbb 5


Output

18


Input

xyz
4
axx 8
za 1
efg 4
t 1


Output

-1

Note

Notes to the samples:

In the first sample from the first string you should take characters "b" and "z" with price 1 ruble, from the second string characters "a", "e" и "b" with price 2 rubles. The price of the string t in this case is 2·1 + 3·2 = 8.

In the second sample from the first string you should take two characters "a" with price 1 ruble, from the second string character "c" with price 2 rubles, from the third string two characters "a" with price 3 rubles, from the fourth string two characters "b" with price 4 rubles. The price of the string t in this case is 2·1 + 1·2 + 2·3 + 2·4 = 18.

In the third sample the solution doesn't exist because there is no character "y" in given strings.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 10;
struct Edge {
  int from, to, cap, flow, cost;
  Edge() {}
  Edge(int f, int t, int c, int fl, int co)
      : from(f), to(t), cap(c), flow(fl), cost(co) {}
};
struct MCMF {
  int n, m, s, t;
  vector<Edge> edges;
  vector<int> G[maxn];
  bool inq[maxn];
  int d[maxn];
  int p[maxn];
  int a[maxn];
  void init(int n, int s, int t) {
    this->n = n, this->s = s, this->t = t;
    edges.clear();
    for (int i = 0; i < n; ++i) G[i].clear();
  }
  void AddEdge(int from, int to, int cap, int cost) {
    edges.push_back(Edge(from, to, cap, 0, cost));
    edges.push_back(Edge(to, from, 0, 0, -cost));
    m = edges.size();
    G[from].push_back(m - 2);
    G[to].push_back(m - 1);
  }
  bool BellmanFord(int &flow, int &cost) {
    for (int i = 0; i < n; ++i) d[i] = 1e9;
    memset(inq, 0, sizeof(inq));
    d[s] = 0, a[s] = 1e9, inq[s] = true, p[s] = 0;
    queue<int> Q;
    Q.push(s);
    while (!Q.empty()) {
      int u = Q.front();
      Q.pop();
      inq[u] = false;
      for (int i = 0; i < G[u].size(); ++i) {
        Edge &e = edges[G[u][i]];
        if (e.cap > e.flow && d[e.to] > d[u] + e.cost) {
          d[e.to] = d[u] + e.cost;
          p[e.to] = G[u][i];
          a[e.to] = min(a[u], e.cap - e.flow);
          if (!inq[e.to]) {
            Q.push(e.to);
            inq[e.to] = true;
          }
        }
      }
    }
    if (d[t] == 1e9) return false;
    flow += a[t];
    cost += a[t] * d[t];
    int u = t;
    while (u != s) {
      edges[p[u]].flow += a[t];
      edges[p[u] ^ 1].flow -= a[t];
      u = edges[p[u]].from;
    }
    return true;
  }
  int Min_cost() {
    int flow = 0, cost = 0;
    while (BellmanFord(flow, cost))
      ;
    return cost;
  }
  int solve(int k) {
    int flow = 0, cost = 0;
    while (BellmanFord(flow, cost))
      ;
    return flow == k ? cost : -1;
  }
} mcmf;
int mp['z' + 10], temp['z' + 10];
int a[maxn];
int main() {
  string s;
  int n;
  cin >> s >> n;
  for (int i = 0; i < s.length(); i++) mp[s[i]]++;
  int S = 0, T = 200;
  mcmf.init(n + 501, S, T);
  string t;
  for (int i = 1; i <= n; i++) {
    memset(temp, 0, sizeof(temp));
    cin >> t >> a[i];
    mcmf.AddEdge(S, i, a[i], 0);
    for (int j = 0; j < t.length(); j++) {
      temp[t[j]]++;
    }
    for (int j = 'a'; j <= 'z'; j++) {
      mcmf.AddEdge(i, j + 4, temp[j], i);
    }
  }
  for (int i = 'a'; i <= 'z'; i++) {
    mcmf.AddEdge(i + 4, T, mp[i], 0);
  }
  int ans = mcmf.solve(s.length());
  printf("%d\n", ans);
}
