My Solution
```
/**
 * Time Complexity: O(n + n_queries * log(n))
 * Space Complexity: O(n)
 * where `n` is the length of the vector `nums`
 *       `n_queries` is the length of the vector `queries`
 */
class Solution {
 public:
  vector<int> solveQueries(const vector<int> &nums, const vector<int> &queries) {
    constexpr int not_found = -1;
    const int n = static_cast<int>(nums.size());
    unordered_map<int, vector<int>> num_to_indices;
    for (int i = 0; i < n; ++i) {
      num_to_indices[nums[i]].emplace_back(i);
    }

    vector<int> ret(queries.size());
    for (size_t i = 0; i < queries.size(); ++i) {
      const int index = queries[i];
      const vector<int> &indices = num_to_indices[nums[index]];
      if (indices.size() == 1) {
        ret[i] = not_found;
      } else {
        auto lb = lower_bound(indices.begin(), indices.end(), index);
        ret[i] = min(
          index + (lb == indices.begin() ? n : 0) - (lb == indices.begin() ? indices.back() : *prev(lb)),
          (next(lb) == indices.end() ? n + indices.front() : *next(lb)) - index
        );
      }
    }
    return ret;
  }
};
```
['C++']