/*
Author: Anas
Date: 15-12-2024
Time: 22:50:54
File: E_Insane_Problem.cpp
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
ll test = 1;
cin >> test;
auto Anas = [&]() {
int k, l1, r1, l2, r2;
cin >> k >> l1 >> r1 >> l2 >> r2;
vector<ll> maxK;
for (int i=0; i < 31; i++) {
ll ans=pow(k,i);
if(ans > r2){
break;
}
maxK.push_back(ans);
}
int cnt=0;
for (auto i:maxK){
for (int j = l2;j <= r2; j++){
if( j % i == 0){
int ans=j/i;
if(ans>=l1 and ans<=r1){
cnt++;
}
}
if(i > j){
break;
}
}
}
cout<<cnt<<"\n";
};
while (test--) {
Anas();
}
return 0;
}
LyoKICAgIEF1dGhvcjogQW5hcwogICAgRGF0ZTogICAxNS0xMi0yMDI0CiAgICBUaW1lOiAgIDIyOjUwOjU0CiAgICBGaWxlOiAgIEVfSW5zYW5lX1Byb2JsZW0uY3BwCiovCiNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdXNpbmcgbGwgPSBsb25nIGxvbmc7CgppbnQgbWFpbigpIHsKICAgIGlvczo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKICAgIGNpbi50aWUoTlVMTCk7CiAgICAKICAgIGxsIHRlc3QgPSAxOwogICAgY2luID4+IHRlc3Q7CgogICAgYXV0byBBbmFzID0gWyZdKCkgewoKICAgICAgICBpbnQgaywgbDEsIHIxLCBsMiwgcjI7CiAgICAgICAgY2luID4+IGsgPj4gbDEgPj4gcjEgPj4gbDIgPj4gcjI7CgogICAgICAgIHZlY3RvcjxsbD4gbWF4SzsKICAgICAgICBmb3IgKGludCBpPTA7IGkgPCAzMTsgaSsrKSB7CiAgICAgICAgICAgIGxsIGFucz1wb3coayxpKTsKICAgICAgICAgICAgaWYoYW5zID4gcjIpewogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgbWF4Sy5wdXNoX2JhY2soYW5zKTsKICAgICAgICB9CiAgICAgICAgaW50IGNudD0wOwogICAgICAgIGZvciAoYXV0byBpOm1heEspewogICAgICAgICAgICBmb3IgKGludCBqID0gbDI7aiA8PSByMjsgaisrKXsKICAgICAgICAgICAgICAgIGlmKCBqICUgaSA9PSAwKXsKICAgICAgICAgICAgICAgICAgICBpbnQgYW5zPWovaTsKICAgICAgICAgICAgICAgICAgICBpZihhbnM+PWwxIGFuZCBhbnM8PXIxKXsKICAgICAgICAgICAgICAgICAgICAgICAgY250Kys7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYoaSA+IGopewogICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICBjb3V0PDxjbnQ8PCJcbiI7CiAgICAgICAgCiAgICB9OwoKICAgIHdoaWxlICh0ZXN0LS0pIHsKICAgICAgICBBbmFzKCk7CiAgICB9CiAgICAKICAgIHJldHVybiAwOwp9Cg==