#include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;i<n;i++)
#define ll long long
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%I64d",&x)
#define ss(s) scanf("%s",s)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
int mod ;
const int N = 3e5;
vpll g[N];
vi cur;
int part;
int gvis[N], lvl[N], stree[N];
ll up[N], down[N], sz[N];
int a[N];
int L, R;
void dfs(int u, int par){
//add u to current tree
if (par == 0) cur.clear();
cur.pb(u);
lvl[u] = 1+lvl[par];
sz[u] = 1;
for(pii it: g[u]){
int v = it.F;
if (gvis[v] or v == par) continue;
dfs(v, u);
sz[u] += sz[v];
}
}
int centroid(int u, int par){
for(auto it: g[u]){
int v = it.F;
if ( v == par or gvis[v]) continue;
if (2*sz[v] > (int)cur.size()) return centroid(v, u);
}
return u;
}
vi dp[N][2];
void go(int u, int par, int cen){
lvl[u] = 1+lvl[par];
if (par == 0) up[u] = 0;
if(par == cen){
stree[u] = part++;
// dr[u] = a[u] <= a[cen]; //0 for lo
}
else {
stree[u] = stree[par];
}
//push u in hi(u)
dp[u][1].pb(0);
int i, dr = 0;
for(auto it: g[u]){
int v = it.F, w = it.S;
if (v == par or gvis[v]) continue;
go(v, u, cen);
dr = a[v] >= a[u];
int ex = a[v] > a[u];
for(int val: dp[v][0])
dp[u][dr].pb(val + ex);
for(int val: dp[v][1])
dp[u][dr].pb(val);
}
sortall(dp[u][0]);
sortall(dp[u][1]);
// cout<<"At " << u <<endl;
// for(int x: dp[u][0]) cout<<x<<" "; cout<<endl;
// for(int x: dp[u][1]) cout<<x<<" "; cout<<endl;
// cout<<"done\n";
}
map<ll, ll> cnt, st[N];
bool f(ll val, ll x){
return val >= x;
}
int query(vi &a, ll val, int lo = 0, int hi = -1){
if(hi==-1)hi = a.size()-1;
// cout<<lo<<" * "<<hi<<" "<<val<<" "<<a[0]<<endl;
if (a.empty())return 0;
if (val < 0)return 0;
if (a[0] > val) return 0;
if (lo == hi) return lo+1;
if (lo+1 == hi){
if(f(val, a[hi]))return hi+1;
return lo+1;
}
int mid = (lo+hi)/2;
if(f(val, a[mid])) return query(a, val, mid, hi);
else return query(a, val, lo, mid-1);
}
ll solvefor(int cen){
int i;
//solve for centroid
//init current parts of subtree
part = 0;
//make centroid as root
//find levels
for(int u: cur) dp[u][0].clear(), dp[u][1].clear();
fo(i, part) st[i].clear();
stree[0] = N-1;
lvl[0] = -1;
go(cen, 0, cen);
//precalculate the maps
//traverse only the current nodes in present tree
set<ll> val;
val.clear();
// for(int u: cur) cnt[up[u]]++, st[stree[u]][up[u]]++, val.insert(up[u]);
ll pre = 0;
//dp so that cnt[x] gives no of nodes with
//distance to root <=x
//calculate ans
ll ans = 0;
int u = cen;
// cout<<"Find answer for centorids "<<u<<endl;
// for(int val: dp[u][0]) cout<<val<<" ";cout<<endl;
// for(int val: dp[u][1]) cout<<val<<" ";cout<<endl;
for(auto it: g[u]){
int v = it.F, w = it.S;
if (gvis[v]) continue;
if(a[v] < a[u]){
// cout<<" v "<<v<<" : "<<dp[v][0].size()<<" "<<dp[v][1].size()<<endl;
for(int val: dp[v][0]){
int lo = L-val, hi = R-val;
if(hi<0) break;
// cout<<val<<": "<<lo<<" "<<hi<<" :: ";
ans += query(dp[u][0], hi-1) - query(dp[u][0], lo-2);
// cout<<ans<<" ";
ans += query(dp[u][1], hi) - query(dp[u][1], lo-1);
// cout<<ans<<" ";
ans -= query(dp[v][0], hi-1) - query(dp[v][0], lo-2);
// cout<<ans<<" ";
ans -= query(dp[v][1], hi-1) - query(dp[v][1], lo-2);
// cout<<ans<<endl;
}
for(int val: dp[v][1]){
int lo = L-val, hi = R-val;
if(hi<0) break;
ans += query(dp[u][0], hi-1) - query(dp[u][0], lo-2);
ans += query(dp[u][1], hi) - query(dp[u][1], lo-1);
ans -= query(dp[v][0], hi-1) - query(dp[v][0], lo-2);
ans -= query(dp[v][1], hi-1) - query(dp[v][1], lo-2);
}
// cout<<"cen "<<cen<<" "<<v<<" "<<ans<<"<"<<endl;
}
else{
// cout<<" v "<<v<<" : "<<dp[v][0].size()<<" "<<dp[v][1].size()<<endl;
for(int val: dp[v][0]){
// cout<<val<<" ";
int ch = 0;
if(a[v]>a[cen])val++, ch = 1;
// cout<<val<<" :: ";
int lo = L-val, hi = R-val;
if(hi<0) break;
ans += query(dp[u][0], hi) - query(dp[u][0], lo-1);
// cout<<ans<<" ";
ans += query(dp[u][1], hi) - query(dp[u][1], lo-1);
// cout<<ans<<" ";
ans -= query(dp[v][0], hi-1-ch) - query(dp[v][0], lo-2-ch);
// cout<<ans<<" ";
ans -= query(dp[v][1], hi-1) - query(dp[v][1], lo-2);
// cout<<ans<<" \n";
}
for(int val: dp[v][1]){
int lo = L-val, hi = R-val;
if(hi<0) break;
ans += query(dp[u][0], hi) - query(dp[u][0], lo-1);
ans += query(dp[u][1], hi) - query(dp[u][1], lo-1);
ans -= query(dp[v][0], hi-1) - query(dp[v][0], lo-2);
ans -= query(dp[v][1], hi-1) - query(dp[v][1], lo-2);
}
}
}
if(u==cen){
ans += query(dp[u][0], R) - query(dp[u][0], L-1);
ans += query(dp[u][1], R) - query(dp[u][1], L-1);
}
// cout<<ans<<endl;
return ans/2;
}
ll solve(int u){
//dfs to calculate centroid
dfs(u, 0);
//find centroid of current tree
int cen = centroid(u, 0);
// cout<<cen<<" * \n";
ll ans = 0;
ans += solvefor(cen);
//mark cen done in global visited
gvis[cen] = 1;
for(auto it: g[cen])
if (!gvis[it.F])
ans += solve(it.F);
// Fo(i, 1, n+1) gvis[i] = 1;
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll i,n,k,j, u, v, w, q;
int t;
cin>>t;
while(t--){
cin>>n>>L>>R;
fo(i, n) cin>>a[i+1];
Fo(i, 1, n+1) g[i].clear(), gvis[i] = 0;
fo(i, n-1){
cin>>u>>v;
g[u].pb({v, 1});
g[v].pb({u, 1});
}
lvl[0] = -1;
cout<<solve(1)<<endl;
}
return 0;
}
int mpow(int base, int exp) {
base %= mod;
int result = 1;
while (exp > 0) {
if (exp & 1) result = ((ll)result * base) % mod;
base = ((ll)base * base) % mod;
exp >>= 1;
}
return result;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgZ2MgZ2V0Y2hhcl91bmxvY2tlZAojZGVmaW5lIGZvKGksbikgZm9yKGk9MDtpPG47aSsrKQojZGVmaW5lIEZvKGksayxuKSBmb3IoaT1rO2k8bjtpKyspCiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgc2koeCkJc2NhbmYoIiVkIiwmeCkKI2RlZmluZSBzbCh4KQlzY2FuZigiJUk2NGQiLCZ4KQojZGVmaW5lIHNzKHMpCXNjYW5mKCIlcyIscykKI2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZmluZSBtcCBtYWtlX3BhaXIKI2RlZmluZSBGIGZpcnN0CiNkZWZpbmUgUyBzZWNvbmQKI2RlZmluZSBhbGwoeCkgeC5iZWdpbigpLCB4LmVuZCgpCiNkZWZpbmUgY2xyKHgpIG1lbXNldCh4LCAwLCBzaXplb2YoeCkpCiNkZWZpbmUgc29ydGFsbCh4KSBzb3J0KGFsbCh4KSkKI2RlZmluZSB0cihpdCwgYSkgZm9yKGF1dG8gaXQgPSBhLmJlZ2luKCk7IGl0ICE9IGEuZW5kKCk7IGl0KyspCiNkZWZpbmUgUEkgMy4xNDE1OTI2NTM1ODk3OTMyMzg0NjI2CnR5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