#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double pii = 2 * pi;
const double eps = 1e-6;
const long long MOD = 1e9 + 7;
void solve() {
  int n, x;
  string s;
  cin >> s;
  cin >> x;
  n = s.size();
  vector<int> ans(n, 1);
  for (int i = 0; i < n; i++)
    if (s[i] == '0') {
      if (i - x >= 0) ans[i - x] = 0;
      if (i + x < n) ans[i + x] = 0;
    }
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      if (!((i - x >= 0 && ans[i - x] == 1) ||
            (i + x < n && ans[i + x] == 1))) {
        cout << "-1\n";
        return;
      }
  for (int i = 0; i < n; i++) cout << ans[i];
  cout << "\n";
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(NULL);
  std::cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
