#include #include #include #include #define mo 1000000007 using namespace std; int n,s[400005],ss[400005],p[400005], l[200005],r[200005],td[400005]; map ml,mr; void read(int &a){ char c;bool f=0;a=0; while(!((((c=getchar())>='0')&&(c<='9'))||(c=='-'))); if(c!='-')a=c-'0';else f=1; while(((c=getchar())>='0')&&(c<='9'))(a*=10)+=c-'0'; if(f)a=-a; } long long ksm(long long a,long long b){ long long ret=1ll; for (long long lj=a;b;b/=2ll,lj=(lj*lj)%mo) if (b&1ll) ret=(long long)((long long)ret*lj)%mo; return ret; } int main(){ int tt;read(tt); for (;tt--;){ read(n);int tdn=0,pn=0,ans=0;ml.clear();mr.clear(); for (int i=1;i<=n;i++){ read(l[i]);read(r[i]); td[++tdn]=l[i];td[++tdn]=r[i]; } sort(td+1,td+tdn+1); for (int i=2;i<=tdn;i++) if (td[i]!=td[i-1]){ p[++pn]=td[i]-td[i-1]; ml[td[i-1]]=pn;mr[td[i]]=pn; } for (int i=1;i<=pn+3;i++) s[i]=ss[i]=0; for (int i=1;i<=n;i++){ s[mr[r[i]]+1]--;s[ml[l[i]]]++; } int ss=0; for (int i=1;i<=pn;i++){ ss+=s[i]; ans=(ans+p[i]%mo*(ksm(2,ss)-1)%mo*ksm(2,n-ss)%mo)%mo; } printf("%d\n",ans); } }