#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; map 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){ auto it = jun.find(pii(-mp[s][0],mp[s][1])); 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])); jun[pii(-mp[s][0],mp[s][1])] = s; }else{ auto it = jun.find(pii(-mp[s][0],mp[s][1])); int ans = distance(jun.begin(), it); cout << ans + 1 <<"\n"; } } return 0; }