#include<bits/stdc++.h>
#define endl '\n'
#define F first
#define S second
#define pb push_back
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
#define error1(x) cerr<<#x<<" = "<<(x)<<endl
#define error2(a,b) cerr<<"("<<#a<<", "<<#b<<") = ("<<(a)<<", "<<(b)<<")\n";
#define coutall(v) for(auto &it: v) cout << it << " "; cout << endl;
using namespace std;
using ll = long long;
const int N = 2e5 + 9;
struct Segment_Tree {
vector<vector<int>> t;
Segment_Tree() {
t.resize(4 * N, {});
}
void build(int node, int st, int en, vector<int> &arr) { //=> O(N)
if (st == en) {
t[node] = {arr[st]};
return;
}
int mid = (st + en) >> 1;
build(node << 1, st, mid, arr); // divide left side
build(node << 1 | 1, mid + 1, en, arr); // divide right side
// Merging left and right portion
auto &Cur = t[node];
auto &Left = t[node << 1];
auto &Right = t[node << 1 | 1];
merge(Left.begin(), Left.end(), Right.begin(), Right.end(), back_inserter(Cur));
return;
}
int query(int node, int st, int en, int l, int r, int val) { //=> O(log n)
if (st > r || en < l) { // No overlapping and out of range
return 0; // <== careful
}
if (l <= st && en <= r) { // Complete overlapped (l-r in range)
return lower_bound(all(t[node]), val) - t[node].begin();
}
// Partial overlapping
int mid = (st + en) >> 1;
auto Left = query(node << 1, st, mid, l, r, val);
auto Right = query(node << 1 | 1, mid + 1, en, l, r, val);
return Left + Right;
}
} st1;
void solve() {
int n;
cin >> n;
vector<int> v(n + 1);
for (int i = 1; i <= n; i++) cin >> v[i];
st1.build(1, 1, n, v); // <==
int q, l, x, r;
cin >> q;
while(q--) {
cin >> l >> x >> r;
ll leftMn = st1.query(1, 1, n, l, x - 1, v[x]);
ll leftMx = (x - l) - st1.query(1, 1, n, l, x - 1, v[x]);
ll rightMn = st1.query(1, 1, n, x + 1, r, v[x]);
ll rightMx = (r - x) - st1.query(1, 1, n, x + 1, r, v[x]);
cout << (leftMn * rightMx) + (leftMx * rightMn) << endl;
}
return;
}
signed main() {
ios::sync_with_stdio(false); cin.tie(0);
int tc = 1;
cin >> tc;
for (int t = 1; t <= tc; t++) {
// cout << "Case " << t << ": ";
solve();
}
return 0;
}