#include <bits/stdc++.h>
using namespace std;
int ans[100005];
void solve() {
  string s;
  cin >> s;
  int n = s.length();
  s = "#" + s;
  int x;
  cin >> x;
  memset(ans, -1, sizeof ans);
  for (int i = 1; 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 = 1; i <= n; ++i)
    if (ans[i] == -1) ans[i] = 1;
  for (int i = 1; i <= n; ++i) {
    bool ok = 1;
    if (s[i] == '0') {
      if (i - x > 0 && ans[i - x] != 0) ok = 0;
      if (i + x <= n && ans[i + x] != 0) ok = 0;
    } else {
      ok = 0;
      if (i - x > 0 && ans[i - x] == 1) ok = 1;
      if (i + x <= n && ans[i + x] == 1) ok = 1;
    }
    if (!ok) {
      cout << -1 << endl;
      return;
    }
  }
  for (int i = 1; i <= n; ++i) cout << ans[i];
  cout << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  cin >> t;
  while (t--) solve();
}
