https://codeforces.com/problemset/problem/1594/B
思路:第
K
K
K个数可以用二进制表示,那么这个二进制对应的基数
N
N
N就是第
K
K
K大的数。
#include<bits/stdc++.h>
using namespace std;
// clock_t start, end;
// start = clock();
// end = clock();
// cout << (double) (end - start) / CLOCKS_PER_SEC << endl;
//ios::sync_with_stdio(false);
#define int long long
#define rep(i, x, y) for(int i=(x);i<=(y);++i)
#define dep(i, x, y) for(int i=(x);i>=(y);--i)
#define gcd(a, b) __gcd(a,b)
const double pi = acos(-1);
const long long mod = 1e9 + 7;
const int maxn = 1e3 + 10;
int lowbit(int x) { return x & -x; }
bool ispow(int n) { return (n & (n - 1)) == 0; }//O(1) 判断是否是 2^k(2的k次方)
int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int fast(int a, int n) {
int base = a, res = 1;
while (n) {
if (n & 1)res =((res%mod)*( base%mod)) % mod;
base =((base%mod)* (base%mod))%mod;
n >>= 1;
}
return res;
}
signed main() {
int t;
cin>>t;
while(t--){
int n,k;
cin>>n>>k;
int ans=0;
int i=0;
while(k){
int z=k%2;
if(z) {
ans = (ans % mod + (int) fast(n, i) % mod) % mod;
}
k/=2;
i++;
}
cout<<ans<<endl;
}
return 0;
}