#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;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojaWZkZWYgTE9DQUwKI2RlZmluZSBERUJVRyguLi4pIGRlYnVnKCNfX1ZBX0FSR1NfXywgX19WQV9BUkdTX18pCiNlbHNlCiNkZWZpbmUgREVCVUcoLi4uKSA2CiNlbmRpZgoKdGVtcGxhdGU8dHlwZW5hbWUgVCwgdHlwZW5hbWUgUz4gb3N0cmVhbSYgb3BlcmF0b3IgPDwgKG9zdHJlYW0gJm9zLCBjb25zdCBwYWlyPFQsIFM+ICZwKSB7cmV0dXJuIG9zIDw8ICIoIiA8PCBwLmZpcnN0IDw8ICIsICIgPDwgcC5zZWNvbmQgPDwgIikiO30KdGVtcGxhdGU8dHlwZW5hbWUgQywgdHlwZW5hbWUgVCA9IGRlY2F5PGRlY2x0eXBlKCpiZWdpbihkZWNsdmFsPEM+KCkpKT4sIHR5cGVuYW1lIGVuYWJsZV9pZjwhaXNfc2FtZTxDLCBzdHJpbmc+Ojp2YWx1ZT46OnR5cGUqID0gbnVsbHB0cj4Kb3N0cmVhbSYgb3BlcmF0b3IgPDwgKG9zdHJlYW0gJm9zLCBjb25zdCBDICZjKSB7Ym9vbCBmID0gdHJ1ZTsgb3MgPDwgIlsiOyBmb3IgKGNvbnN0IGF1dG8gJnggOiBjKSB7aWYgKCFmKSBvcyA8PCAiLCAiOyBmID0gZmFsc2U7IG9zIDw8IHg7fSByZXR1cm4gb3M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GhzdW0gPSBhLmhzdW0gKyBiLmhzdW0gKyAodGkgLSBhLnRpKSAqIGEuc3VtICsgKHRpIC0gYi50aSkgKiBiLnN1bTsKICAgIH0KCiAgICB2b2lkIHB1c2goY29uc3QgTm9kZSAmb3RoZXIpIHsKICAgICAgICBmbGlwIF49IG90aGVyLmZsaXA7CiAgICAgICAgaWYgKGNudCAlIDIgPT0gMCkgewogICAgICAgICAgICBobGF6eTEgKz0gb3RoZXIuaGxhenkxOwogICAgICAgICAgICBobGF6eTIgKz0gb3RoZXIuaGxhenkyOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGhsYXp5MSArPSBvdGhlci5obGF6eTI7CiAgICAgICAgICAgIGhsYXp5MiArPSBvdGhlci5obGF6eTE7CiAgICAgICAgfQogICAgICAgIGlmIChsYXp5VGkgPT0gLTEpIHsKICAgICAgICAgICAgbGF6eUZpcnN0VGkgPSBvdGhlci5sYXp5Rmlyc3RUaTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBpZiAoY250ICUgMiA9PSAwKQogICAgICAgICAgICAgICAgaGxhenkyICs9IG90aGVyLmxhenlGaXJzdFRpIC0gbGF6eVRpOwogICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICBobGF6eTEgKz0gb3RoZXIubGF6eUZpcnN0VGkgLSBsYXp5VGk7CiAgICAgICAgfQogICAgICAgIGNudCArPSBvdGhlci5jbnQ7CiAgICAgICAgbGF6eVRpID0gb3RoZXIubGF6eVRpOwogICAgfQoKICAgIHZvaWQgYXBwbHkoKSB7CiAgICAgICAgaHN1bSArPSBobGF6eTEgKiAociAtIGwgKyAxIC0gc3VtKSArIChobGF6eTIgKyBsYXp5Rmlyc3RUaSAtIHRpKSAqIHN1bTsKICAgICAgICB0aSA9IGxhenlUaTsKICAgICAgICBpZiAoZmxpcCkKICAgICAgICAgICAgc3VtID0gciAtIGwgKyAxIC0gc3VtOwogICAgICAgIGZsaXAgPSBmYWxzZTsKICAgICAgICBobGF6eTEgPSBobGF6eTIgPSBjbnQgPSAwOwogICAgICAgIGxhenlGaXJzdFRpID0gbGF6eVRpID0gLTE7CiAgICB9Cn07CgpzdHJ1Y3QgU2VnbWVudFRyZWUgewogICAgaW50IG47CiAgICB2ZWN0b3I8aW50PiBhOwogICAgdmVjdG9yPE5vZGU+IHN0OwoKICAgIFNlZ21lbnRUcmVlKGludCBfbikgOiBuKF9uKSwgYShuKSwgc3QoNCpuKSB7CiAgICAgICAgYnVpbGQoMSwgMCwgbi0xKTsKICAgIH0KCiAgICBTZWdtZW50VHJlZShjb25zdCB2ZWN0b3I8aW50PiAmX2EpIDogbigoaW50KSBfYS5zaXplKCkpLCBhKF9hKSwgc3QoNCpuKSB7CiAgICAgICAgYnVpbGQoMSwgMCwgbi0xKTsKICAgIH0KCiAgICB2b2lkIGJ1aWxkKGludCBwLCBpbnQgbCwgaW50IHIpIHsKICAgICAgICBzdFtwXS5sID0gbDsKICAgICAgICBzdFtwXS5yID0gcjsKICAgICAgICBpZiAobCA9PSByKSB7CiAgICAgICAgICAgIHN0W3BdLmxlYWYoKTsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBpbnQgbSA9IChsICsgcikgLyAyOwogICAgICAgIGJ1aWxkKDIqcCwgbCwgbSk7CiAgICAgICAgYnVpbGQoMipwKzEsIG0rMSwgcik7CiAgICAgICAgc3RbcF0ucHVsbChzdFsyKnBdLCBzdFsyKnArMV0pOwogICAgfQoKICAgIHZvaWQgcHVzaChpbnQgcCkgewogICAgICAgIGlmIChzdFtwXS5sYXp5VGkgIT0gLTEpIHsKICAgICAgICAgICAgaWYgKHN0W3BdLmwgIT0gc3RbcF0ucikgewogICAgICAgICAgICAgICAgc3RbMipwXS5wdXNoKHN0W3BdKTsKICAgICAgICAgICAgICAgIHN0WzIqcCsxXS5wdXNoKHN0W3BdKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBzdFtwXS5hcHBseSgpOwogICAgICAgIH0KICAgIH0KCiAgICBOb2RlIHF1ZXJ5KGludCBwLCBpbnQgaSwgaW50IGopIHsKICAgICAgICBwdXNoKHApOwogICAgICAgIGlmIChzdFtwXS5sID09IGkgJiYgc3RbcF0uciA9PSBqKQogICAgICAgICAgICByZXR1cm4gc3RbcF07CiAgICAgICAgaW50IG0gPSAoc3RbcF0ubCArIHN0W3BdLnIpIC8gMjsKICAgICAgICBpZiAoaiA8PSBtKQogICAgICAgICAgICByZXR1cm4gcXVlcnkoMipwLCBpLCBqKTsKICAgICAgICBlbHNlIGlmIChpID4gbSkKICAgICAgICAgICAgcmV0dXJuIHF1ZXJ5KDIqcCsxLCBpLCBqKTsKICAgICAgICBOb2RlIHJldCwgbHMgPSBxdWVyeSgyKnAsIGksIG0pLCBycyA9IHF1ZXJ5KDIqcCsxLCBtKzEsIGopOwogICAgICAgIHJldC5wdWxsKGxzLCBycyk7CiAgICAgICAgcmV0dXJuIHJldDsKICAgIH0KCiAgICBsb25nIGxvbmcgcXVlcnkoaW50IGksIGludCBqLCBsb25nIGxvbmcgdGkpIHsKICAgICAgICBOb2RlIHJldCA9IHF1ZXJ5KDEsIGksIGopOwogICAgICAgIHJldHVybiByZXQuaHN1bSArICh0aSAtIHJldC50aSkgKiByZXQuc3VtOwogICAgfQoKICAgIHZvaWQgdXBkYXRlKGludCBwLCBpbnQgaSwgaW50IGosIGludCB0aSkgewogICAgICAgIGlmIChzdFtwXS5sID09IGkgJiYgc3RbcF0uciA9PSBqKSB7CiAgICAgICAgICAgIE5vZGUgY3VyOwogICAgICAgICAgICBjdXIuZmxpcCA9IHRydWU7CiAgICAgICAgICAgIGN1ci5jbnQgPSAxOwogICAgICAgICAgICBjdXIubGF6eUZpcnN0VGkgPSBjdXIubGF6eVRpID0gdGk7CiAgICAgICAgICAgIHN0W3BdLnB1c2goY3VyKTsKICAgICAgICAgICAgcHVzaChwKTsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBwdXNoKHApOwogICAgICAgIGludCBtID0gKHN0W3BdLmwgKyBzdFtwXS5yKSAvIDI7CiAgICAgICAgaWYgKGogPD0gbSkgewogICAgICAgICAgICB1cGRhdGUoMipwLCBpLCBqLCB0aSk7CiAgICAgICAgICAgIHB1c2goMipwKzEpOwogICAgICAgIH0gZWxzZSBpZiAoaSA+IG0pIHsKICAgICAgICAgICAgcHVzaCgyKnApOwogICAgICAgICAgICB1cGRhdGUoMipwKzEsIGksIGosIHRpKTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICB1cGRhdGUoMipwLCBpLCBtLCB0aSk7CiAgICAgICAgICAgIHVwZGF0ZSgyKnArMSwgbSsxLCBqLCB0aSk7CiAgICAgICAgfQogICAgICAgIHN0W3BdLnB1bGwoc3RbMipwXSwgc3RbMipwKzFdKTsKICAgIH0KCiAgICB2b2lkIHVwZGF0ZShpbnQgaSwgaW50IGosIGludCB0aSkgewogICAgICAgIHVwZGF0ZSgxLCBpLCBqLCB0aSk7CiAgICB9Cn07CgppbnQgbWFpbigpIHsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogICAgY2luLnRpZShOVUxMKTsKCiAgICBpbnQgbjsKICAgIGNpbiA+PiBuOwogICAgdmVjdG9yPGludD4gYShuKTsKICAgIGZvciAoaW50IGk9MDsgaTxuOyBpKyspIHsKICAgICAgICBjaW4gPj4gYVtpXTsKICAgICAgICBhW2ldLS07CiAgICB9CiAgICBpbnQgbTsKICAgIGNpbiA+PiBtOwogICAgdmVjdG9yPHZlY3RvcjxwYWlyPGludCwgaW50Pj4+IHF1ZXJpZXMobik7CiAgICBmb3IgKGludCBpPTA7IGk8bTsgaSsrKSB7CiAgICAgICAgaW50IGwsIHI7CiAgICAgICAgY2luID4+IGwgPj4gcjsKICAgICAgICBsLS0sIHItLTsKICAgICAgICBxdWVyaWVzW3JdLmVtcGxhY2VfYmFjayhsLCBpKTsKICAgIH0KCiAgICBTZWdtZW50VHJlZSBzdChuKTsKICAgIHZlY3RvcjxpbnQ+IGxhc3QobiwgLTEpOwogICAgdmVjdG9yPGxvbmcgbG9uZz4gcmV0KG0pOwogICAgZm9yIChpbnQgcj0wOyByPG47IHIrKykgewogICAgICAgIGlmIChsYXN0W2Fbcl1dICE9IC0xKQogICAgICAgICAgICBzdC51cGRhdGUoMCwgbGFzdFthW3JdXSwgcik7CiAgICAgICAgbGFzdFthW3JdXSA9IHI7CiAgICAgICAgc3QudXBkYXRlKDAsIHIsIHIpOwogICAgICAgIGZvciAoYXV0byBbbCwgaV0gOiBxdWVyaWVzW3JdKQogICAgICAgICAgICByZXRbaV0gPSBzdC5xdWVyeShsLCByLCAobG9uZyBsb25nKSByICsgMSk7CiAgICB9CgogICAgZm9yIChsb25nIGxvbmcgeCA6IHJldCkKICAgICAgICBjb3V0IDw8IHggPDwgIlxuIjsKCiAgICByZXR1cm4gMDsKfQo=