#include <bits/stdc++.h>
int N, x;
char s[100001], w[100001];
void solve() {
  bool f = false;
  for (long int i = 0; i < strlen(s); i++) {
    if (s[i] == '0') {
      if (i >= x) w[i - x] = '0';
      if (i + x < strlen(s)) w[i + x] = '0';
    }
  }
  for (long int i = 0; i < strlen(s); i++) {
    bool status = false;
    if (i >= x && w[i - x] == '1') status = true;
    if (i + x < strlen(s) && w[i + x] == '1') status = true;
    if (s[i] == '1' && !status) {
      printf("-1");
      return;
    }
  }
  for (long int i = 0; i < strlen(s); i++) printf("%c", w[i]);
}
int main() {
  int T;
  scanf("%d", &T);
  for (int i = 0; i < T; i++) {
    scanf("%s", s);
    if (strlen(s) > 100000) {
      printf("-1\n");
      continue;
    }
    scanf("%d", &x);
    memset(w, '1', strlen(s) * sizeof(char));
    solve();
    printf("\n");
  }
  return 0;
}
