#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int N=200005;
struct node{
ll l;
ll r;
int p;
int okp;//上一个走谁
ll okl;//之前最左边
}a[N];
bool cmp(node n1,node n2){
if(n1.r!=n2.r){
return n1.r>n2.r;
}
return n1.l>n2.l;
}
ll sum[N]={0};
bool vis[N]={0};
void solve(int tt){
int n;
ll f;
scanf("%d %lld",&n,&f);
ll all=0;
for(int i=1;i<=n;i++){
vis[i]=0;
sum[i]=0;
ll x,y;
scanf("%lld %lld",&x,&y);
a[i]={x,y,i,-1,y};
all+=(y-x);
}
if(tt==13){
cout<<n<<"nf"<<f;
for(int i=1;i<=n;i++){
cout<<"l"<<l<<"r"<<r;
}
}
sort(a+1,a+1+n,cmp);
sum[a[1].p]=0;
for(int i=2;i<=n;i++){
if(a[i].r>=a[i-1].l){//往上无伤
if(a[i-1].l<a[i-1].okl){//左边更小
a[i].okl=a[i-1].l;
a[i].okp=i-1;
}else{
a[i].okl=a[i-1].okl;
a[i].okp=a[i-1].okp;
}
sum[a[i].p]=sum[a[i-1].p];
}else{//往上有伤
if(a[i-1].l<a[i-1].okl){//左边更小
a[i].okl=a[i-1].l;
a[i].okp=i-1;
sum[a[i].p]=sum[a[i-1].p]+a[i-1].l-a[i].r;
}else{
a[i].okl=a[i-1].okl;
a[i].okp=a[i-1].okp;
sum[a[i].p]=sum[a[i-1].okp]+max(0ll,a[i-1].okl-a[i].r);
}
}
}
ll mini=1e17;
int st=-1;
for(int i=1;i<=n;i++){
if(max(0ll,a[i].l-f)+sum[a[i].p]<mini){
mini=max(0ll,a[i].l-f)+sum[a[i].p];
st=i;//逻辑标号
}
}
all+=mini;
printf("%lld\n",all);
int s=0;
for(int i=st;i!=-1;i=a[i].okp){
if(s==1){
printf(" ");
}
s=1;
printf("%d",a[i].p);
vis[a[i].p]=1;
}
for(int i=1;i<=n;i++){
if(!vis[a[i].p]){
if(s==1){
printf(" ");
}
s=1;
printf("%d",a[i].p);
vis[a[i].p]=1;
}
}
}
int main(){
int t=1;
scanf("%d",&t);
int tt=1;
while(t--){
solve(tt);
tt++;
printf("\n");
}
return 0;
}
/*
6 1
5 10
1 9
1 8
2 3
4 7
6 7
*/