#include <iostream>

using namespace std;

int main(){
    int n,q;
    cin >> n;
    int a[n];
    for(int i=0;i<n;i++){
        cin >> a[i];
    }
    cin >> q;
    int b[q],c[q];
    for(int i=0;i<q;i++){
        cin >> b[i];
        cin >> c[i];
    }
    int d[100000] = {0};
    int e[100000] = {0};
    for(int i=0;i<q;i++){
        d[b[i]] = c[i]+1;
        e[b[i]] = i;
    }
    long f[q] = {0};
    long sum=0;
    for(int i=0;i<n;i++){
        sum += a[i];
        int last=0;
        while(1){
            if(d[a[i]] == 0){
                //cout << "break : " << i << endl;
                break;
            }
            //cout << "not break : " << i << " -> " << a[i] << endl;
            int diff = (d[a[i]]-1)-a[i];
            f[e[a[i]]] += diff;
            //cout << "diff : " << diff  << "  index : " << e[a[i]] << endl;
            a[i] = d[a[i]]-1;
            if(e[a[i]] <= last){
                //cout << e[a[i]] << " <= " << last << endl;
                break;
            }
            last = e[a[i]];
        }
    }
    for(long i=0;i<q;i++){
        sum += f[i];
        cout << "f[" << i << "] : " << f[i] << endl;
        cout << sum << endl;
    }
}