C++ || hashing and binary search || O( Q * log n) || 
Code

# Code
```cpp []
class Solution {
public:
    vector<int> solveQueries(vector<int>& nums, vector<int>& queries) {
        unordered_map<int, vector<int>> map;
        vector<int> result; // result
        int n = nums.size(); // size of nums 
        for (int i = 0; i < n; i++) {
            // store each value and its occuring indices/position
            map[nums[i]].push_back(i);
            // map[nums[i]] := stores indices where the value 'nums[i]' occurred in sorted order
        }

        // iterate through queries (here, queries are in form of indices)
        for (auto& query : queries) {
            // if query is NOT a valid index OR
            // frequency of the value 'nums[query]' is only one ie 'nums[query]' occurs only once 
            if (query < 0 || query >= n || map[nums[query]].size() == 1) {
                result.push_back(-1);
                continue;
            }

            // get all indices where the value 'nums[query]' exists
            vector<int>& indices = map[nums[query]];
            int sz = indices.size();

            // find index of 'query' in list of indices 
            auto it = std::lower_bound(begin(indices), end(indices), query);
            int pos = it - indices.begin(); 
            // pos  := index/position of 'query' in 'indices' 
            int minDist = INT_MAX;

            // if pos = 0 or pos = sz - 1 ie either the first or last occurrence of nums[query]
            // find circular distance 
            if (pos == 0 || pos == sz - 1) {
                int dist = std::min(abs(indices[0] - indices[sz - 1]),
                                    n - (abs(indices[0] - indices[sz - 1])));
                minDist = std ::min(dist, minDist);
            }

            // if exists to the left 
            if (pos - 1 >= 0) {
                int dist = std::min(abs(indices[pos] - indices[pos - 1]),
                                    n - (abs(indices[pos] - indices[pos - 1])));
                minDist = std ::min(minDist, dist);
            }

            // if exists to the right 
            if (pos + 1 < sz) {
                int dist = std::min(abs(indices[pos + 1] - indices[pos]),
                                    n - (abs(indices[pos + 1] - indices[pos])));
                minDist = std ::min(minDist, dist);
            }
            result.push_back(minDist);
        }
        return result;
    }
};
```
['Array', 'Hash Table', 'Binary Search', 'C++']