//Let's join Kaede Takagaki Fan Club !! #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include //#include #include #include #include #include #include #include #include #include //#include //#include //#include //#include //#include //#include using namespace std; //using namespace atcoder; #define int long long //#define L __int128 typedef long long ll; typedef pair P; typedef pair P1; typedef pair P2; #define pu push #define pb push_back #define eb emplace_back #define mp make_pair #define eps 1e-7 #define INF 1000000000 #define a first #define b second #define fi first #define sc second #define rng(i,a,b) for(int i=(int)(a);i<(int)(b);i++) #define rep(i,x) for(int i=0;i=(int)(a);i--) #define per(i,b) gnr(i,0,b) #define repn(i,x) for(int i=1;i<=x;i++) #define SORT(x) sort(x.begin(),x.end()) #define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end()) #define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) #define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) #define all(x) x.begin(),x.end() #define si(x) (int)(x.size()) #define pcnt(x) __builtin_popcountll(x) #define all(x) x.begin(),x.end() #ifdef LOCAL #define dmp(x) cerr<<__LINE__<<" "<<#x<<" "< bool chmax(t&a,u b){if(a bool chmin(t&a,u b){if(b using vc=vector; template ostream& operator<<(ostream& os,const pair& p){ return os<<"{"< ostream& operator<<(ostream& os,const vc& v){ os<<"{"; for(auto e:v)os<> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } //don't make x negative! size_t operator()(pair x)const{ return operator()(uint64_t(x.first)<<32|x.second); } }; //unordered_set -> dtype, null_type //unordered_map -> dtype(key), dtype(value) using namespace __gnu_pbds; template using hash_table=gp_hash_table; template void o(const T &a,bool space=false){ cout << a << (space?' ':'\n'); } //ios::sync_with_stdio(false); const ll mod = 998244353; //const ll mod = 1000000007; mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count()); struct mint{ ll v; mint(ll vv=0){s(vv%mod+mod);} mint& s(ll vv){ v=vv>=1; } return res; } mint inv()const{return pow(mod-2);} friend mint operator+(ll x,const mint&y){ return mint(x)+y; } friend mint operator-(ll x,const mint&y){ return mint(x)-y; } friend mint operator*(ll x,const mint&y){ return mint(x)*y; } friend mint operator/(ll x,const mint&y){ return mint(x)/y; } friend ostream& operator<<(ostream&os,const mint&m){ return os<>(istream&is,mint&m){ ll x;is>>x; m=mint(x); return is; } bool operator<(const mint&r)const{return v0){ if(n&1) res=res*x%_md; x=x*x%_md; n>>=1; } return res; } #define _sz 1 mint F[_sz],R[_sz]; void make(){ F[0] = 1; for(int i=1;i<_sz;i++) F[i] = F[i-1] * i; R[_sz-1] = F[_sz-1].pow(mod-2); for(int i=_sz-2;i>=0;i--) R[i] = R[i+1] * (i+1); } mint C(int a,int b){ if(b < 0 || a < b) return mint(); return F[a]*R[b]*R[a-b]; } using ld=long double; using vi=vc; using ull=unsigned long long; /*int dst(P p, P q){ return (p.a-q.a)*(p.a-q.a)+(p.b-q.b)*(p.b-q.b); }*/ /*template void add_inplace(t &a, t b){ if(a.size() < b.size()) swap(a, b); for(int i=0;i= mod) a[i] -= mod; } return ; }*/ template void ov(const vc&a){ if(a.empty()) return; rep(i, si(a)) cout << a[i] << (i+1==si(a)?'\n':' '); } //o(ans?"Yes":"No"); #define sz 100005 int par[sz],ran[sz]; void init(){ for(int i=0;i=0) return x/y; ll vl=md(-x,y); if((-x)%y==0) return -vl; else return -vl-1; } //[(ax+b)/n] (s<=x<=t) ll count(ll s,ll t,ll n,ll a,ll b){ if(a < 0){ return count(-t,-s,n,-a,b); } ll S = md(a*s+b,n), T = md(a*t+b,n); if(S == T) return (t-s+1)*S; if(n <= a){ ll sum = (s+t)*(t-s+1)/2LL; return count(s,t,n,a%n,b)+sum*(a/n); } ll sum = count(S+1,T,a,n,-b-1); ll all = T*t-S*(s-1); return all-sum; } }gs; void solve(){ int n,lb,ub;cin>>n>>lb>>ub; vi a(n+1); vce(n+1); repn(i,n)cin>>a[i]; repn(i,n)e[a[i]].pb(i); init(); repn(i,n) unite(i,a[i]); vi cnt(n+1); repn(i,n) cnt[find(i)] ++; vi cyc(n+1, -1), len(n+1); vi dep(n+1); vcup(20,vi(n+1,-1)); auto dfs=[&](auto self,int v,int u)->void{ dep[v] = dep[u] + 1; up[0][v] = u; for(auto to:e[v]){ self(self,to,v); } }; repn(i,n){ if(i != find(i)) continue; int now = i; rep(_, cnt[i]*2) now = a[now]; int beg = now; rep(_, cnt[i]*2){ cyc[now] = _; now = a[now]; if(now == beg){ len[i] = _+1; break; } } now = beg; rep(_, cnt[i]*2){ for(auto to:e[now]){ if(cyc[to] == -1) dfs(dfs, to, now); } now = a[now]; if(now == beg) break; } } rep(j,19){ repn(i,n){ if(up[j][i] == -1) up[j+1][i] = -1; else up[j+1][i] = up[j][up[j][i]]; } } int q;cin>>q; while(q--){ int s,t;cin>>s>>t; if(!same(s,t)) o(-1); else{ int beg, d; if(cyc[s] >= 0 and cyc[t] < 0){ o(-1); continue; } else if(cyc[t] >= 0){ if(cyc[s] == -1){ beg = dep[s]; rep(_, 20) if(((beg>>_)&1)) s = up[_][s]; } else beg = 0; int diff = cyc[t] - cyc[s]; diff = (diff % len[find(s)] + len[find(s)]) % len[find(s)]; beg += diff; d = len[find(s)]; } else{ if(dep[s] < dep[t]){ o(-1); continue; } else{ int D = dep[s] - dep[t]; rep(_,20) if(((D>>_)&1)) s = up[_][s]; if(s == t){ beg = D, d = 0; } else{ o(-1); continue; } } } //cout << beg << " d " << d << endl; if(d == 0){ int x = (beg+ub-1)/ub; if(x*lb > beg) o(-1); else o(x); } else{ cerr << "IN\n"; int l2b = -1, u2b = 1000000; while(u2b-l2b > 1){ int mid = (l2b+u2b)/2; if(gs.count(0,mid,lb,d,beg) > gs.count(0,mid,ub,d,beg-1)){ u2b = mid; } else l2b = mid; } if(u2b == 1000000) o(-1); else{ int tar = beg+d*u2b; o((tar+ub-1)/ub); } } } } } signed main(){ cin.tie(0); ios::sync_with_stdio(0); cout<> t; while(t--) solve(); }