#include <bits/stdc++.h>
using namespace std;
void FJ() {}
vector<pair<int, int> > V[107];
bool visit[107];
bool bfs(int u, int v, int c) {
  list<int> Q;
  Q.push_back(u);
  memset(visit, false, sizeof(visit));
  visit[u] = true;
  while (!Q.empty()) {
    int s = Q.front();
    Q.pop_front();
    for (typeof((V[s]).begin()) it = (V[s].begin()); it != V[s].end(); it++) {
      if (it->first == v && it->second == c) return true;
      if (!visit[it->first] && it->second == c) {
        visit[it->first] = true;
        Q.push_back(it->first);
      }
    }
  }
  return false;
}
int main() {
  FJ();
  int n;
  scanf("%d", &n);
  int m;
  scanf("%d", &m);
  vector<int> cnt;
  map<int, int> mp;
  for (int i = 0; i < m; i++) {
    int u;
    scanf("%d", &u);
    int v;
    scanf("%d", &v);
    int c;
    scanf("%d", &c);
    V[u].push_back(make_pair(v, c));
    V[v].push_back(make_pair(u, c));
    if (mp[c] == 0) cnt.push_back(c);
    mp[c]++;
  }
  int Q;
  scanf("%d", &Q);
  for (int i = 0; i < Q; i++) {
    int u;
    scanf("%d", &u);
    int v;
    scanf("%d", &v);
    int R = 0;
    for (typeof((cnt).begin()) it = (cnt.begin()); it != cnt.end(); it++)
      if (bfs(u, v, *it)) R++;
    printf("%d\n", R);
  }
  return 0;
}
