
#include "bits/stdc++.h"
#define int long long

using namespace std;

void solve(){
	int n;
	cin>>n;
	int a;
	int o[100]={0};
	int ans =0;
		cin>>a;
		for(int j=0;j<=63;++j){
			if(a&(1LL<<j)){
				++o[j];
			}
		}
	}

	for(int i=0;i<=63;++i){
		if(o[i]!=0 && o[i]!=n){
			int x = o[i]*(n-o[i]);
			for(int j=0;j<i;++j){
				x*=2;
				x%= 1000000007LL;
			}
			ans+=x;
			ans%=1000000007LL;
		}
	}	
	cout<<ans%(1000000007LL)<<endl;
}

int main(){
	IOS
	int t=1;
	cin>>t;
	while(t--)
		solve();
	return 0;
}

