#include #define rep(x,to) for(int (x)=0;(x)<(to);(x)++) #define all(c) (c).begin(),(c).end() #define sz(v) (int)(v).size() #define dnmin(a,b) (a)=min((a),(b)) using namespace std; typedef vector VI; typedef pair pii; int main(){ int n, t; cin >>n; VI hs(n); unordered_map mp; vector jun; rep(i,n) cin >>hs[i]; cin >> t; VI nos(n); rep(i, t){ string s; char a; cin >> s >> a; if(!mp.count(s)) mp[s] = VI{ 0,0,0}; if(a != '?'){ if(mp[s][0] !=0){ int64_t sk = (1000-mp[s][0])*500000 + mp[s][1]; auto it = lower_bound(all(jun), sk); jun.erase(it); } mp[s][1] = i+1; int mz= a-'A'; mp[s][0] += 50 * hs[mz] + (500 * hs[mz]) / (8 + 2* (++nos[mz])); int64_t sk = (1000-mp[s][0])*500000 + mp[s][1]; auto it = lower_bound(all(jun), sk); jun.insert(it, sk); }else{ int64_t sk = (1000-mp[s][0])*500000 + mp[s][1]; auto it = lower_bound(all(jun), sk); int ans = distance(jun.begin(), it); cout << ans + 1 <<"\n"; } } return 0; }