[백준] 1966 프린터 큐
#include <algorithm>
#include <queue>
#include <vector>
#include <iostream>
using namespace std;
bool cmp(int a, int b) {
	return a > b;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	int t;
	cin >> t;
	while (t--) {
		int n;
		int m;
		cin >> n >> m;
		
		
		queue<pair<int, int>> q;
		vector<int> maxPriority;
		for (int i = 0; i < n; ++i) {
			int priority;
			cin >> priority;
			q.push({ i, priority });
			maxPriority.push_back(priority);
		}
		sort(maxPriority.begin(), maxPriority.end(), cmp);
		
		int seq = 0;
		
		int idx = 0;
		while (true) {
			int curDoc = q.front().first; 
			int curPriority = q.front().second;
			if (curPriority == maxPriority[idx]) {
				
				seq++;
				q.pop();
				idx++;
				if (curDoc == m) break;
			}
			else {
				
				q.push(q.front());
				q.pop();
			}
		}
		cout << seq << "\n";
	}
	return 0;
}