#include using namespace std; typedef long long ll; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b inv_gcd(long long a, long long b) { a = safe_mod(a, b); if (a == 0) return {b, 0}; long long s = b, t = a; long long m0 = 0, m1 = 1; while (t) { long long u = s / t; s -= t * u; m0 -= m1 * u; auto tmp = s; s = t; t = tmp; tmp = m0; m0 = m1; m1 = tmp; } if (m0 < 0) m0 += b / s; return {s, m0}; } std::pair crt(const std::vector& r, const std::vector& m) { assert(r.size() == m.size()); int n = int(r.size()); long long r0 = 0, m0 = 1; for (int i = 0; i < n; i++) { assert(1 <= m[i]); long long r1 = safe_mod(r[i], m[i]), m1 = m[i]; if (m0 < m1) { std::swap(r0, r1); std::swap(m0, m1); } if (m0 % m1 == 0) { if (r0 % m1 != r1) return {0, 0}; continue; } long long g, im; std::tie(g, im) = inv_gcd(m0, m1); long long u1 = (m1 / g); if ((r1 - r0) % g) return {0, 0}; long long x = (r1 - r0) / g % u1 * im % u1; r0 += x * m0; m0 *= u1; if (r0 < 0) r0 += m0; } return {r0, m0}; } unsigned long long floor_sum_unsigned(unsigned long long n, unsigned long long m, unsigned long long a, unsigned long long b) { unsigned long long ans = 0; while (true) { if (a >= m) { ans += n * (n - 1) / 2 * (a / m); a %= m; } if (b >= m) { ans += n * (b / m); b %= m; } unsigned long long y_max = a * n + b; if (y_max < m) break; n = (unsigned long long)(y_max / m); b = (unsigned long long)(y_max % m); std::swap(m, a); } return ans; } long long floor_sum(long long n, long long m, long long a, long long b) { assert(0 <= n && n < (1LL << 32)); assert(1 <= m && m < (1LL << 32)); unsigned long long ans = 0; if (a < 0) { unsigned long long a2 = safe_mod(a, m); ans -= 1ULL * n * (n - 1) / 2 * ((a2 - a) / m); a = a2; } if (b < 0) { unsigned long long b2 = safe_mod(b, m); ans -= 1ULL * n * ((b2 - b) / m); b = b2; } return ans + floor_sum_unsigned(n, m, a, b); } ll gcd(ll a,ll b){ if(b==0) return a; return gcd(b,a%b); } struct UF{ int n; vector par,size,edge; void init(int n_){ n=n_; par.assign(n,-1); size.assign(n,1); edge.assign(n,0); for(int i=0;i G[MAX]; ll bel[MAX],pos[MAX],depth[MAX],ro[MAX],sz[MAX],par[18][MAX]; void DFS(int u,int p,int roro){ ro[u]=roro; par[0][u]=p; for(int to:G[u]){ depth[to]=depth[u]+1; DFS(to,u,roro); } } int main(){ std::ifstream in("text.txt"); std::cin.rdbuf(in.rdbuf()); cin.tie(0); ios::sync_with_stdio(false); ll N,L,R;cin>>N>>L>>R; vector A(N); vector deg(N); for(int i=0;i>A[i];A[i]--; deg[A[i]]++; } queue Q; for(int i=0;i X; int now=i; while(1){ X.push_back(now); now=A[now]; if(now==i) break; } for(int t=0;t>QQ; while(QQ--){ int s,t;cin>>s>>t;s--;t--; int ss=ro[s],tt=ro[t]; if(depth[t]==0){ if(bel[ss]!=bel[tt]){ cout<<-1<<"\n"; continue; } ll a,b; b=(pos[tt]-pos[ss]+sz[ss])%sz[ss]; b+=depth[s]; a=sz[ss]; //cout<1){ ll mid=(left+right)/2; ll sum=floor_sum(mid+1,a,R,-b)-floor_sum(mid+1,a,L,-b-1); if(sum>0) right=mid; else left=mid; } if(right==2*N) cout<<-1<<"\n"; else cout<=0;q--){ if(kyo&(1<