#include <bits/stdc++.h>   class Counter {    public:     Counter(std::vector<size_t> perm1, std::vector<size_t> perm2)         : size_(perm1.size()) {       std::vector<size_t> inv2(size_, 0);       for (size_t i = 0; i < size_; ++i) {         inv2[perm2[i]] = i;       }       auto& points = perm1;       for (size_t x = 0; x < size_; ++x) {         points[x] = inv2[perm1[x]];       }       size_t nextPowOfTwo = 1;       while (nextPowOfTwo < size_) nextPowOfTwo <<= 1;       segTree_.resize(2 * nextPowOfTwo - 1);       build(points, 0, 0, size_ - 1);     }     int count(size_t minX, size_t maxX, size_t minY, size_t maxY) {       return countAux(0, 0, size_ - 1, minX, maxX, minY, maxY);     }       private:     void build(const std::vector<size_t>& points, int vertex, int from, int to) {       if (from == to) {         segTree_[vertex].push_back(points[from]);       } else {         int middle = from + (to + 1 - from) / 2;         build(points, 2 * vertex + 1, from, middle - 1);         build(points, 2 * vertex + 2, middle, to);         const auto& left = segTree_[2 * vertex + 1];         const auto& right = segTree_[2 * vertex + 2];         segTree_[vertex].reserve(left.size() + right.size());         std::merge(left.begin(), left.end(), right.begin(), right.end(),                    std::back_inserter(segTree_[vertex]));       }     }     int countAux(int vertex, int from, int to, int minX, int maxX, int minY,                  int maxY) {       if (maxX < minX) {         return 0;       }       if (from == minX && to == maxX) {         const auto& range = segTree_[vertex];         auto it1 = std::lower_bound(range.begin(), range.end(), minY);         auto it2 = std::lower_bound(range.begin(), range.end(), maxY + 1);         return it2 - it1;       }       int middle = from + (to + 1 - from) / 2;       int countLeft = countAux(2 * vertex + 1, from, middle - 1, minX,                                std::min(maxX, middle - 1), minY, maxY);       int countRight = countAux(2 * vertex + 2, middle, to,                                 std::max(minX, middle), maxX, minY, maxY);       return countLeft + countRight;     }       private:     size_t size_;     std::vector<std::vector<int>> segTree_;   };   int main() {     size_t size;     ::scanf( %lu , &size);     auto readPerm = [](size_t size) {       std::vector<size_t> perm;       perm.reserve(size);       for (size_t i = 0; i < size; ++i) {         size_t elem;         ::scanf( %lu , &elem);         --elem;         perm.push_back(elem);       }       return perm;     };     auto perm1 = readPerm(size);     auto perm2 = readPerm(size);     Counter counter(std::move(perm1), std::move(perm2));     size_t nQueries;     ::scanf( %lu , &nQueries);     size_t x = 0;     auto f = [&](size_t z) { return (z + x - 1) % size; };     for (size_t i = 0; i < nQueries; ++i) {       size_t a, b, c, d;       ::scanf( %lu %lu %lu %lu , &a, &b, &c, &d);       size_t count = counter.count(std::min(f(a), f(b)), std::max(f(a), f(b)),                                    std::min(f(c), f(d)), std::max(f(c), f(d)));       std::cout << count <<   n ;       x = count + 1;     }   }   