#include<bits/stdc++.h> #define INF 0x3f3f3f3 #define maxn 100100 typedeflonglong ll; usingnamespacestd; #define rep(i, n, args...) for (ll i = 0, ##args; i < n; i++) #define repr(i, s, e, args...) for (ll i = s, ##args; i < e; i++) #define erg(i, u, args...) for (ll i = vtx[u], ##args; ~i; i = eg[i].nxt) #define fulset(x, v) memset(x, v, sizeof(x)); char s[maxn]; vector<int> pos[27]; int L[30],R[30]; int st[28][maxn]; int exi[28]; int used[28],no[28]; char ans[maxn]; int k,co,now; intmain() { freopen("02", "r", stdin); freopen("sample.out", "w", stdout); while (scanf("%s%d",s,&k)!=EOF) { co=-1; int cmp = k; int lens = strlen(s); //fulset(st, 0); for (int i=0;i<=26;i++) { exi[i]=0; used[i]=0; no[i]=0; } now =0; for (int i=0;i<=26;i++) pos[i].clear(); for (int i=1;i<=26;i++) scanf("%d%d",&L[i],&R[i]); for (int i=lens;i>=0;i--) { exi[s[i]-'a'+1]++; for (int j=1;j<=26;j++) st[j][i] = exi[j]; } bool OK = true; for (int i=1;i<=26;i++) if (L[i]>exi[i]) { OK = false; break; } if (!OK) { printf("-1\n"); continue; } for (int i=0;i<=lens-1;i++) pos[s[i]-'a'+1].push_back(i); //index related to string starts from 0 // int kkkk=0; while (1) { //cout << ++kkkk << endl; for (int i=1;i<=26;i++) { if (used[i] == R[i] || used[i] == exi[i]) continue; OK = true; int tot=0; int you=0; used[i]++; k--; int temp = now; for (int j=no[i];j<exi[i];j++) { if (pos[i][j] >= now) { now = pos[i][j]; break; } else no[i]++; } now++; for (int j=1;j<=26;j++) { if (st[j][now] + used[j] < L[j]) { OK = false; break; } } if (!OK) { used[i]--; k++; now = temp; continue; } for (int j=1;j<=26;j++) { you += min(st[j][now], R[j]-used[j]); tot += max(0,L[j]-used[j]); } if (tot > you || tot >k || you <k) OK = false;
if (!OK) { used[i]--; k++; now = temp; continue; } else { ans[++co] = 'a'+i-1; break; } } if (!k) break; } if (co+1 != cmp) printf("-1\n"); else { ans[++co] = '\0'; printf("%s\n",ans); } } fclose(stdin); fclose(stdout); return0; }