#include <iostream>
#include <string>
#include <algorithm>
#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
int ilog2(int x) {
return 31 ^ __builtin_clz(x);
}
#elif defined(_MSC_VER)
#include <intrin.h>
int ilog2(int x) {
unsigned long r = 0;
_BitScanReverse(&r, x);
return r;
}
#endif
std::string toBinary(long long l) {
long len = (long)log2(l);
std::string str;
str.resize(len);
// log2(127 = 111 1111) = 6
// log2(128 = 1000 0000) = 7
while (l) {
str[--len] = (l & (1 << len)) + '0';
}
return str;
}
int countBinary(const std::string& line) {
int index = 0;
int len = line.length();
while (line[index++] != ' ') {}
int lbLen = index - 1;
int shift = lbLen;
long long lb = 0; // lower bound
for (int i = 0; i < lbLen; ++i) {
char ch = line[i] - '0';
if (ch > 1) {
lb += 1ll << shift;
break;
}
lb |= (long long)(ch & 1) << (--shift);
}
//std::cout << "lbLen: " << lbLen << std::endl;
//std::cout << "lb: " << lb << " = " << toBinary(lb) << std::endl;
long long ub = 0; // upper bound
while (index != len) {
ub <<= 1;
ub |= (line[index++] != '0');
}
//std::cout << "ub: " << toBinary(ub) << std::endl;
//std::cout << lb << ", " << ub << std::endl;
return ub - lb + 1;
}
static long ilog10(long long x) {
return log10(x);
//static const unsigned char guess[33] = {
// 0, 0, 0, 0, 1, 1, 1, 2, 2, 2,
// 3, 3, 3, 3, 4, 4, 4, 5, 5, 5,
// 6, 6, 6, 6, 7, 7, 7, 8, 8, 8,
// 9, 9, 9
//};
//static const unsigned int tenToThe[] = {
// 1, 10, 100, 1000, 10000, 100000,
// 1000000, 10000000, 100000000, 1000000000,
//};
//unsigned int digits = guess[ilog2(x)];
//return digits + (x >= tenToThe[digits]);
}
int main() {
//while (true) {
// int i;
// std::cin >> i;
// int j = i;// < 10 ? 0 : i - 10;
// std::cout << "log2(" << j << ") = " << ilog10(j) << std::endl;
//}
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
std::cin.ignore(); // Skip newline
std::string line;
for (int i = 0; i < n; ++i) {
std::getline(std::cin, line);
std::cout << countBinary(line) << "\n";
}
}