/**
* Author: Sergey Kopeliovich (Burunduk30@gmail.com)
* Date: 2014.04.10
*/
#include <cstdio>
#include <cassert>
#include <algorithm>
using namespace std;
#define forn(i, n) for (int i = 0; i < (int)(n); i++)
#define all(a) (a).begin(), (a).end()
// O(log n)
int get( vector <int> &v, int x, int y )
{
return upper_bound(all(v), y) - lower_bound(all(v), x);
}
int main()
{
int n;
scanf("%d", &n);
int a[n];
forn(i, n)
scanf("%d", &a[i]);
// build = O(nlogn)
vector <int> v[2 * n];
forn(i, n)
v[i + n].push_back(a[i]);
for (int i = n - 1; i > 0; i--)
{
v[i].resize(v[2 * i].size() + v[2 * i + 1].size());
merge(all(v[2 * i]), all(v[2 * i + 1]), v[i].begin());
}
scanf("%d", &m);
while (m--)
{
// get (i <= k <= j) (x <= a[k] <= y)
int L, R, x, y;
scanf("%d%d%d%d", &L, &R, &x, &y);
// O(log^2 n)
int sum = 0;
for (L += n, R += n; L <= R; L >>= 1, R >>= 1)
{
if (L % 2 == 1) sum += get(v[L++], x, y);
if (R % 2 == 0) sum += get(v[R--], x, y);
}
printf("%d\n", sum);
}
return 0;
}