#include <bits/stdc++.h>
#define MAX 31624
using namespace std;
bitset<31624> primes;
vector<int> all_primes;
void siev(){
primes.set();
primes[0] = 0;
primes[1] = 0;
for(long long i = 2; i < MAX; i++)
if(primes[i]){
for(long long j = i * i; j < MAX; j += i)
primes[j] = 0;
all_primes.push_back((int)i);
}
}
set<int> powers ;
void powers_two(){
int i = 1;
while (i < 1000000000){
powers.insert(i);
i *= 2;
}
}
int main(){
siev();
powers_two();
int t, n, m, nc, mc, ans;cin >> t;
bool prime, possible;
for(int tt = 0; tt< t; tt++){
cin >> n; cin >> m;
nc = n; mc = m;
if(n % m == 0){
ans = 0;
cout << ans << endl;
continue;
}
prime = true;
possible = true;
while (n % 2 == 0){
n /= 2;
}
while (m % 2 == 0){
m /= 2;
}
if(n % m) {
cout << -1 << endl;
}
else{
long long cont = 0;
while (nc % mc){
//cout << nc << endl;
nc %= mc;
cont += nc;
nc *= 2;
}
cout << cont << endl;
}
}
}
535A - Tavas and Nafas | 581A - Vasya the Hipster |
1537B - Bad Boy | 1406B - Maximum Product |
507B - Amr and Pins | 379A - New Year Candles |
1154A - Restoring Three Numbers | 750A - New Year and Hurry |
705A - Hulk | 492B - Vanya and Lanterns |
1374C - Move Brackets | 1476A - K-divisible Sum |
1333A - Little Artem | 432D - Prefixes and Suffixes |
486A - Calculating Function | 1373B - 01 Game |
1187A - Stickers and Toys | 313B - Ilya and Queries |
579A - Raising Bacteria | 723A - The New Year Meeting Friends |
302A - Eugeny and Array | 1638B - Odd Swap Sort |
1370C - Number Game | 1206B - Make Product Equal One |
131A - cAPS lOCK | 1635A - Min Or Sum |
474A - Keyboard | 1343A - Candies |
1343C - Alternating Subsequence | 1325A - EhAb AnD gCd |