#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PLL;
typedef pair<int, int>PII;
typedef pair<double, double>PDD;
#define I_int ll
inline ll read()
{
ll x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9')
{
if(ch == '-')f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9')
{
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
inline void out(ll x){
if (x < 0) x = ~x + 1, putchar('-');
if (x > 9) out(x / 10);
putchar(x % 10 + '0');
}
inline void write(ll x){
if (x < 0) x = ~x + 1, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
puts("");
}
#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
ll ksm(ll a, ll b, ll p)
{
ll res = 1;
while(b)
{
if(b & 1)res = res * a % p;
a = a * a % p;
b >>= 1;
}
return res;
}
const int maxn=1e5+7;
int n,m,a[maxn],ans[maxn];
int vis[maxn],sum[maxn*100];
///sum表示当前节点的总和
///vis表示上一次出现的下标
int tr[maxn*100][2],idx;
int b[maxn];
int lowbit(int x){
return x&-x;
}
struct node{
int l,a,b,id;
};
vector<node>q[maxn];
void insert(int &now,int v,int x){
if(!now) now=++idx;
int u=now;
for(int i=20;i>=0;i--){
int p=x>>i&1;
if(!tr[u][p]) tr[u][p]=++idx;
u=tr[u][p];
sum[u]+=v;
}
}
int query(int now,int a,int b){
if(!now) return 0;
int res=0,u=now;
for(int i=20;i>=0;i--){
int p1=a>>i&1,p2=b>>i&1;
if(p2){
if(p1) res+=sum[tr[u][1]],u=tr[u][0];
else res+=sum[tr[u][0]],u=tr[u][1];
}
else{
if(p1) u=tr[u][1];
else u=tr[u][0];
}
if(!u) break;
}
return res+sum[u];
}
void add(int u,int v,int x){///树状数组的插入
while(u<=n){
insert(b[u],v,x);///字典树的插入
u+=lowbit(u);
}
}
int cal(int u,int a,int bb){///树状数组的查询
int res=0;
while(u>0){
res+=query(b[u],a,bb);///字典树的查询
u-=lowbit(u);
}
return res;
}
int main(){
n=read;
rep(i,1,n) a[i]=read;
m=read;
rep(i,1,m){
int l=read,r=read,aa=read,bb=read;
q[r].push_back({l,aa,bb,i});//存询问
}
rep(i,1,n){
if(vis[a[i]]) add(vis[a[i]],-1,a[i]);///删除之前的贡献
vis[a[i]]=i;
add(i,1,a[i]);///增加该点的贡献
for(int j=0;j<q[i].size();j++){
node t=q[i][j];
ans[t.id]=cal(i,t.a,t.b)-cal(t.l-1,t.a,t.b);
}
}
rep(i,1,m) printf("%d
",ans[i]);
return 0;
}