#include <bits/stdc++.h>
using namespace std;
int main() {
  long long q;
  cin >> q;
  for (long long tes = (0); tes < (long long)(q); tes++) {
    long long n, m;
    cin >> n >> m;
    long long sum = 0;
    set<long long> st;
    long long cur = m % 10;
    long long unit = 0;
    do {
      unit++;
      sum += cur;
      cur += m;
      cur %= 10;
    } while (cur != m % 10);
    long long mx = n / m;
    long long div = mx / unit;
    long long ans = div * sum;
    cur = m % 10;
    for (long long i = (0); i < (long long)(mx % unit); i++) {
      ans += cur;
      cur += m;
      cur %= 10;
    }
    cout << ans << endl;
  }
  return 0;
}
