<p>Given the array <code>queries</code> of positive integers between <code>1</code> and <code>m</code>, you have to process all <code>queries[i]</code> (from <code>i=0</code> to <code>i=queries.length-1</code>) according to the following rules:</p>

<ul>
	<li>In the beginning, you have the permutation <code>P=[1,2,3,...,m]</code>.</li>
	<li>For the current <code>i</code>, find the position of <code>queries[i]</code> in the permutation <code>P</code> (<strong>indexing from 0</strong>) and then move this at the beginning of the permutation <code>P</code>. Notice that the position of <code>queries[i]</code> in <code>P</code> is the result for <code>queries[i]</code>.</li>
</ul>

<p>Return an array containing the result for the given <code>queries</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> queries = [3,1,2,1], m = 5
<strong>Output:</strong> [2,1,2,1] 
<strong>Explanation:</strong> The queries are processed as follow: 
For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is <strong>2</strong>, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. 
For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is <strong>1</strong>, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. 
For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is <strong>2</strong>, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. 
For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is <strong>1</strong>, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. 
Therefore, the array containing the result is [2,1,2,1].  
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> queries = [4,1,2,2], m = 4
<strong>Output:</strong> [3,1,2,0]
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> queries = [7,5,5,8,3], m = 8
<strong>Output:</strong> [6,5,0,7,5]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= m &lt;= 10^3</code></li>
	<li><code>1 &lt;= queries.length &lt;= m</code></li>
	<li><code>1 &lt;= queries[i] &lt;= m</code></li>
</ul>

#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
static auto _ = [](){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    return nullptr;
}();

class Solution {
public:
    std::vector<int> processQueries(std::vector<int>& queries, int m) {
        std::deque<int> p;
        for (std::size_t i = 1; i <= m; ++i) {
            p.push_back(i);
        }

        std::vector<int> result;

        for (const auto q : queries) {
            auto it = std::find(p.begin(), p.end(), q);
            result.push_back(std::distance(p.begin(), it));
            const auto v = *it;
            p.erase(it);
            p.insert(p.begin(), v);
        }

        return result;
    }
};


/*


[3,1,2,1]
5
[4,1,2,2]
4
[7,5,5,8,3]
8
[7,5,5,8,3,7,5,5,8,3,7,5,5,8,3,7,5,5,8,3,1,1,1,1,1,1,1,1,2,2,2,2,3,3,4,4,7,5,5,8,3,7,5,5,8,3,7,5,5,8,3,7,5,5,8,3,1,1,1,1,1,1,1,1,2,2,2,2,3,3,4,4,7,5,5,8,3,7,5,5,8,3,7,5,5,8,3,7,5,5,8,3,1,1,1,1,1,1,1,1,2,2,2,2,3,3,4,4,7,5,5,8,3,7,5,5,8,3,7,5,5,8,3,7,5,5,8,3,1,1,1,1,1,1,1,1,2,2,2,2,3,3,4,4,7,5,5,8,3,7,5,5,8,3,7,5,5,8,3,7,5,5,8,3,1,1,1,1,1,1,1,1,2,2,2,2,3,3,4,4,7,5,5,8,3,7,5,5,8,3,7,5,5,8,3,7,5,5,8,3,1,1,1,1,1,1,1,1,2,2,2,2,3,3,4,4,7,5,5,8,3,7,5,5,8,3,7,5,5,8,3,7,5,5,8,3,1,1,1,1,1,1,1,1,2,2,2,2,3,3,4,4,7,5,5,8,3,7,5,5,8,3,7,5,5,8,3,7,5,5,8,3,1,1,1,1,1,1,1,1,2,2,2,2,3,3,4,4,7,5,5,8,3,7,5,5,8,3,7,5,5,8,3,7,5,5,8,3,1,1,1,1,1,1,1,1,2,2,2,2,3,3,4,4]
500
[7,5,5,8,3,7,5,5,8,3,7,5,5,8,3,7,5,5,8,3,1,1,1,1,1,1,1,1,2,2,2,2,3,3,4,4,7,5,5,8,3,7,5,5,8,3,7,5,5,8,3,7,5,5,8,3,1,1,1,1,1,1,1,1,2,2,2,2,3,3,4,4,7,5,5,8,3,7,5,5,8,3,7,5,5,8,3,7,5,5,8,3,1,1,1,1,1,1,1,1,20,20,20,20,30,30,40,40,70,50,50,80,30,70,50,50,80,30,70,50,50,80,30,70,50,50,80,30,10,10,10,10,10,10,10,10,20,20,20,20,30,30,40,40,70,50,50,80,30,70,50,50,80,30,70,50,50,80,30,70,50,50,80,30,10,10,10,10,10,10,10,10,20,20,20,20,30,30,40,40,70,50,50,80,30,70,50,50,80,30,70,50,50,80,30,70,50,50,80,30,10,10,10,10,10,10,10,10,20,20,20,20,30,30,40,40,70,50,5,8,3,7,5,5,8,3,700,500,500,800,300,700,500,500,800,300,100,100,100,100,100,100,100,100,200,200,200,200,300,300,400,400,700,500,500,800,300,700,500,500,800,300,700,500,500,800,300,700,500,500,800,300,100,100,100,100,100,100,100,100,200,200,200,200,300,300,400,400,700,500,500,800,300,700,500,500,800,300,700,500,500,800,300,700,500,500,800,300,100,100,100,100,100,100,100,100,200,200,200,200,300,300,4,4]
1000


*/
14400