#pragma GCC target("avx2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include using namespace std; #include using namespace atcoder; template inline bool chmax(T &a, T b) { return ((a < b) ? (a = b, true) : (false)); } template inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false)); } #define rep(i, n) for (long long i = 0; i < (long long)(n); i++) #define rep2(i, m ,n) for (int i = (m); i < (long long)(n); i++) #define REP(i, n) for (long long i = 1; i < (long long)(n); i++) typedef long long ll; ll sz; ll floor(ll x, ll m) { ll r = (x % m + m) % m; return (x - r) / m; } struct graph{ // 座圧を行う int n=0; vector a; vector b; map mp; vector> table; vector> child; set st; vector cycle; vector depth; vector ans1; vector> ins; vector> del; vector ans2; vector ans3; void add(int i,int j){ mp[i] = a.size(); n++; a.push_back(i); b.push_back(j); } void construction(int l,int r){ if(n==0){return;} rep(i,n){mp[a[i]] = i;} table.resize(20); rep(i,20){table[i].resize(n);} rep(i,n){a[i] = mp[a[i]];b[i] = mp[b[i]];} table[0] = b; rep(i,19){ rep(j,n){ table[i+1][j] = table[i][table[i][j]]; } } child.resize(n); rep(i,n){child[b[i]].push_back(i);} rep(i,n){st.insert(table[19][i]);} ll start = *st.begin(); while(cycle.size()==0||cycle[0]!=b[cycle[cycle.size()-1]]){ if(cycle.size()==0){cycle.push_back(start);continue;} cycle.push_back(b[cycle[cycle.size()-1]]); } sz = cycle.size(); depth.resize(n);rep(i,n){depth[i] = 999999999LL;} queue q; rep(i,sz){q.push(cycle[i]);depth[cycle[i]] = 0;} while(!q.empty()){ auto u = q.front(); q.pop(); rep(i,child[u].size()){ if(depth[child[u][i]]==999999999LL){ q.push(child[u][i]); depth[child[u][i]] = min(depth[child[u][i]],depth[u]+1); } } } ans1.resize(n); ans1[0] = 0; multiset mst; REP(i,n){ if(0<=i-l&&ans1[i-l]!=-1){mst.insert(ans1[i-l]);} if(0<=i-r-1&&ans1[i-r-1]!=-1){mst.erase(mst.find(ans1[i-r-1]));} if(mst.size()==0){ans1[i] = -1;} else{ans1[i] = (*begin(mst))+1;} //cout << ans1[i]<<" "; }//cout< mst1; rep(i,cycle.size()+1){ ins[min((2LL*sz),(i*l)%(ll)(sz))].push_back(i); del[min((2LL*sz),i*r+1-(ll)((i*l)/sz*sz))].push_back(i); } rep(i,2*sz){ rep(j,ins[i].size()){mst1.insert(ins[i][j]);} rep(j,del[i].size()){mst1.erase(mst1.find(del[i][j]));} if(0==mst1.size()){ans2[i] = 999999999999LL;} else{ ans2[i] = (*begin(mst1)); } } ans3.resize(n+sz+1); set mst2;unordered_map mst3; rep(i,sz){ if(i==sz-1){ans3[i]=0;} else{ans3[i] = min(ans2[i+1],ans2[i+sz+1]);}} reverse(ans2.begin(),ans2.end()); rep(i,sz){ mst3[ans3[sz-i-1]] += floor((r-i),(ll)(sz))-floor((l-1-i),(ll)(sz)); if(0newl){newl = (newl+200000000LL*sz)%sz;} mst3[ans3[newl]]++; if(mst3[ans3[newl]]==1){mst2.insert(ans3[newl]);} ll newr = i-r-1; //cout << newl << " "<newr){newr = (newr+200000000LL*sz)%sz;} mst3[ans3[newr]]--; if(mst3[ans3[newr]]==0){mst2.erase(ans3[newr]);} // cout << newl << " "<cycledist){cycledist += sz;} dist += cycledist; // cycleの場合の答え if(99999999>n>>l>>r; vector a(n);rep(i,n){cin>>a[i];a[i]--;} dsu d(n); rep(i,n){d.merge(a[i],i);} vector v(n); rep(i,n){ v[d.leader(i)].add(i,a[i]); } rep(i,n){v[i].construction(l,r);} int q;cin>>q; rep(i,q){ ll from,to;cin>>from>>to;from--;to--; if(!d.same(from,to)){cout<<-1<