#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#define DEBUG(...) debug(#__VA_ARGS__, __VA_ARGS__)
#else
#define DEBUG(...) 6
#endif

template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) {return os << "(" << p.first << ", " << p.second << ")";}
template<typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type* = nullptr>
ostream& operator << (ostream &os, const C &c) {bool f = true; os << "["; for (const auto &x : c) {if (!f) os << ", "; f = false; os << x;} return os << "]";}
template<typename T> void debug(string s, T x) {cerr << "\033[1;35m" << s << "\033[0;32m = \033[33m" << x << "\033[0m\n";}
template<typename T, typename... Args> void debug(string s, T x, Args... args) {for (int i=0, b=0; i<(int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++; else
if (s[i] == ')' || s[i] == '}') b--; else if (s[i] == ',' && b == 0) {cerr << "\033[1;35m" << s.substr(0, i) << "\033[0;32m = \033[33m" << x << "\033[31m | "; debug(s.substr(s.find_first_not_of(' ', i + 1)), args...); break;}}

struct Node {
    int l, r;
    bool flip = false;
    long long sum = 0, hsum = 0, hlazy1 = 0, hlazy2 = 0, cnt = 0, ti = -1, lazyFirstTi = -1, lazyTi = -1;

    void leaf() {}

    void pull(const Node &a, const Node &b) {
        sum = a.sum + b.sum;
        ti = max(a.ti, b.ti);
        hsum = a.hsum + b.hsum + (ti - a.ti) * a.sum + (ti - b.ti) * b.sum;
    }

    void push(const Node &other) {
        flip ^= other.flip;
        if (cnt % 2 == 0) {
            hlazy1 += other.hlazy1;
            hlazy2 += other.hlazy2;
        } else {
            hlazy1 += other.hlazy2;
            hlazy2 += other.hlazy1;
        }
        if (lazyTi == -1) {
            lazyFirstTi = other.lazyFirstTi;
        } else {
            if (cnt % 2 == 0)
                hlazy2 += other.lazyFirstTi - lazyTi;
            else
                hlazy1 += other.lazyFirstTi - lazyTi;
        }
        cnt += other.cnt;
        lazyTi = other.lazyTi;
    }

    void apply() {
        hsum += hlazy1 * (r - l + 1 - sum) + (hlazy2 + lazyFirstTi - ti) * sum;
        ti = lazyTi;
        if (flip)
            sum = r - l + 1 - sum;
        flip = false;
        hlazy1 = hlazy2 = cnt = 0;
        lazyFirstTi = lazyTi = -1;
    }
};

struct SegmentTree {
    int n;
    vector<int> a;
    vector<Node> st;

    SegmentTree(int _n) : n(_n), a(n), st(4*n) {
        build(1, 0, n-1);
    }

    SegmentTree(const vector<int> &_a) : n((int) _a.size()), a(_a), st(4*n) {
        build(1, 0, n-1);
    }

    void build(int p, int l, int r) {
        st[p].l = l;
        st[p].r = r;
        if (l == r) {
            st[p].leaf();
            return;
        }
        int m = (l + r) / 2;
        build(2*p, l, m);
        build(2*p+1, m+1, r);
        st[p].pull(st[2*p], st[2*p+1]);
    }

    void push(int p) {
        if (st[p].lazyTi != -1) {
            if (st[p].l != st[p].r) {
                st[2*p].push(st[p]);
                st[2*p+1].push(st[p]);
            }
            st[p].apply();
        }
    }

    Node query(int p, int i, int j) {
        push(p);
        if (st[p].l == i && st[p].r == j)
            return st[p];
        int m = (st[p].l + st[p].r) / 2;
        if (j <= m)
            return query(2*p, i, j);
        else if (i > m)
            return query(2*p+1, i, j);
        Node ret, ls = query(2*p, i, m), rs = query(2*p+1, m+1, j);
        ret.pull(ls, rs);
        return ret;
    }

    long long query(int i, int j, long long ti) {
        Node ret = query(1, i, j);
        return ret.hsum + (ti - ret.ti) * ret.sum;
    }

    void update(int p, int i, int j, int ti) {
        if (st[p].l == i && st[p].r == j) {
            Node cur;
            cur.flip = true;
            cur.cnt = 1;
            cur.lazyFirstTi = cur.lazyTi = ti;
            st[p].push(cur);
            push(p);
            return;
        }
        push(p);
        int m = (st[p].l + st[p].r) / 2;
        if (j <= m) {
            update(2*p, i, j, ti);
            push(2*p+1);
        } else if (i > m) {
            push(2*p);
            update(2*p+1, i, j, ti);
        } else {
            update(2*p, i, m, ti);
            update(2*p+1, m+1, j, ti);
        }
        st[p].pull(st[2*p], st[2*p+1]);
    }

    void update(int i, int j, int ti) {
        update(1, i, j, ti);
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin >> n;
    vector<int> a(n);
    for (int i=0; i<n; i++) {
        cin >> a[i];
        a[i]--;
    }
    int m;
    cin >> m;
    vector<vector<pair<int, int>>> queries(n);
    for (int i=0; i<m; i++) {
        int l, r;
        cin >> l >> r;
        l--, r--;
        queries[r].emplace_back(l, i);
    }

    SegmentTree st(n);
    vector<int> last(n, -1);
    vector<long long> ret(m);
    for (int r=0; r<n; r++) {
        if (last[a[r]] != -1)
            st.update(0, last[a[r]], r);
        last[a[r]] = r;
        st.update(0, r, r);
        for (auto [l, i] : queries[r])
            ret[i] = st.query(l, r, (long long) r + 1);
    }

    for (long long x : ret)
        cout << x << "\n";

    return 0;
}
