#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n, m, c = 0, i, tc = 0;
  cin >> n >> m;
  long long x = m % 10;
  long long m1 = 10 * m;
  if (n >= m1) {
    for (i = 1; i < 10; i++) {
      c += ((i * x) % 10);
    }
    tc = (n / m1) * c;
    n = n % m1;
  }
  for (i = 1; i * m <= n; i++) {
    tc += (i * x) % 10;
  }
  cout << tc << "\n";
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
