## Algorithm

Sample Input 2 5 2 3 3 4 5 2 2 1 3 0 3 3 5 1 1 4 5 2 3 3 4 5 2 2 1 3 0 3 3 5 1 1 4

Sample Output 72 72

## Code Examples

### #1 Code Example with C Programming

```Code - C Programming```

`````` #include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
using namespace std;

int main() {
int t, n, s, q;
int c, v;
scanf("%d\n",&t);
while(t--){
cin >> n >> s >> q;
int time = 0;
int cur = 0;
int remaining = 0;
stack < int> carrier;
vector<queue<int>> stations(n,queue < int>());
for(int i=0;i < n;i++){
cin >> c;
remaining += c;
while(c--){
cin >> v;
stations[i].push(v);
}
}
while(remaining){
while(!carrier.empty()){
if(carrier.top() == cur+1) {
carrier.pop();
time++;
remaining--;
}
else if(stations[cur].size() >= q) break;
else {
stations[cur].push(carrier.top());
carrier.pop();
time++;
}
}
while(!stations[cur].empty() && carrier.size() < s){
carrier.push(stations[cur].front());
stations[cur].pop();
time++;
}
if(remaining){
cur = (cur+1>%n;
time += 2;
}
}
cout << time << endl;
}
}``````
Copy The Code &

Input

cmd
2
5 2 3
3 4 5 2
2 1 3
0
3 3 5 1
1 4
5 2 3
3 4 5 2
2 1 3
0
3 3 5 1
1 4

Output

cmd
72
72