#include <bits/stdc++.h>
#define endl '\n'
#define left jhghajkhja
#define right oauighgajk
#define prev aioghajga
#define next ioyhjhfajasj
#define y0 iuadoghasdgj
#define y1 taklahgjkla
#define remainder pogjuakllhga
#define pow pajklgaklha
#define pow10 iopuioadjlgkah
#define div aljghajkghak
//#define floor hjakjhaja
//#define time ashjlahjka
//#define double_t double
//#define tm kahjflahaajk
using namespace std;
const int N = 1<<17;
template <typename T>
struct implicit_treap {
private:
#define NEUTRAL_VALUE 0
#define NEUTRAL_DELTA 0
struct xorshift {
unsigned long long x,y,z,w;
xorshift(): x(1234567891011121314),
y(5346754754),
z(777777777777777),
w(125573887823)
{}
unsigned long long next() {
unsigned long long t=x^(x<<11);
x=y;y=z;z=w;
return w=w^(w>>19)^t^(t>>8);
}
};
struct node {
T node_value;
T subtree_value;
T delta;
unsigned size;
unsigned long long priority;
node *left, *right;
};
typedef node *node_ptr;
node_ptr root;
xorshift rng;
T join_values(T a, T b) {
return a+b;
}
T join_deltas(T a, T b) {
return a+b;
}
T join_value_with_delta(T a, T b, int length) {
return a+b*length;
}
unsigned long long next() {
return rng.next();
}
node_ptr new_node(T key) {
node_ptr res=(node_ptr)malloc(sizeof(node));
res->node_value=key;
res->subtree_value=key;
res->delta=NEUTRAL_DELTA;
res->size=1;
res->priority=next();
res->left=NULL;
res->right=NULL;
return res;
}
unsigned get_size(node_ptr &a) {
if(a==NULL) return 0;
else return a->size;
}
T get_subtree_value(node_ptr &a) {
if(a==NULL) return NEUTRAL_VALUE;
else return a->subtree_value;
}
void update_node(node_ptr &a) {
if(a==NULL) return;
a->size=1+get_size(a->left)+get_size(a->right);
a->subtree_value=join_values(a->node_value,join_values(get_subtree_value(a->left),get_subtree_value(a->right)));
}
void apply_delta(node_ptr &a, T delta) {
if(a==NULL) return;
a->delta=join_deltas(a->delta,delta);
a->node_value=join_value_with_delta(a->node_value,delta,1);
a->subtree_value=join_value_with_delta(a->subtree_value,delta,a->size);
}
void push_delta(node_ptr &a) {
if(a==NULL) return;
apply_delta(a->left,a->delta);
apply_delta(a->right,a->delta);
a->delta=NEUTRAL_DELTA;
}
void merge(node_ptr &t, node_ptr l, node_ptr r) {
push_delta(l);
push_delta(r);
if(l==NULL) t=r;
else if(r==NULL) t=l;
else if(l->priority<r->priority) merge(l->right,l->right,r),t=l;
else merge(r->left,l,r->left),t=r;
update_node(t);
}
void split(node_ptr t, node_ptr &l, node_ptr &r, int key) {
push_delta(t);
if(t==NULL) l=NULL,r=NULL;
else if(key<=get_size(t->left)) split(t->left,l,t->left,key),r=t;
else split(t->right,t->right,r,key-1-get_size(t->left)),l=t;
update_node(l);
update_node(r);
}
public:
implicit_treap(): root(NULL) {}
void clear() {
root=NULL;
rng=xorshift();
}
unsigned size() {
return get_size(root);
}
void insert(T key, int position) {
node_ptr t1=new node(),t2=new node();
split(root,t1,t2,position-1);
merge(t1,t1,new_node(key));
merge(root,t1,t2);
}
void update(int from, int to, T value) {
node_ptr t1=new node(),t2=new node(),t3=new node();
split(root,t1,t3,to);
split(t1,t1,t2,from-1);
apply_delta(t2,value);
merge(t1,t1,t2);
merge(root,t1,t3);
}
T query(int from, int to) {
node_ptr t1=new node(),t2=new node(),t3=new node();
T ans;
split(root,t1,t3,to);
split(t1,t1,t2,from-1);
ans=get_subtree_value(t2);
merge(t1,t1,t2);
merge(root,t1,t3);
return ans;
}
};
int tests,n,q;
int a[N];
implicit_treap <long long> t;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen("test.txt","r",stdin);
//freopen(IN.c_str(),"r",stdin);
//freopen(OUT.c_str(),"w",stdout);
//fread(buff,1,sizeof(buff),stdin);
int i,type,from,to,w;
scanf("%d", &tests);
while(tests--) {
scanf("%d %d", &n, &q);
t.clear();
for(i=1;i<=n;i++) {
t.insert(0,i);
}
while(q--) {
scanf("%d", &type);
if(type==0) {
scanf("%d %d %d", &from, &to, &w);
t.update(from,to,w);
}
else {
scanf("%d %d", &from, &to);
printf("%lld\n", t.query(from,to));
}
}
}
//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