#include <bits/stdc++.h>
using namespace std;
void update(int* Bit, int index) {
  for (; index <= 100000; index += (index & (-index))) {
    Bit[index] += 1;
  }
}
int query(int* Bit, int index) {
  int sum = 0;
  for (; index > 0; index -= (index & -index)) sum += Bit[index];
  return sum;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    int maxx;
    for (int i = 31; i >= 0; i--) {
      if (n & (1 << i)) {
        maxx = i;
        break;
      }
    }
    long long ans = 1;
    for (int i = 0; i < maxx; i++) {
      ans = (ans + ans * (1 << i)) % m;
    }
    ans = (ans + m - 1) % m;
    long long d = n - (1 << maxx) + 1;
    ans = (((d + 1) * ans) % m + d) % m;
    cout << ans << endl;
  }
}
