/*n两个人交换箱子互换方向,相当于没有交换。
n当经过2F时间后,所有人的状态没有改变,0层的其中N个箱子搬到了F层,因此先把B对N取模。
求出每个人从当前状态把0层的一个箱子搬到F层需要的时间,排序。
B对N取模的余数就是将最后一个箱子搬到F层的人。*/
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> step;
int main()
{
int p,n,f,b,i,m,flag;
int times,last;
cin >> n;
while(n--){
cin >> p >> f >> b;
step.clear();
for(i=0; i<p; i++){
cin >> m >> flag;
if(flag==0)
step.push_back(f+m); //下m层,去到0层,再搬箱子到F层
else
step.push_back(3*f-m); //上下2f+(f-m)将箱子从m层搬到F层
}
sort(step.begin(), step.end());
times= b/p;
last = b%p;
//2f乘以次数+最后一个人的搬运时间
if(last==0)
cout << (times-1)*2*f + step[p-1] << endl;
else
cout << times*2*f + step[last-1] << endl;
}
return 0;
}