#include <bits/stdc++.h>
using namespace std;
long long q, m, n, cnt, ans, cnt1;
int main() {
  cin >> q;
  while (q--) {
    cin >> n >> m;
    bool check[10];
    long long digit = 0, rep = 0;
    memset(check, 1, sizeof(check));
    cnt1 = 0;
    digit = m % 10;
    while (check[digit]) {
      check[digit] = 0;
      cnt1 += digit;
      digit = (digit + m) % 10;
      rep++;
    }
    cnt = n / m;
    ans = 0;
    ans += cnt / rep * cnt1;
    cnt -= cnt / rep * rep;
    digit = 0;
    while (cnt > 0) {
      cnt--;
      digit = (digit + m) % 10;
      ans += digit;
    }
    cout << ans << endl;
  }
  return 0;
}
